Filter löschen
Filter löschen

fzero function, additional output needed

6 Ansichten (letzte 30 Tage)
Habib
Habib am 2 Mai 2013
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.

Akzeptierte Antwort

José-Luis
José-Luis am 2 Mai 2013
Is there anything preventing you from modifying find_pw() to return p1 as well?
function [F,p1]=find_pw(p,pw)
Globals are evil.
  1 Kommentar
Habib
Habib am 2 Mai 2013
No, nothing prevented me from this simple modification, except I was too tired, I guess. Thanks Jose, solved.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Matt J
Matt J am 2 Mai 2013
Bearbeitet: Matt J am 2 Mai 2013
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 Kommentare
Habib
Habib am 2 Mai 2013
Thanks. It is introduced since 2009b, and I have 2009a.
Matt J
Matt J am 2 Mai 2013
You're due for an upgrade!

Melden Sie sich an, um zu kommentieren.


Alan Weiss
Alan Weiss am 2 Mai 2013
Make p1 global for the script, too, and it will be available everywhere you need it.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Kommentar
Habib
Habib am 2 Mai 2013
I did that, but sometimes you forget to consider this globalization in you next steps of you project. That can cause future headache for debugging.
I was wondering if there is another solution.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Function Handles finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by