Root Finding using Secant method

718 Views Asked by At

I am currently trying to write a matlab code that will use the secant method to find a root of a function f(x) while only using the initial guess Xr = 1.0 and Del = 10^-6. I have solved the problem analytically and got a final root after a few iterations but I do not know how to translate that into Matlab while keeping the output in a table that displays the number of iterations, root estimate (Xi), func value @ root estimate (f(Xi)) and the approx error that comes with this method. I would appreciate any help that will guide me to a final solution.

1

There are 1 best solutions below

10
On

Suppose you're given a function secStep that runs a secant iteration step for your particular problem with the function f. Suppose you're going to do nSteps iterations and your initial guess is x0. Then you can do the following:

table=zeros(nSteps+1,3);
table(1,1)=0;
table(1,2)=x0;
table(1,3)=f(x0);
for k=2:(nSteps+1)
    table(k,1)=k-1;
    xk=secStep(table(k-1,2));
    table(k,2)=xk;
    table(k,3)=f(xk);
end

This doesn't include an error estimate in the table; how exactly you would do that depends on your exact problem. There isn't a universal way to do it, unlike with, say, bisection.

You can modify this slightly to use a while loop if you are not performing a preset number of iterations.