0

Hello I have written this to determine a root using Newton's method. The algorithm works. I also tried to implement an Experimental order of convergence EOC. It also works but I get the result that the order of convergence for Newton's method is 1 when in fact it is 2.

        function [x,y,eoc,k]=newnew(f,df,x0,xe,eps,kmax)
          x = x0;
          y = feval(f,x);
          for m=1:kmax
            z = -y/feval(df,x);
            x = x + z;
            y = feval(f,x);
            k = m;
            for n=m
              Ek=abs(x-xe);
            end
            for n=m+1
              Ekp=abs(x-xe);
            end
            eoc=log(Ek)/log(Ekp);
            if abs(y)<eps
              return
            end
          end
          disp('no convergence');
        end   

what is wrong?

wrongu
  • 560
  • 3
  • 13
WaitForIt
  • 131
  • 1
  • 10

1 Answers1

1

When you say Ek=abs(x-xe) and Exp=abs(x-xe), they are exactly the same thing! That's why eoc evaluates to 1 every time.

Notice that you have no n in those equations. In fact, you don't need those extra for n=m loops either. Inside the for m=1:kmax loop, m is a single value not an array.

eoc needs to be calculated by comparing the previous loop iteration to the current one (since it doesn't make much sense to compare to a future loop iteration which hasn't happened yet). Because this looks like homework, I won't give you any code.. but this is a very strong hint.

wrongu
  • 560
  • 3
  • 13
  • should I use some kind of loop to compare those iterations? And I don't really know how to do it since like you said I have no n or m in those equations. – WaitForIt May 03 '14 at 13:46
  • your outer `for m=1:kmax` loop is the only loop you need. Let's say you make a variable called `Ek_previous` and one called `Ek_current`. Then `eoc` is calculated by comparing these, and you update `Ek_previous` at the start (or end) of each loop. – wrongu May 03 '14 at 13:51
  • yeah but how to differentitate between those 2. That is really my big problem. That is why I tried with those for n=m or m+1 loops. Or maybe I should start all over and define the Newton method as a classic recursion. – WaitForIt May 03 '14 at 13:57
  • could you please post the code. I still haven't figured it out. – WaitForIt May 04 '14 at 07:40
  • As a first more intuitive solution, store the Ek values in an array where the index is the loop counter m. – Lutz Lehmann May 04 '14 at 08:56
  • That's a much better idea, Lutzl! – wrongu May 04 '14 at 14:59