0
votes

I asked for help about Matlab task few weeks ago but removed it since it took me some time to solve. Unfortunately, I still have a problem.

Task is: Find a real root of the function f(x)=tanh(x^2 - 9) using at least 3 iterations, using Newton-Raphson method. x= 3.2 show each iteration graphically.

In code "pog" means min. mistake, "br" is counter.

If I don't put a counter, it immediately gives me "NaN", with counter it does write first few calculations.

My code:

clc

clear all

x=3.2;

fx=tanh(x^2-9);

iter=5;

pog=0.01;

br=1;

while br<10;

xk= x-((tanh(x^2-9))/(-2*x*(tanh(x^2 - 9)^2 - 1)));

fprintf ('x=%g\txk=%g\t%g\n', x,xk, abs(xk-x))

if pog>abs(xk-x);

break

end

x=xk;

br=br+1;

end

Thank you in advance!

1
possible duplicate of Newton-Raphson Method in Matlab - rayryeng
@rayryeng: I disagree, it seems to me the code is correct, but the initial guess of the root makes it impossible to converge using the regular Newton-Rhapson method. - knedlsepp
I marked it as a duplicate so the OP can double check with a verified method that works. I also allude to ensuring that the initial guess needs to be proper or it won't work! - rayryeng
@Prolixus: Is it possible this is a trick question by your instructor? For me the iteration will only converge if the initial value is somewhere in the interval of [2.82, 3.18] - knedlsepp
Your code is trying to solve tanh(x^2-9) but you state you are tyring to solve tanh(x^3-9). In either case, it's easier to solve just the polynomial as tanh(x)=0 means that x=0. You're getting NaN's because at some point -2*x*(tanh(x^2-9)^2-1) is equal to zero. - David

1 Answers

1
votes

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