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 
    
有帮助吗?

解决方案

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
    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top