Secant Method for Smallest Positive Root

3 ビュー (過去 30 日間)
cee878
cee878 2016 年 3 月 2 日
回答済み: Walter Roberson 2016 年 3 月 2 日
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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by