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.

 채택된 답변

José-Luis
José-Luis 2013년 5월 2일

0 개 추천

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

Habib
Habib 2013년 5월 2일
No, nothing prevented me from this simple modification, except I was too tired, I guess. Thanks Jose, solved.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt J
Matt J 2013년 5월 2일
편집: Matt J 2013년 5월 2일

1 개 추천

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
Habib 2013년 5월 2일
Thanks Matt. That works.
Matt J
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
Habib 2013년 5월 2일
sorry, It is my first question here :)
your reply raised another question for me. what does ~ mean as an input or output argument? I have seen it in example gui's and I dont know what it is. and when i ran the gui, Matlab gave error on ~. This is a part of that code:
% --- Executes just before casio is made visible.
function casio_OpeningFcn(hObject, ~ , handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to casio (see VARARGIN)
% Choose default command line output for casio
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes casio wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = casio_OutputFcn(~, ~, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(~, ~, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
Matt J
Matt J 2013년 5월 2일
Habib
Habib 2013년 5월 2일
Thanks. It is introduced since 2009b, and I have 2009a.
Matt J
Matt J 2013년 5월 2일
You're due for an upgrade!

댓글을 달려면 로그인하십시오.

Alan Weiss
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

댓글 수: 1

Habib
Habib 2013년 5월 2일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

제품

태그

질문:

2013년 5월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by