How must I declare a function handle that refers to a function you've written? What syntax is necessary?

1 次查看(过去 30 天)
I keep getting the error, "??? Output argument [...] not assigned during call to" my function psi_en whenever I try any of the following commands:
Case 1:
quad_fun = @(E) mass_ab_E(E).*exp(-depth*mass_E(E)*rho_water) ...
.*psi_en(E,spectrum);
max_energy = spectrum(length(spectrum),1);
y = quad(quad_fun,0,max_energy);
Case 2:
quad_fun = @(E) mass_ab_E(E).*exp(-depth*mass_E(E)*rho_water);
max_energy = spectrum(length(spectrum),1);
y = quad(@(E) quad_fun.*psi_en(E,spectrum),0,max_energy);
Case 3:
psi_E = @(E) psi_en(E,spectrum); % error points to both lines as problematic
quad_fun = @(E) mass_ab_E(E).*exp(-depth*mass_E(E)*rho_water).*psi_E(E);
I think it is angry that I'm trying to define a function handle that refers to a function I've written! I can execute the function directly, e.g. "psi_en(0.02,spectrum)" returns "ans = 0.0041", but it takes issue with this function handle declaration. What must I do, and why is it unhappy?!
psi_en.m contains the following; it is intended to assign values to psi_en based on where the quad integration variable E falls within the spectrum histogram (first column is the energy bin, second column is the energy's weighting factor psi_en):
function y = psi_en(en,spect)
if en < 0
error('energy must be positive!')
elseif en == 0
y = 0;
else
if (en > 0) & (en <= spect(1,1))
y = spect(1,2);
end
for i = 1:length(spect)-1
if (en > spect(i,1)) & (en <= spect(i+1,1))
y = spect(i+1,2);
end
end
if en > spect(length(spect))
error('Energy surpasses data!')
end
end

采纳的回答

Walter Roberson
Walter Roberson 2012-2-7
You have flows of control where y is not set, or at least not obviously so.
When you have a function that returns a value, you should make sure that the output value is always defined (except possibly in cases you are going to error() out of.) You could initialize y to inf for example.
  2 个评论
Daniel
Daniel 2012-2-7
My problem seems more than that: I initialized y = 0 before entering the if statements, and now it's giving only 0. It appears not to be using the if statements; that is, it seems I am not properly calling the function. "psi_E = @(E) psi_en(E,spectrum);", "quad_fun = @(E) mass_ab_E(E).*exp(-depth*mass_E(E)*rho_water).*psi_E(E);" and "y = quad(quad_fun,0,max_energy);" -- it is not working as I think it should. Why is it so difficult to use this quad function?!
Walter Roberson
Walter Roberson 2012-2-7
quad passes a vector argument -- your E will be a vector, and that will reach your function psi_en through the argument en. You then do tests suitable for _scalars_ on the _vector_ . When "if" is applied to vector (or array) of values, the test is considered true if and only if _every_ value in the vector (or array) is non-zero -- if *all* of the values meet the condition.
If you are only getting the 0 output, the implication is that none of your "if" statements was true for all elements in the vector.
You need to either switch over to using a loop over the "en" values, or else switch over to using logical indexing (a better approach.)

请先登录,再进行评论。

更多回答(1 个)

Kevin Holst
Kevin Holst 2012-2-7
Is 'spectrum' actually getting passed into your function psi_en? It looks like, if it isn't and 'en' in passed in correctly, 'y' would never be set.
  3 个评论
Kevin Holst
Kevin Holst 2012-2-7
I don't think the problem lies in assigning a function handle to to an expression that includes the function, there's something else at play. Which line of code in your main function is producing the error?
Daniel
Daniel 2012-2-7
It's the lines that involve the function handle. Assigning a value at the beginning to later be overwritten within the if statements solves the problem, as Roberson suggested, but it appears still not to be "working" as I like -- that is, psi_en is then always that value, apparently...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by