1. ## mathlab- root finding

Hey im trying to find the secant method.
I am trying to modify my newton's code, but am not getting anywhere.
Here is the code I am using for newton's.

>> format long
>> c=1
c =
1
>> n=0;
>> x=1.5;
>> f=@(x) ((x)^4-c);
>> fder=@(x) (4*(x)^3);
>> while n<100
newx=x-(f(x)/fder(x));
n=n+1
x=newx
end

The secant formula is xn+1=xn-(xn-xn-1)*f(xn)/f(xn)-f(xn-1).
Thus I need two points. Can someone please write out a nice matlab code that I can use (with say two points being 1.5, and 1.3)?

thanks

2. Maybe something like this would work for you:

Code:
function [x err iter]  = mhfSecant(f,x1,x2)
maxerr = 1e-6;
maxiter = 100;
iter = 0;
while abs(x1-x2) > maxerr && iter < maxiter
xnew = x2-(x2-x1)/(f(x2)-f(x1))*f(x2);
x1 = x2;
x2 = xnew;
iter = iter +1;
end
x = (x1+x2)/2;
err = f(x);
Code:
EDU>> f=@(x) ((x)^4-1);
EDU>> [x e i] = mhfSecant(f,1.5,1.3)

x =

1.0000

e =

6.9803e-009

i =

7

EDU>>
Im not very familiar with this method so I cant guarantee this is the right solution.

Regards Elbarto