MATLAB: Equation and its tangent with x

function and its tangent in a graph

The following code finds the tangent of a function which is y=x*x+2x at point x1=1 and plots both function and its tangent. The code includes the equation of the tangent line which is z(x) function. My problem is that I need to plot the same graph for x1=1 x1=2 and x1=3 in the same figure..
When I change 4th line x1=1:3 then there is an error.
thanks for comments please do not think that it is a math problem… best wishes,
____________________
syms x f(x) z(x)
f(x)=x.*x+2*x
df=diff(f,x)
x1=1:1
m=df(x1)
z1=f(x1)
z(x)=z1+(m*(x-x1))
figure
x=-7:5;
plot (x,f(x),x,z(x))
hold on

Best Answer

syms x f(x) z(x)
f(x)=x.*x+2*x;
df=diff(f,x);
x1=1:3;
m=df(x1);
z1=f(x1);
hold on
x=-7:.1:5;
plot(x,f(x));
for i=1:3
z = double(z1(i))+(double(m(i))*(x-x1(i)));
plot(x,z);
end
Related Question