Secant Method for Smallest Positive Root
1 view (last 30 days)
Show older comments
I'm trying to code the secant method for f(x)=e^(-x)-sin(x) to find the smallest positive root. My code seems to be getting an error. I think my logic is fine.
// Initial values and tolerance
x(0) = 2;
x(1) = 10;
f = @(x) exp(-x)-sin(x);
error = 0.001;
%// Different iterations
for k=0:100
x(k+1) = x(k) - (f(x(k)))*((x(k) - x(k-1))/(f(x(k)) - f(x(k-1))));
if abs(x(k)-x(k-1)) < error
return;
end
end
0 Comments
Answers (1)
Walter Roberson
on 2 Mar 2016
Do not name a variable "error". "error" is a key MATLAB facility name.
It is not permitted to index a variable at location 0, so x(0) is illegal.
Your code is not MATLAB code, it is Octave or SciLab code.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!