Approximations of solution using Newton's Method

I have to implement the Newton method in Matlab to plot the function,its tangent and the first 4 approximations. I would like to show how the algorithm works, that mean that the tangent of one approximation finds the next approximation. The code I have written is the following:
plot(x,f(x))
hold on
for j=1:4
x_1=x_0-f(x_0)/F(x_0);
tangent=@(x) F(x_0)*(x-x_0)+f(x_0);
line=@(x) (f(x_0)/(x_0-x_1))*(x-x_1);
plot(x_0,f(x_0),x,line(x),x_1,0)
x_0=x_1;
end
where F is the derivative of f and the function f is f(x)=e^x-x/2. Could you tell me if this is right? The plot I got is at the attachment.. Also at the plot the range of y is [-2000,3000], how could I make it smaller so that I can see the approximations better?

6 commentaires

What is the point of constructing "tangent" there? Remember it is going to be overwritten in the next iteration of the "for" loop.
It is not recommended to name a variable "line" as line() is the name of the command to draw lines in MATLAB.
evi
evi le 14 Nov 2013
Ok, I changed the name of the variable... I wanted to check firstly if the graph of the function and the approximations are plotted right... Are they right?? As you can see the y-axis is from -2000 to 3000, how can I change that so that I can see the resuts better???
See xlim and ylim
At the beginning of the function I wrote
x=-2:0.00001:8;
ylim([-4 6])
but there is no change...Why? Do I something wrong??
Set ylim after you plot.
evi
evi le 15 Nov 2013
Great now it shows only a specific range of y!! Could you also tell me how the x-axis & y-axis could be shown in the plot??

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Question posée :

evi
le 14 Nov 2013

Commenté :

evi
le 15 Nov 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by