Search code examples
matlabnewtons-method

How to add a Sequence of Estimates to my Newton Raphson Code for matlab?


My code works BUT I need to add 2 more things:

  • output- a vector containing the sequence of estimates including the initial guess x0,
  • input- max iterations

    function [ R, E ] = myNewton( f,df,x0,tol )
        i = 1;
    
        while abs(f(x0)) >= tol
            R(i) = x0;
            E(i) = abs(f(x0));
            i = i+1;
            x0 = x0 - f(x0)/df(x0);
        end
    
        if abs(f(x0)) < tol
            R(i) = x0;
            E(i) = abs(f(x0));
        end
    
    end 
    

Solution

  • well, everything you need is pretty much done already and you should be able to deal with it, btw..

    1. max iteration is contained in the variable i, thus you need to return it; add this

       function [ R, E , i] = myNewton( f,df,x0,tol )
      
    2. Plot sequence of estimates:

      plot(R); %after you call myNewton
      
    3. display max number of iterations

      disp(i); %after you call myNewton