0

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!

Prolixus
  • 3
  • 4
  • 2
    possible duplicate of [Newton-Raphson Method in Matlab](http://stackoverflow.com/questions/25129233/newton-raphson-method-in-matlab) – rayryeng Dec 15 '14 at 17:33
  • @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 Dec 15 '14 at 20:08
  • 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 Dec 15 '14 at 20:09
  • @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 Dec 15 '14 at 20:10
  • @knedlsepp, this is not a trick question because instructor himself doesn't know how to solve it. I agree with what you wrote, it can converge on that interval only, but I'm curious if there is a way to show at least that, graphically. Also, code is confirmed as correct, I suppose it's numbers that are just not convenient for this method? – Prolixus Dec 15 '14 at 21:32
  • 2
    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 Dec 15 '14 at 21:55
  • Well, there are a lot of possible ways to modify the Newton-Raphson method, so that this will work, but if you state your task is to: "use PURE newton-raphson method, with starting value x=3.2 to find a root", then the answer is: This is impossible. We see this because it simply doesn't work. The reasons for this are simply: 1. The starting value is too far away from the root. 2. The values of the derivative of your function are very close to zero if you are not close enough to your solution, so you get NaN as a result of division by zero. – knedlsepp Dec 15 '14 at 22:01
  • @David x^3 is obviously a typo, I edited it now, my apologies. I understand why it's NaN, and yes, I have to use pure Newton-Raphson. I just wanted to know if there's a way to show it graphically even with these few numbers I got. Out of curiosity, can you explain me the way to solve it with not PURE Newton-Raphson? – Prolixus Dec 15 '14 at 23:41
  • And to make it clear, there is no possible way to show a single iteration graphically, of the code above? – Prolixus Dec 15 '14 at 23:47
  • @Prolixus: It would be nice for you to actually pose a question (i.e. not in the comments)! You just keep asking for more information (even though David provided a perfectly good plot), while you don't even state if you want to know, a.) why it doesn't work b.) how to solve it in a completely different way c.) plot the failing version d.) plot a completely different version. As for now this question is too broad. – knedlsepp Dec 16 '14 at 11:49
  • @knedlsepp I apologize, I've been here since yesterday and don't really know how it works. I thanked David because it really helped a lot and I do understand why it doesn't work. Thanks, everyone. – Prolixus Dec 16 '14 at 12:06

1 Answers1

1

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
David
  • 8,449
  • 1
  • 22
  • 32
  • The function was (x^2-9) as I corrected myself above, but I see what you did here. This is really helpful! Thank you! I'm a beginner so my code is pretty simple. I would also appreciate any other suggestion how to solve this task, if someone thinks of a better way! – Prolixus Dec 15 '14 at 23:54