简体   繁体   中英

MATLAB: “Not enough input arguments” error

I have two simple functions in two separate files like below:

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

When I test this code with opt_compute_posterior(plJoint, 0, 300, 1000) , I have error of "Not enough input arguments.", and I cannot find where the hell is wrong with the codes. Please lit me a light.

It looks like you are trying to pass plJoint as a function handle to opt_compute_posterior . But if you just write plJoint , MATLAB interprets that as a function call and treats it as if you had written plJoint() . To indicate that you want a function handle, you need the @ symbol:

opt_compute_posterior(@plJoint, 0, 300, 1000)

EDIT:

It seems I had mistaken the intent of the original code. plJoint already returns a function handle, and you did intend to call it from the command window. In that case, you need to pass it a value for tobs when you call it, ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM