Numerical analysis: secant method. What am I doing wrong.
$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?
numerical-methods secant
$endgroup$
add a comment |
$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?
numerical-methods secant
$endgroup$
$begingroup$
Is there a reason that you do not want to use thefsolve
command?
$endgroup$
– LutzL
Dec 12 '18 at 10:25
add a comment |
$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?
numerical-methods secant
$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
numerical-methods secant
asked Dec 12 '18 at 1:09
Granger ObliviateGranger Obliviate
557415
557415
$begingroup$
Is there a reason that you do not want to use thefsolve
command?
$endgroup$
– LutzL
Dec 12 '18 at 10:25
add a comment |
$begingroup$
Is there a reason that you do not want to use thefsolve
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
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Change the initial point
x1 = 1e-3
This is what I got
$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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$begingroup$
Change the initial point
x1 = 1e-3
This is what I got
$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
add a comment |
$begingroup$
Change the initial point
x1 = 1e-3
This is what I got
$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
add a comment |
$begingroup$
Change the initial point
x1 = 1e-3
This is what I got
$endgroup$
Change the initial point
x1 = 1e-3
This is what I got
answered Dec 12 '18 at 1:51
caveraccaverac
14.6k31130
14.6k31130
$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
add a comment |
$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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$begingroup$
Is there a reason that you do not want to use the
fsolve
command?$endgroup$
– LutzL
Dec 12 '18 at 10:25