MATLAB: Displaying the answer of Newton’s method for multiple roots

newton's method multiple roots

Hello. I'm looking for a way to display my solutions for a Newton's method. There are 4 roots in my interval.
roots=[-0.0505 1.3232 1.3636 2.3333] %initial guesses corresponding to the 4 roots.
The trouble i'm having is that the script only shows the results for first root using -0.0505, while m reaches the value 4. I don't know why it cant display the other results for m=2,3,4. What could be the problem? Thanks!
m=1;
while m<=length(roots)
x=roots(m); % initial guess for the root
tol=10e-8;
fprintf(' k xk fx dfx \n');
for k=1:50 % iteration number
fx=sin(x^(2))+x^(2)-2*x-0.09;
dfx=(2*(x*cos(x^2)+x-1));
fprintf('%3d %12.8f %12.8f %12.8f \n', k,x,fx,dfx);
x=x-fx/dfx;
m=m+1;
if abs(fx/dfx)<tol
return;
end
end
end
RUN
k xk fx dfx
1 -0.05050505 0.01611162 -2.20201987
2 -0.04318831 0.00010707 -2.17275307
3 -0.04313903 0.00000000 -2.17255596

Best Answer

for k=1:50
fx=sin(x^(2))+x^(2)-2*x-0.09;
dfx=(2*(x*cos(x^2)+x-1));
fprintf('%3d %12.8f %12.8f %12.8f \n', k,x,fx,dfx);
x=x-fx/dfx;
m=m+1;
You increase m on each iteration step.
Related Question