How must I declare a function handle that refers to a function you've written? What syntax is necessary?
2 views (last 30 days)
Show older comments
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
2 Comments
Accepted Answer
Walter Roberson
on 7 Feb 2012
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 Comments
Walter Roberson
on 7 Feb 2012
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.)
More Answers (1)
Kevin Holst
on 7 Feb 2012
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 Comments
Kevin Holst
on 7 Feb 2012
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?
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!