请教一个问题:
如何调用这样的一个m函数,谢谢
bisection(f,a,b)
function p = bisection(f,a,b)
% provide the equation you want to solve with R.H.S = 0 form.
% Write the L.H.S by using inline function
% Give initial guesses.
% Solves it by method of bisection.
% A very simple code. But may come handy
if f(a)*f(b)>0
disp('Wrong choice bro')
else
p = (a + b)/2;
err = abs(f(p));
while err > 1e-7
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
end
end
比如我现在需要的f函数是 y = 3*x^3 + x*x -2
我的做法是在一m文件,或者command windows:
ff=@(x) x.*x+3.*x+2;
p = bisection(ff,-3,4);
运行,提示的出错信息:
Wrong choice bro
Error in bisection (line 9)
if f(a)*f(b)>0
Output argument "p" (and maybe others) not assigned during call to "D:\Program
Files\MATLAB\R2013a\bisection.m>bisection".
Error in f (line 4)
p = bisection(ff,-3,4);