fzero function, additional output needed
이전 댓글 표시
Hi, I use fzero to find the zero of my function. fezero returns [x, fval]. however, inside the function another parameter is being calculated. I need to return that parameter back to the script that fzero comes from. how can I do that? apparently fzero doesnet return anything but x and fval!
objective function:
function [F]=find_pw(p,pw)
global qglim gamma pdew p1
if pw>= pdew
p1=pdew;
else
F=find_p11(p,pdew,pw);
if F>0
p1=pdew;
else
objectivefunction=@(p1)find_p11(p,p1,pw);
% options = optimset('Tolx',pwlim);
p1=fzero(objectivefunction,[pw,min(p,pdew)]);%,options);
end
end
mpt=mp2(p,pw,p1);
qg=gamma*mpt;
F=qg-qglim;
end
scpirt:
% ....
objectivefunctionpw=@(pw)find_pw(p,pw);
pw=fzero(objectivefunctionpw,[pwlim,pw]);%,options);
% I Need "p1" here
%...
I need the function "find_pw", to return "p1" too. thanks.
채택된 답변
추가 답변 (2개)
You can define find_pw to return a 2nd argument
[F,p1]=find_pw(p,pw)
This will not conflict with FZERO. FZERO only uses the first output argument. After FZERO is finished, call the function an additional time to get the extra parameter
pw=fzero(objectivefunctionpw,[pwlim,pw]);%,options);
[~,p1] = objectivefunctionpw(pw);
There is no point trying to avoid one additional evalulation of the objective function. It's negligible overhead compared to the multiple times it gets evaluated by FZERO.
댓글 수: 7
Habib
2013년 5월 2일
Matt J
2013년 5월 2일
You're welcome, but Flags are not meant for marking a solution you're happy with. Do that by clicking "Accept".
Habib
2013년 5월 2일
Habib
2013년 5월 2일
Matt J
2013년 5월 2일
It means you don't want that input/output assigned.
Habib
2013년 5월 2일
Matt J
2013년 5월 2일
You're due for an upgrade!
Alan Weiss
2013년 5월 2일
0 개 추천
Make p1 global for the script, too, and it will be available everywhere you need it.
Alan Weiss
MATLAB mathematical toolbox documentation
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!