2016-10-03 123 views
0

我有兩個單獨的文件兩個簡單的功能,象下面這樣:MATLAB:「沒有足夠的輸入參數」錯誤

function [thetavals postvals] = opt_compute_posterior(joint, theta_min, theta_max, num_steps) 
    thetavals = linspace(theta_min, theta_max, num_steps); 
    postvals = joint(thetavals); 
    postvals = postvals/(sum(postvals) .* ((theta_max - theta_min)/num_steps)); 
end 

function joint = plJoint(tobs) 
    gamma = 2.43; 
    joint = @(theta)((1 ./ (theta.^(gamma + 1))) .* (tobs < theta)); 

end 

當我與 opt_compute_posterior(plJoint, 0, 300, 1000)測試此代碼,我有一個「沒有足夠的輸入參數錯誤。 「,而且我找不到代碼出錯的地方。請點亮我的燈。

+0

是什麼'這opt_compute_posterior'返回? – hbaderts

+0

@hbaderts它返回thetavals和postvals,這是一些間隔和聯合函數的Riemann近似 – noclew

+0

根據錯誤消息,您沒有足夠的輸入參數。你需要'opt_compute_posterior(plJoint(you_need_an_input_here),0,300,1000)''。 –

回答

1

它看起來像你試圖通過plJoint作爲一個函數句柄opt_compute_posterior。但是,如果你只寫plJoint,MATLAB會將其解釋爲一個函數調用,並將其視爲寫入plJoint()。爲了表明你想要的功能句柄,你需要的@符號:

opt_compute_posterior(@plJoint, 0, 300, 1000) 

編輯:

看來我走錯了原代碼的意圖。 plJoint已經返回一個函數句柄,並且您打算從命令窗口中調用它。在這種情況下,你需要傳遞給它一個價值tobs當你調用它,即

opt_compute_posterior(plJoint(0.1), 0, 300, 1000) 
+0

感謝您的輸入。我當然想把plJoint作爲一個函數句柄來傳遞。但是,如果我鍵入opt_compute_posterior(@plJoint,0,300,1000),則將「postvals」分配給函數句柄,而不是計算pjoint中匿名函數的結果,從而導致「未定義函數sum」的錯誤爲輸入參數類型'function_handle'「。我怎樣才能解決這個問題? – noclew

+0

啊......我明白了,你正在'plJoint'函數中返回一個函數句柄。那麼,請不要回答我的初步問題。當你調用它時,你需要爲'tobs'傳遞'plJoint'的值。 – KQS

相關問題