MATLAB: Secant Method executing error.

numerical methodsecantmethod

Can anybody help with this error? Thanks.
error occured: Secant (line 1)
[x, y] = Secant(inline('x + exp(-x^2)'), 0, 1, 0.0001, 10)
Here is my codes.
[x, y] = Secant(inline('x + exp(-x^2)'), 0, 1, 0.0001, 10)
% f is an inline function
y(1) = f(a);
y(2) = f(b);
x(1) = a;
x(2) = b;
Dx(1) = 0;
Dx(2) = 0;
disp(' step x(k-1) x(k) x(k+1) y(k+1) Dx(k+1)')
for k = 2:kmax
x(k+1) = x(k) - y(k)*(x(k)-x(k-1))/(y(k)-y(k-1));
y(k+1) = f(x(k+1));
Dx(k+1) = x(k+1)-x(k);
iter = k-1;
out = [ iter, x(k-1), x(k), x(k+1), y(k+1), Dx(k+1) ];
disp( out )
xx = x(k+1);
yy = y(k+1);
if abs(y(k+1)) < tol
disp('secant method has converged'); break;
end
if (iter >= kmax)
disp('zero not found to desired tolerance');
end
end

Best Answer

Your file, Secant.m, is a script: any .m file whose first non-comment is not "function" or "classdef" is considered a script. You cannot pass parameters when you call scripts. However, your very first line of code attempts to call your file Secant.m as if it is a function.
Note that if you somehow succeeded in calling Secant from Secant.m then the first thing the invoked Secant would do would be to call Secant, and the first that that would do would be to call Secant, and so on, until MATLAB eventually notices that you are calling the same routine over and over again and terminates the function. You need to be very careful when you have a file invoke itself!
In your other question, remember how I had you move some code to Newton_test.m and had you have Newton.m itself start with "function" ? You need to do the same kind of thing: you need to separate out your definition of Secant from your call to Secant.
Related Question