Why Manipulate command is not working when ploting the tangent line?

Hi all,

I’m trying to get an interactive output that allows me to plot both the function f and its tangent line y by changing the parameter x0. However I’m not able to obtain the plot of y. This is the code:

f[x_] = x^2;
y[x_] = f[x0] + f'[x0]*(x - x0);
Manipulate[Plot[{y[x], f[x]}, {x, -4, 10}], {x0, 0, 6}]  

And this what I get:

As you see no tangent line appears.

Thanks a lot!

I found that if I write the whole expression of the tangent line inside the Manipulate command it works:

f[x_] = x^2;
Manipulate[Plot[{f[x0] + f'[x0]*(x - x0), f[x]}, {x, -4, 10}], {x0, 0, 6}] 

Why is that?

Thanks!

Your original posted code does not work as in the illustration. The function f and y are missing their arguments.

The reason it wouldn’t work with f and y is that Manipulate localizes x0. This means the x0 symbol in the definition of y is a different symbol than the x0 in Manipulate.

Here’s a way to get what seems intended:

f[x_] = x^2;
y[x_, x0_] = f[x0] + f'[x0]*(x - x0);
Manipulate[Plot[{y[x, x0], f[x]}, {x, -4, 10}], {x0, 0, 6}]

Thanks a lot Michael now it works!

I did write the missing arguments of y and f in Mathematica but for some reason they dissapeared when copying them here.

Notes on typing posts, including keeping code looking good, are in

http://community.wolfram.com/groups/-/m/t/151347

I put your code into MCode boxes and the function arguments reappeared.

Thanks, Bruce!