Secant Method for Smallest Positive Root

1 visualizzazione (ultimi 30 giorni)
cee878
cee878 il 2 Mar 2016
Risposto: Walter Roberson il 2 Mar 2016
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

Risposte (1)

Walter Roberson
Walter Roberson il 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.

Community Treasure Hunt

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

Start Hunting!

Translated by