Secant Method for Smallest Positive Root

1 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2016-3-2
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by