Results 1 to 2 of 2

Math Help - Help with Newton's Method in Matlab

  1. #1
    Newbie
    Joined
    Feb 2009
    Posts
    1

    Help with Newton's Method in Matlab

    Hi, I really need some help setting up a matlab program to solve this problem.

    Using MATLAB, calculate and plot the real root of function : f(x) = xtanh(x) + C = 0, for C=1.5, 2.5, 3.5

    I understand the basics of the Newton Method, but keep getting errors when I run this code, and don't have enough of an understanding of whats going on to correct it. This is the code I have so far:

    c=1.5
    n=0;
    x=.1;
    f=x*tanh(x)-c;
    fder=tanh(x)+(x*sech(x)^2);
    while n<100
    newx= x - (f(x)/fder(x));
    n=n+1
    x=newx
    end
    x

    If someone could give me any tips or help I would really appreciate it. Thanks in advance for your help.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by ajmilano View Post
    Hi, I really need some help setting up a matlab program to solve this problem.

    Using MATLAB, calculate and plot the real root of function : f(x) = xtanh(x) + C = 0, for C=1.5, 2.5, 3.5

    I understand the basics of the Newton Method, but keep getting errors when I run this code, and don't have enough of an understanding of whats going on to correct it. This is the code I have so far:

    c=1.5
    n=0;
    x=.1;
    f=x*tanh(x)-c;
    fder=tanh(x)+(x*sech(x)^2);
    while n<100
    newx= x - (f(x)/fder(x));
    n=n+1
    x=newx
    end
    x

    If someone could give me any tips or help I would really appreciate it. Thanks in advance for your help.
    Please in future give a description of the kind of error you are getting, and any error messages.

    f and fder are not functions the way you have written this.

    To make them anonymous functions try:

    Code:
    c=1.5
    n=0;
    x=.1;
    f=@(x) (x*tanh(x)-c);
    fder=@(x) (tanh(x)+(x*sech(x)^2));
    while n<100
      newx= x - (f(x)/fder(x));
      n=n+1
      x=newx
    end
    x
    Also the loop structure you are using is not ideal, as this should only need ~5 itterations to converge, you need to compare itterates and when the change is less than some threshold exit the loop.

    I would also suggest you start further from 0 than 0.1, 1 should be better.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Matlab- Newton's method
    Posted in the Math Software Forum
    Replies: 4
    Last Post: March 31st 2010, 07:50 PM
  2. Newton's Method
    Posted in the Calculus Forum
    Replies: 5
    Last Post: December 21st 2009, 03:48 PM
  3. Matlab Newton's method help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: November 10th 2009, 04:22 PM
  4. Matlab: Newton-Raphson Method
    Posted in the Math Software Forum
    Replies: 5
    Last Post: September 12th 2009, 03:34 PM
  5. Newton Method, to solve nonlinear equations, in Matlab
    Posted in the Math Software Forum
    Replies: 4
    Last Post: February 18th 2009, 02:27 PM

Search Tags


/mathhelpforum @mathhelpforum