2016-10-03 46 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

相关问题