MATLAB如何向定义的函数里传送函数

如题所述

利用函数句柄,可以传递函数,给你举个例子:
【code】
function y = test( fun, x )
if(~isa(fun,'function_handle')),
fprintf('fun must be a function handle.\n');
return;
end
x = x(:);
y = fun( 3*x+1 );
plot( x, y);

【运行结果】
>> y = test(@sin,0:0.01:pi);

>> fun = @(x) x.^3-2*x.^2-1;
>> y = test(fun,0:0.1:10);

>> fun = 'x.^3-2*x.^2-1';
>> y = test(fun,0:0.1:10);
fun must be a function handle.
Error in ==> test at 2
if(~isa(fun,'function_handle')),

isa是用来判断是否是句柄的,
温馨提示:答案为网友推荐,仅供参考
相似回答