Results 1 to 2 of 2

Math Help - new NR error problem

  1. #1
    Newbie
    Joined
    Nov 2009
    Posts
    2

    new NR error problem

    Code:
    function root =nlenewton(f,df,x0,tol,N)
    %Filename: nlenewton.m
    %Usage: root=nlenewton(f,df,x0,tol,N)
    %(c)H
    %Input
    % f       function f(x) whose root is desired
    % df      derivative of f(x), df=d(f(x))/dx
    % x0      initial approximation
    % tol     tolerence for convergence criterion
    % N       maximum number of iteration 
    %Output
    % root    approximations to the root of f(x)=0
    
    x0=0;
    tol=0.0005;
    N=10;
    f= 786.272-104.488*x+4.48*x^2-0.064*x^3;
    df= -104.488+8.96*x-0.192*x^2;
    
    disp('    i, x(i), f(x(i)), |x(i)-x(i-1)|');
    disp('    --------------------------------');
    
    
    for i=1:N
        if df(x0)==0
            error('Division by zero');
        end
        x=x0-f(x0)/df(x0);
    disp([i,x,f(x),abs(x-x0);
            if (abs(x-x0),tol)||(abs(f(x)),tol)
               root=x;
               break;
            end;
        if i==N
    error('Method not converge after N=%d iterations.',N);
        end
        x0=x;
    end

    but i got error that
    ??? Error: File: nlenewton.m Line: 29 Column: 9
    Illegal use of reserved keyword "if".

    by the way thnx for helping me figure out what the problem before
    Last edited by CaptainBlack; November 11th 2009 at 02:22 AM.
    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 dlugia View Post
    Code:
    function root =nlenewton(f,df,x0,tol,N)
    %Filename: nlenewton.m
    %Usage: root=nlenewton(f,df,x0,tol,N)
    %(c)H
    %Input
    % f       function f(x) whose root is desired
    % df      derivative of f(x), df=d(f(x))/dx
    % x0      initial approximation
    % tol     tolerence for convergence criterion
    % N       maximum number of iteration 
    %Output
    % root    approximations to the root of f(x)=0
     
    x0=0;
    tol=0.0005;
    N=10;
    f= 786.272-104.488*x+4.48*x^2-0.064*x^3;
    df= -104.488+8.96*x-0.192*x^2;
     
    disp('    i, x(i), f(x(i)), |x(i)-x(i-1)|');
    disp('    --------------------------------');
     
     
    for i=1:N
        if df(x0)==0
            error('Division by zero');
        end
        x=x0-f(x0)/df(x0);
    disp([i,x,f(x),abs(x-x0);
            if (abs(x-x0),tol)||(abs(f(x)),tol)
               root=x;
               break;
            end;
        if i==N
    error('Method not converge after N=%d iterations.',N);
        end
        x0=x;
    end

    but i got error that
    ??? Error: File: nlenewton.m Line: 29 Column: 9
    Illegal use of reserved keyword "if".

    by the way thnx for helping me figure out what the problem before
    If the line in question is:

    Code:
            if (abs(x-x0),tol)||(abs(f(x)),tol)
    what do you think this line means?

    Code:
            if (abs(x-x0)<tol)||(abs(f(x))<tol)
    might be better

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. best fit, minimizing point-to-line error, not y error
    Posted in the Advanced Applied Math Forum
    Replies: 1
    Last Post: March 4th 2011, 05:16 AM
  2. Error problem
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 2nd 2010, 03:07 AM
  3. Lagrange error problem
    Posted in the Calculus Forum
    Replies: 2
    Last Post: April 21st 2010, 04:59 PM
  4. Replies: 1
    Last Post: December 10th 2009, 10:48 PM
  5. Replies: 0
    Last Post: April 1st 2009, 04:22 AM

Search Tags


/mathhelpforum @mathhelpforum