How to fixe this error with fsolve ?
Show older comments
Hello everyone.
I am having trouble to solve a system of two nonlinear equations of two variables with the fsolve command. Here is how I proceed:
- I define a function of 5 variables:
function y = eq2(Par,kl,kf,ml,okt,omt) % Par is structure that contains parameters (constants) of my function
... % lines of instructions
end
The function eq2 works well. For example:
eq2(Par,.5,1,0.1,0.3,0.5)
% gives
ans =
0.0011 -0.9532
- Then I fixe the value of 3 variables and define the function of the two remaining variables:
fun = @(kl,ml) eq2(Par,kl,1,ml,0.3,0.5);
The function fun works as well. For example:
fun(0.5,0.1)
% gives
ans =
0.0011 -0.9532
% same answer as before, as expected
- Then I want to solve the equation fun(kl,ml)=(0,0):
x0 = [0.1,0.1];
solve(fun,x0)
which does not work. I get the error message:
Error using sym.getEqnsVars>checkVariables (line 92)
Second argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 56)
checkVariables(vars);
Error in solve>getEqns (line 429)
[eqns, vars] = sym.getEqnsVars(argv{:});
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
I don't understand the error message, and I don't know what is wrong in my code. If the problem were only that solution does not exist the message should be different.
Can someone help me please ? Thank you in advance.
4 Comments
Star Strider
on 1 Oct 2020
Idossou Marius Adom posted —
Sorry. I did not put the Par structure and the function because it is heavy. Here they are:
Parameters
% Par structure
Par.alpha1 = 0.35/3;
Par.alpha2 = 0.35*2/3;
Par.alpha3 = 0.6;
Par.psik = 1/2;
Par.Ak = 1;
Par.Bk = 1.1;
Par.sigmak = 2;
Par.psim = 1/2;
Par.Am = 1;
Par.Bm = 1.1;
Par.sigmam = 2;
Par.rhok = 1-1/Par.sigmak;
Par.rhom = 1-1/Par.sigmam;
Par.delta = 0.1/55;
Par.R = 1 + 0.055/52;
Par.beta = 1/Par.R;
Par.theta = 0.479179;
Par.w = 2;
Par.Gamma = (1-Par.alpha3)*(Par.alpha3/Par.w)^(Par.alpha3/(1-Par.alpha3));
Par.alpha1t = Par.alpha1/(1-Par.alpha3);
Par.alpha2t = Par.alpha2/(1-Par.alpha3);
Par.alpha3t = Par.alpha3/(1-Par.alpha3);
Function:
function y = eq2(Par,kl,kf,ml,okt,omt)
%
y(1) = -1 + Par.beta*(1-Par.delta) + Par.beta*Par.alpha1t*Par.Gamma*Par.psik*Par.Ak^Par.rhok*kl.^(Par.rhok-1).* ...
( ...
Par.theta*(Par.psim*(Par.Am*ml).^Par.rhom + (1-Par.psim)*(Par.Bm*omt).^Par.rhom).^(Par.alpha2t/Par.rhom).*...
(Par.psik*(Par.Ak*kl).^Par.rhok + (1-Par.psik)*(Par.Bk*(kf+okt)).^Par.rhok).^(Par.alpha1t/Par.rhok-1) + ...
(1-Par.theta)*(Par.psim*(Par.Am*ml).^Par.rhom).^(Par.alpha2t/Par.rhom).*...
(Par.psik*(Par.Ak*kl).^Par.rhok + (1-Par.psik)*(Par.Bk*kf).^Par.rhok).^(Par.alpha1t/Par.rhok-1)...
);
%
y(2) = -1 + Par.beta*Par.alpha2t*Par.Gamma*Par.psim*Par.Am^Par.rhom*ml.^(Par.rhom-1).* ...
( ...
Par.theta*(Par.psim*(Par.Am*ml).^Par.rhom + (1-Par.psim)*(Par.Bm*omt).^Par.rhom).^(Par.alpha2t/Par.rhom-1).*...
(Par.psik*(Par.Ak*kl).^Par.rhok + (1-Par.psik)*(Par.Bk*(kf+okt)).^Par.rhok).^(Par.alpha1t/Par.rhok) + ...
(1-Par.theta)*(Par.psim*(Par.Am*ml).^Par.rhom).^(Par.alpha2t/Par.rhom-1).*...
(Par.psik*(Par.Ak*kl).^Par.rhok + (1-Par.psik)*(Par.Bk*kf).^Par.rhok).^(Par.alpha1t/Par.rhok)...
);
%
end
Other lines of code:
eq2(Par,.5,1,0.1,0.3,0.5)
fun = @(kl,ml) eq2(Par,kl,1,ml,0.3,0.5);
fun(0.5,0.1)
x0 = [0.1,0.1];
solve(fun,x0)
Thank you.
J. Alex Lee
on 2 Oct 2020
you are invoking "solve", rather than "fsolve"...
VBBV
on 2 Oct 2020
Use fsolve as
% if true
% code
% end
options = optimoptions('fsolve','Display','iter');
[x fval] = fsolve(fun,x0,options)
Idossou Marius Adom
on 2 Oct 2020
Answers (0)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!