當我傳遞一個函數(讓我們稱它爲f
)到我的基地功能,基本功能不承認 的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)
?
由於
嘗試通過@f作爲參數,而不是? – Ansari
你寫的代碼,正如錯誤提示,正試圖評估函數f並將結果傳遞給函數測試。 – Edric