Not enough input argument frenzy.

Hi, I have two simple functions in two files. in in opt_compute_posterior.m,
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
And in plJoint.m,
function joint = plJoint(tobs)
gamma = 2.43;
joint = @(theta)( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) );
end
When I test this code by typing "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.

1 comentario

KSSV
KSSV el 3 de Oct. de 2016
joint = @(theta)( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) );
The above line from the function plJoint(), needs theta. You have to provide this value.

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 3 de Oct. de 2016
Editada: Stephen23 el 3 de Oct. de 2016
When you type
opt_compute_posterior(plJoint, 0, 300, 1000)
you are calling plJoint, but with no input arguments, thus the error. It is just as if you type this, and do not define what tobs is, do you expect this to work without error?:
plJoint() % <- no tobs
...
joint = @(theta)( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) ); % <- what is tobs?

13 comentarios

Woongki
Woongki el 3 de Oct. de 2016
I thought joint(thetavals) will return a function handle since plJoint returns a function handle. If it's wrong, how can I properly pass plJoint into opt_compute_posterior function?
Stephen23
Stephen23 el 3 de Oct. de 2016
Editada: Stephen23 el 3 de Oct. de 2016
The concept of passing a function handle is fine. The problem is where its variables are defined.
You create the function handle inside plJoint, but at the moment it is created the variable tobs does not exist (because you call plJoint without any input argument). You can either define the anonymous function to:
  1. use existing variables in the local workspace where it is created, or
  2. use input variables defined within the parentheses.
tobs is neither of these, thus the error. You will solve your problem when you can answer this question: where is the value of tobs specified?
Woongki
Woongki el 3 de Oct. de 2016
hmmm OK. sorry for the confusion. What I expected was more like a closure in JS or Python. By typing opt_compute_posterior(plJoint, 0, 300, 1000), what I expected was that, when it reached "postvals = joint(thetavals);", it would pass "thetavals" into plJoint, and the anonymous function would calculate and return ( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) ) with the thetavals. Am I thinking wrong?
You just need
opt_compute_posterior(@plJoint, 0, 300, 1000)
so that you are not calling p1Joint, that instead you take a handle to it and pass the handle into the routine.
Stephen23
Stephen23 el 3 de Oct. de 2016
Editada: Stephen23 el 3 de Oct. de 2016
@Woongki: It will pass thetavals, exactly as you expect.
But you don't specify tobs and that is the problem. That is what my answer and comments talk about. I repeat: "You will solve your problem when you can answer this question: where is the value of tobs specified?"
Woongki
Woongki el 3 de Oct. de 2016
Then, I get a error "Undefined function 'sum' for input arguments of type 'function_handle'." Should I change the code?
Stephen23
Stephen23 el 3 de Oct. de 2016
Editada: Stephen23 el 3 de Oct. de 2016
@Walter Roberson: please check again, a bit more carefully. The function plJoint already returns a function handle. The concept is fine, just the variables need attention.
Stephen23
Stephen23 el 3 de Oct. de 2016
Editada: Stephen23 el 3 de Oct. de 2016
@Wonngki: yes, you need to change the code. However Walter Roberson's suggestion is not helpful because it does not resolve your problem and just makes this discussion more complicated.
I repeat: you need to think about the variable tobs: where is its value defined?
Woongki
Woongki el 3 de Oct. de 2016
Editada: Woongki el 3 de Oct. de 2016
@Stephen Cobeldick I thought tobs was specified in the local scope of plJoint, so it would be accessible when the anonymous function was called, just like in the case of closures in other language. If not, can you make examples of how I can do 1 and 2 in your comment?
Stephen23
Stephen23 el 3 de Oct. de 2016
Editada: Stephen23 el 3 de Oct. de 2016
@Woongki: you define tobs in the list of input augments, but you never call plJoint with any input arguments, so tobs will never be defined in the workspace. So all you need to do is specify tobs when you call plJoint:
>> opt_compute_posterior(plJoint(1), 0, 300, 1000)
ans =
Columns 1 through 7
0 0.3003 0.6006 0.9009 1.2012 1.5015 1.8018
Columns 8 through 14
2.1021 2.4024 2.7027 3.003 3.3033 3.6036
...etc
This why I was telling to to think about where the value of tobs is defined. You simply never defined its value.
"I thought tobs was specified in the local scope of plJoint"
Yes, of the local workspace when the anonymous function is created, not when it is called. As I already explained, and as the documentation clearly states (I gave you a link).
"so it would be accessible when the anonymous function was called"
Nope. See the link I gave.
"just like in the case of closures in other language"
MATLAB is not other languages. Try reading the MATLAB documentation (see the link I gave)
Woongki
Woongki el 3 de Oct. de 2016
Ohhhhhh, now I see the confusion. I was confused by theta and tobs, and now I see why you talked about tobs. Many thanks!!!
Stephen23
Stephen23 el 3 de Oct. de 2016
@Woongki: I am glad to help. Please accept my answer if it helped you to resolve your question.
Woongki
Woongki el 3 de Oct. de 2016
Love to you!

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 3 de Oct. de 2016

Comentada:

el 3 de Oct. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by