2

I am new to MATLAB I have implemented the following code of the Steffensen's method but with out using the implementation of the Aitken's delta-squared process:

%Test of Steffesen's Method

function st(x0)

    tol=10^-6; % tolerance
    itmax=1000; % max number of iterations
    itnum=0; % iterations counter


    disp([itnum,x0])
    x1=(f1(x0+f1(x0))-f1(x0))/f1(x0);
    itnum=itnum+1;
    disp([itnum,x1,abs((x0-x1)/x0)])

    while abs((x0-x1)/x0)>tol && itnum<itmax
        x0=x1;
        x1=(f1(x0+f1(x0))-f1(x0))/f1(x0);
        itnum=itnum+1;
        disp([itnum,x1,abs((x0-x1)/x0)])
    end

end


function y=f1(x)
y=x^3+x-3;
end
function y=f2(x)
    y=x-tan(x);
end 

The thing is that when I want to test it with $f1$ and $x0=1$ it doesn't converge. I get the same problem when I run it with $x0=2$, and I know that the root is in $1.302775637720899$. Can someone help me to fix this mistake please?

Edition

%Test of Steffesen's Method

function st(x0)

    tol=10^-6; % tolerance
    itmax=1000; % max number of iterations
    itnum=0; % iterations counter


    disp([itnum,x0])
    x1=x0-f1(x0)/((f1(x0+f1(x0))-f1(x0))/f1(x0));
    itnum=itnum+1;
    disp([itnum,x1,abs((x0-x1)/x0)])

    while abs((x0-x1)/x0)>tol && itnum<itmax
        x0=x1;
        x1=x0-f1(x0)/((f1(x0+f1(x0))-f1(x0))/f1(x0));
        itnum=itnum+1;
        disp([itnum,x1,abs((x0-x1)/x0)])
    end

end


function y=f1(x)
y=x^3+x-3;
end
function y=f2(x)
    y=x-tan(x);
end

and it gives me

st(1) 0 1

Columns 1 through 2

 1.000000000000000e+00     1.500000000000000e+00

Column 3

 5.000000000000000e-01

Columns 1 through 2

 2.000000000000000e+00     1.404837430610626e+00

Column 3

 6.344171292624907e-02

Columns 1 through 2

 3.000000000000000e+00     1.316105875037959e+00

Column 3

 6.316144034836793e-02

Columns 1 through 2

 4.000000000000000e+00     1.249192129475165e+00

Column 3

 5.084222085161958e-02

Columns 1 through 2

 5.000000000000000e+00     1.218482688521551e+00

Column 3

 2.458344095276661e-02

Columns 1 through 2

 6.000000000000000e+00     1.213521222822721e+00

Column 3

 4.071839301098271e-03

Columns 1 through 2

 7.000000000000000e+00     1.213411714510119e+00

Column 3

 9.024012975056845e-05

Columns 1 through 2

 8.000000000000000e+00     1.213411662762241e+00

Column 3

 4.264659465520323e-08
user162343
  • 3,245
  • The assignment of x0 and x1 in and before the while loop doesnt seem to follow the recurrence relation in the wiki page yes? Also has it worked in any test cases? – Ismail Bello Sep 20 '15 at 17:38
  • 1
    Yes I have already read wikipedia :), but I don't know where is my mistake :( or why it doesn't converges in any of both cases, l know that with the tangent function should not because the root is not in the interval $[1,2]$ but with the other one should work, let me try with other functions, but I don't know where is my error , Thanks – user162343 Sep 20 '15 at 17:41
  • 1
    You saw that there is a probably working implementation to matlab on the wiki page? – user190080 Sep 20 '15 at 17:43
  • No, what do you mean with that ? :) – user162343 Sep 20 '15 at 17:46
  • 1
    As I see, (and I havent looked at it for long) your assignment of x1 before and in the while loop doesnt follow the recurrence relation in wikipedia. You seem to be caclulating the denominator $g$. You see you are assigning x1 as a (albeit strange) function of x0, wheras in wikipedia, $x_{n+1} = x_n - \frac{f(x_n)}{g(x_n)}$ which isnt replicated in the code in either computation of x1? Those are the errors i see. – Ismail Bello Sep 20 '15 at 17:52
  • jajaja yes I have seen that too, give a minute and I edit my post :), for a verification right? – user162343 Sep 20 '15 at 17:54
  • 1
    I mean that there is the matlab code for the method on the wiki page, of course this doesn't answer your question but could help you to implement it correctly and for cross checking – user190080 Sep 20 '15 at 17:55
  • I have already edited my post I have wrote the recursion correctly this time , I think now it is correct , could you give me your verification please :)? Thanks – user162343 Sep 20 '15 at 17:57
  • 1
    Thats more like it. Converging? Also look at the code in the wiki page. Looks very compact but it'll help with matlab familiarisation :). Also whats f2 doing? Or is it just there for testing purposes? – Ismail Bello Sep 20 '15 at 17:57
  • There is another edition with the results :) – user162343 Sep 20 '15 at 17:59
  • I see. Nice one, try using function handles/anonymous functions as well, makes for cuter code. e.g f1 = @(x_in)(x_in.^3+ x_in.^(-3)); and it does exactly the same as the f1 in your code. You can write g in similar way calling f1 just as you have and it makes (at least for me) easier debugging. seperating the maths from the programming so I know where the mistake is. – Ismail Bello Sep 20 '15 at 18:03
  • 1
    Well what is meaning of Sperating ? Thanks – user162343 Sep 20 '15 at 18:05
  • Is that working? Was expecting to see "iteration number" "latest x1" "absolute error" – Ismail Bello Sep 20 '15 at 18:07
  • http://www.wolframalpha.com/input/?i=x%5E3%2Bx-3 I think it is working, or there is a mistake :( – user162343 Sep 20 '15 at 18:08

0 Answers0