Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Newton's Method Matlab Code

  1. Oct 30, 2012 #1
    I've been using this for a Newton Approximation in Matlab

    function x = Newton(f, fp, x, nmax, e)

    % f is an inline function which we apply Newton's method on
    % fp is an inline function that is the derivative of function f
    % x is the initial guess of the root
    % nmax is the total number of iterations done
    % e is the error used to control convergence

    fprintf('x(0) = %10g \n', x)
    for n = 1:nmax
    d = f(x)/fp(x);
    x = x - d;
    fprintf('x(%i) = %10g \n', n, x)
    if abs(d) < e
    fprintf('Converged! \n')

    with this to run it:
    %declare our function f
    f = inline('e^x+2^(-1*x)+2*cos(x)-6');

    % declare the derivative of function f
    fp = inline('e^x-ln(2)*2^(-1*x)-2*sin(x)');

    % declare total number of iterations to be undertaken
    nmax = 100;

    % declare value of initial starting point
    x = 1.0;

    % declare amount of error allowed
    e = 10.0e-5;

    % carry out iteration using function above
    x = newton2(f,fp,x,nmax,e);

    This isn't working or running for this function, but has run fine for every other function. I'm not sure what isn't working about it. I keep getting the error:

    Error using inline/subsref (line 13)
    Not enough inputs to inline function.

    Error in newton2 (line 11)
    d = f(x)/fp(x);

    But I am very new to Matlab, and have no idea what this means.
  2. jcsd
  3. Oct 30, 2012 #2
    Could it be that, in the derivative, "ln" is not the function name for log(e) in Matlab? Try replacing it with "log".
  4. Oct 31, 2012 #3
    That didn't work. I am still getting the same error.
  5. Oct 31, 2012 #4
    OK, is 'e' a pre-defined constant in Matlab? (I can't see it in the function list, wheareas pi, i and j are listed and there's no reference to it under the definition of exp)
  6. Oct 31, 2012 #5


    User Avatar
    Homework Helper

    I think NemoReally hit the problem. The variable 'e' is used in two different contexts that conflict.
  7. Oct 31, 2012 #6
    Error using inlineeval (line 15)
    Error in inline expression ==> 2.71828^x-ln(2)*2^(-1*x)-2*sin(x)
    Undefined function 'ln' for input arguments of type 'double'.

    Error in inline/subsref (line 24)
    INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

    Error in newton2 (line 11)
    d = f(x)/fp(x);

    I got this error when I replaced e with 2.71828, which I thought would have gotten rid of the problem? I'm sorry, I really have no idea what I'm doing matlab wise and my teacher isn't any help.
  8. Oct 31, 2012 #7
    I don't think 'ln' is a valid Matlab function name - try replacing it with log again.
  9. Oct 31, 2012 #8
    Oh, and replace 'e' with 'exp(1)' for greater precision.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook