[英]How to pass a function as argument in Matlab
I have this function defined in a m-file: 我在m文件中定义了此功能:
function[it,xvect,xdif,fx]=bisez(a,b,nmax,toll,fun)
it=-1;
xvect=[];
xdif=[];
fx=[];
err=toll+1;
while(it<nmax && err>toll)
x=(b+a)/2;
if(fun(x)==0)
err=0;
else
err=abs(b-a)/2;
end
it=it+1;
xvect=[xvect;x];
xdif=[xdif;err];
fx=[fx:fun(x)];
if(fun(x)*fun(a)>0)
a=x;
else
b=x;
end;
end;
if(it<nmax)
fprintf('Convergence computed at step k:%d\n',it);
else
fprinf('Iteration limit reached: %d\n',it);
end
fprintf('Computed root: %-12.8f\n',xvect(it+1));
return
Then if I try to invoke it with these commands: 然后,如果我尝试使用以下命令调用它:
fun=@(x)exp(x);
a=1;
b=1.5;
nmax=1000;
toll=2;
bisez(a,b,nmax,toll,fun)
I get this error: 我收到此错误:
??? Undefined function or method 'bisez' for input arguments of type 'function_handle'.
What am I missing? 我想念什么?
PS: I'm using Matlab 2007b
PS:我正在使用Matlab 2007b
It appears it is not in your PATH when you run it. 在运行它时,它似乎不在您的PATH中。
If I run it from my PATH I get: 如果从PATH运行它,则会得到:
>> bisez(a,b,nmax,toll,fun)
Convergence computed at step k:0
Computed root: 1.25000000
ans =
0
Outside of my PATH: 在我的路径之外:
>> bisez(a,b,nmax,toll,fun)
Undefined function 'bisez' for input arguments of type 'function_handle'.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.