How must I declare a function handle that refers to a function you've written? What syntax is necessary?
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Kevin Holst
2012년 2월 7일
0 개 추천
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
Daniel
2012년 2월 7일
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
2012년 2월 7일
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!