Using fsolve inside an other fsolve

조회 수: 5 (최근 30일)
Vanduy TRAN
Vanduy TRAN 2015년 3월 16일
댓글: Alan Weiss 2015년 3월 16일
Hi everyone,
How can I use the result of each iteration fsolve as the initial guess for other fsolve. My code is like that: The main file
if true
X0 = rho; % Make a starting guess at the solution
options = optimoptions('fsolve','Display','iter'); % Option to display output
[X,fval,exitflag,output] = fsolve(@eqDFT,X0,options); % Call solver
rho=X;
end
m -file for the first fsolve
if true
function F=eqDFT(X)
Y0 = [alpha1 alpha2]; % Make a starting guess at the solution
options = optimoptions('fsolve','TolFun',1.e-12,'Display','iter'); % Option to display output
Y = fsolve(@eqAlpha,Y0,options,X); % Call solver
....
end
and the second fsolve @eqAlpha with Y variable and additionnal parameter X which is defined by rho like that
if true
function G=eqAlpha(Y,rho)
....
end
end
Thank you so much for reading this question and give me some ideas about it.

채택된 답변

Alan Weiss
Alan Weiss 2015년 3월 16일
I am not sure that I understand you. How many times do you want to run fsolve? I suppose that you have a sequence of equations to solve, where each differs from the previous by a little bit, so the solution to the previous equation is a good starting guess for the next.
All you have to do is to use the previous solution x as the x0 input for fsolve. Something like
x0 = rho
for ii = 1:15
x = fsolve(fun,x0);
% change the parameters for fun here
x0 = x; % update x0
end
Alan Weiss
MATLAB mathematical toolbox documentation
  댓글 수: 2
Vanduy TRAN
Vanduy TRAN 2015년 3월 16일
Hi Alan,
I need to solve two non-linear systems A and B by fsolve. A is describeb by
if true
X=fsolve(@fun(X,a),X0)
end
B is describeb by
if true
[a,Y]=fsolve(@func(Y,X),Y0);
end
I want that when i run fsolve to solve A in na iterations, at each iteration, i take X to pass into fsolve of B as a output, and passing back from fsolve of B parameter a into fsolve of A for the next iteration of A. My problem is programed by the following codes.
if true
X0; % initial guess
a;
for i=1:na,
X(i)=fsolve(@fun(X(i),a),X0)
[a,Y]=fsolve(@func(Y,X(i)),Y0);
X0=X(i); %update X0
Y0=Y; % update Y0
end
Do you have any idea?
Thank you so much :)
Alan Weiss
Alan Weiss 2015년 3월 16일
What you are doing looks reasonable in some sense. But some of the details are not clear to me.
When you call
[a,Y]=fsolve(@func(Y,X(i)),Y0);
the returned Y will almost certainly be a vector of zeros, or a vector with very small elements. Is that what you want?
Alan Weiss
MATLAB mathematical toolbox documentation

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by