How must I declare a function handle that refers to a function you've written? What syntax is necessary?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Kevin Holst
le 7 Fév 2012
0 votes
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 commentaires
Daniel
le 7 Fév 2012
Kevin Holst
le 7 Fév 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?
Daniel
le 7 Fév 2012
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!