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
>> f=@(x) ((x)^4-c);
>> fder=@(x) (4*(x)^3);
>> while n<100
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)?
Maybe something like this would work for you:
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;
x = (x1+x2)/2;
err = f(x);
Im not very familiar with this method so I cant guarantee this is the right solution.
EDU>> f=@(x) ((x)^4-1);
EDU>> [x e i] = mhfSecant(f,1.5,1.3)