当我将一个函数(我们称之为f
)传递给我的 Base 函数时,Base 函数无法识别该f
函数,而不使用''
引号,代码如下:
function y = test(a, b, n ,f)
if ( rem(n,2) ~= 0 )
error ( 'n is not even' )
end
% create a vector of n+1 linearly spaced numbers from a to b
x = linspace ( a, b, n+1 );
for i = 1:n+1
% store each result at index "i" in X vector
X(i) = feval ( f, x(i) );
end
y=sum(X);
end
这是f.m
:
function [y] = f (x)
y = 6-6*x^5;
当我从带有引号的命令行运行时:
>> [y] = test(0,1,10,'f')
y =
52.7505
但是当我删除它们时:
>> [y] = test(0,1,10,f)
Error using f (line 2)
Not enough input arguments.
我的错误在哪里?为什么我不能执行[y] = test(0,1,10,f)
?
谢谢