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












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?










share|cite|improve this question









$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$


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?










share|cite|improve this question









$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












0








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?










share|cite|improve this question









$endgroup$




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?







numerical-methods secant






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Dec 12 '18 at 1:09









Granger ObliviateGranger Obliviate

557415




557415












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


















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
















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




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










1 Answer
1






active

oldest

votes


















0












$begingroup$

Change the initial point



x1 = 1e-3



This is what I got



enter image description here






share|cite|improve this answer









$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





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3036101%2fnumerical-analysis-secant-method-what-am-i-doing-wrong%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0












$begingroup$

Change the initial point



x1 = 1e-3



This is what I got



enter image description here






share|cite|improve this answer









$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
















0












$begingroup$

Change the initial point



x1 = 1e-3



This is what I got



enter image description here






share|cite|improve this answer









$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














0












0








0





$begingroup$

Change the initial point



x1 = 1e-3



This is what I got



enter image description here






share|cite|improve this answer









$endgroup$



Change the initial point



x1 = 1e-3



This is what I got



enter image description here







share|cite|improve this answer












share|cite|improve this answer



share|cite|improve this answer










answered Dec 12 '18 at 1:51









caveraccaverac

14.8k31130




14.8k31130












  • $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


















  • $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
















$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$
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 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




$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


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematics Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3036101%2fnumerical-analysis-secant-method-what-am-i-doing-wrong%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents