Secant Method for Smallest Positive Root

1 visualización (últimos 30 días)
cee878
cee878 el 2 de Mzo. de 2016
Respondida: Walter Roberson el 2 de Mzo. de 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

Respuestas (1)

Walter Roberson
Walter Roberson el 2 de Mzo. de 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.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by