%secant method of root finding
function [r,fr,logre]=secantV2(fh,xnm1,xn)
steps=0; maxsteps=20; re=1e-8; myrel=1;
fnm1=fh(xnm1);
fn=fh(xn);
if abs(fn)>abs(fnm1)
        temp=xn; xn=xnm1; xnm1=temp;
        temp=fn; fn=fnm1; fnm1=temp;
    end;
while myrel>re && steps<maxsteps
    dfdx=(fn-fnm1)/(xn-xnm1);
    xnp1=xn-fn/dfdx;
    fnp1=fh(xnp1);
    if abs(fnp1)<abs(fn)
        xnm1=xn; fnm1=fn;
        xn=xnp1; fn=fnp1;
    else
        xnm1=xnp1; fnm1=fnp1;
    end;
    steps=steps+1;
    myrel=abs((xn-xnm1)/xn);
    logre(steps)=log10(myrel);
end;
r=xn;
fr=fn;
end