Numerical analysis: secant method. What am I doing wrong.

I'm trying to solve a problem regarding the application of the secant numerical method.

My MATLAB code is the following

function [f]= fsecante(t)
R=24.7;
L=2.74;
C=0.000251;
P1=-0.5*(R/L)*t;
P2=t*sqrt(1/(L*C)-(R^2)/(4*L^2));
f=2*exp(P1).*cos(P2)-1;
end

x0=0;
x1=10^-4;
wanted=10^-8;
f0=fsecante(x0);
f1=fsecante(x1);
iter=0;
error=wanted;

while(erro>=wanted)
F=(x1-x0)/(f1-f0);
xn=x1-F*f1
error=abs(F*f1);
iter=iter+1;
x0=x1;
x1=xn;
f0=fsecante(x0);
f1=fsecante(x1);
end

I used a calculator to get an idea about the value I should obtain which is 0.152652376 (approximately) However using the method in MATLAB, it converges to 1.4204 which is way over what we should get. What am I doing wrong? My guess is that I have my error variable wrong in the cycle? I also find strange that my solution goes of the set [0,1] where the solution should be. Can someone give me some clarification about what am I missing?

• Is there a reason that you do not want to use the fsolve command? – LutzL Dec 12 '18 at 10:25 