Help with the Steffensen's method in MATLAB not using Aitken's delta-squared process

1.8k Views Asked by At

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