0

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.

3
  • What does which opt_compute_posterior return? Commented Oct 3, 2016 at 3:51
  • @hbaderts It returns thetavals and postvals, which are some intervals and Riemann approximation of the joint function Commented Oct 3, 2016 at 3:58
  • As per the error message, you don't have enough input arguments. You need opt_compute_posterior(plJoint(you_need_an_input_here), 0, 300, 1000). Commented Oct 3, 2016 at 4:37

1 Answer 1

2

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, i.e.

opt_compute_posterior(plJoint(0.1), 0, 300, 1000)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your input. I surely want to pass plJoint as a function handle. However, if I type opt_compute_posterior(@plJoint, 0, 300, 1000), "postvals" is assigned to a function handle, not the calculated result of the anonymous function inside plJoint, and thus results error of "Undefined function 'sum' for input arguments of type 'function_handle'". How can I fix this?
Ahhh... I see, you're returning a function handle in the plJoint function. Nevermind my initial answer then; you need to pass plJoint a value for tobs when you call it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.