0

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 
    
Thilo
  • 8,827
  • 2
  • 35
  • 56
Ajay Kejriwal
  • 41
  • 1
  • 3
  • 7
  • your code is difficult to read to someone not familiar with the program. Avoid using one letter variables `E, R, m, a, b, f` .... – Rasman Nov 19 '12 at 18:18
  • sorry, i should have clarified, E is the final root and R is the number of iterations – Ajay Kejriwal Nov 19 '12 at 18:25
  • Don't you mean that R is the final root, and E is the magnitude of the function at that root? – Dan Becker Nov 19 '12 at 18:50
  • Anyway, doesn't you output vector R already satisfy your first requirement? And won't an if statement in your while loop satisfy your second requirement? You just need to decide what to do once you pass the max iterations. – Dan Becker Nov 19 '12 at 18:51
  • possible duplicate of a [newer question of the user](http://stackoverflow.com/questions/13459784/how-to-show-all-the-midpoints-on-my-bisection-code/13460616#13460616) however that one is clearer, so this should go I think. – Barney Szabolcs Nov 19 '12 at 19:23
  • I'm really quite confused, which of your questions (http://stackoverflow.com/questions/13459784/how-to-show-all-the-midpoints-on-my-bisection-code) is your question? What have you tried to do yourself? – Ben Nov 19 '12 at 19:26

1 Answers1

1

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
    
Acorbe
  • 8,367
  • 5
  • 37
  • 66