As far as showing the iterations graphically, this is the best I can do:
clc
clear all
close
G=zeros(20,10);
X=linspace(2.5,3.5,20)
G(:,1)=X;
for i=1:length(X)
x=X(i)
fx=tanh(x^2-9);
pog=0.0001;
br=1;
while br<10;
xk= x-((tanh(x^2-9))/(2*x*sech(9-x^2)^2));
G(i,br+1)=xk;
x=xk;
br=br+1;
end
end
I=tanh(G(:,end).^2-9)<1e-5;
X(I)
plot(G,1:10)
hold off
axis([2.5 3.5 0 5])
X(I) is a list of starting values the converge to the root, and the plot shows iteration number on the y-axis, and the guess at that iteration on the x-axis. You can follow each starting value through and see what happens.
![enter image description here]()
Here's another way of visualising Newton's method. It shows the tangent line that is constructed, and where it passes through 0 which gives you the new x values, from which a vertical line gives you the new function value, which defines a new tangent line for the next iteration. It might help.
clc
clear all
close
G=zeros(20,10);
X=linspace(2.75,3.25,20)
G(:,1)=X;
x2=2:.01:4;f2=@(x) tanh(x.^2-9); %// to help with plotting
for i=1:length(X)
x=X(i)
fx=tanh(x^2-9);
pog=0.0001;
br=1;
xk=x;
while br<10;
%// Newton method step
dx=((tanh(x^2-9))/(2*x*sech(9-x^2)^2));
xk=x-dx;
%// plotting everything
G(i,br+1)=xk;
plot(x2,f2(x2))
hold all
plot(G(i,br:br+1),f2(G(i,br:br+1)),'.','MarkerSize',16)
plot(x2,f2(x)+(x2-x)./(xk-x).*(-f2(x)))
plot(xk,0,'.','MarkerSize',16)
plot(x2,(x2-xk)*(2*xk*sech(9-xk^2)^2)+f2(xk))
plot([xk xk],[-1 1])
plot([2 4],[0 0],'k')
axis([2 4 -1 1])
drawnow
pause
hold off
%// finishing Newton step
x=xk;
br=br+1;
end
hold off
end