0
$\begingroup$

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

%iteradas iniciais%
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?

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

Change the initial point

x1 = 1e-3

This is what I got

enter image description here

$\endgroup$
  • $\begingroup$ Hey! After a few tries I realized that the problem was the initial point. Do you know any easy way to justify why the method does not converge? I mean in an analytical way. I searched about it and only find sufficient conditions of convergence, not necessary ones. $\endgroup$ – Granger Obliviate Dec 12 '18 at 1:58
  • $\begingroup$ @GrangerObliviate Thing with this method is that you kind of have to be close enough to the root for it to work, otherwise it will diverge in a few steps, which I believe was your case. My suggestion is to use something like bisection to get to a reasonable neighborhood of the root and then use the secant, or better yet the tangent itself $\endgroup$ – caverac Dec 12 '18 at 2:03
  • $\begingroup$ @GrangerObliviate : Use one of the anti-stalling variants of regula falsi, this is faster than bisection and while only half as fast as the secant method, it is a bracketing method and thus converges to a root. Dekker's and Brent's methods are almost as fast as the secant method while bracketing a root, but have a more involved implementation. $\endgroup$ – LutzL Dec 12 '18 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.