system of 5 eq and 5 uknowns, FSOLVE error

Hi,
I have an error in the initial condition (B0) estimation but don't know how to solve it. How does one choose the initial parameters of the solution? I only gave it random values... here's the code:
load constants
syms Rsa Vsa P3 q Psa real
e1= Rsab/(Vsa/Vsab)^2 -Rsa;
e2= Csa*(Psa-Pic) -Vsa;
e3= q*Rlv+Pvb-P3;
e4= (Psa-P3)/(Rsv+0.5*Rsa+Rnet) -q;
e5= ABP-q*(Rla+0.5*Rsa)-Psa;
Eqnsfcn = matlabFunction([e1, e2, e3, e4, e5], 'Vars',{[Rsa, Vsa, P3, q, Psa]});
B0 = rand(1,5)*100; %initial parameters
[B,fval] = fsolve(@(b)Eqnsfcn(b(1),b(2),b(3),b(4),b(5)), B0);
Bc = num2cell(B);
[Rsa, Vsa, P3, q, Psa] = Bc{:}
this is the error I get is below. Could you please help me get real solutions of these equations? Thanks!
Error using
symengine>@(in1)[-in1(:,1)+1.0./in1(:,2).^2.*2.145016313856e-4,in1(:,5).*3.7e-8-in1(:,2)-3.7e-7,-in1(:,3)+in1(:,4).*1.8928e6+6.0,-in1(:,4)-(in1(:,3)-in1(:,5))./(in1(:,1).*(1.0./2.0)+9.024393684230502e15),-in1(:,5)-in1(:,4).*(in1(:,1).*(1.0./2.0)+1.352e6)+1.0e2]
Too many input arguments.
Error in eqsystem_O2_15_04_19_v1>@(b)Eqnsfcn(b(1),b(2),b(3),b(4),b(5))
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Error in eqsystem_O2_15_04_19_v1 (line 48)
[B,fval] = fsolve(@(b)Eqnsfcn(b(1),b(2),b(3),b(4),b(5)), B0);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.

 Accepted Answer

There are a few errors in your posted code.
Corrected:
syms Rsa Vsa P3 q Psa Rsab Vsab Csa Pic Rsv Rlv Pvb Rnet ABP Rla real
V = load('constants.mat');
Rsab = V.Rsab;
Vsab = V.Vsab;
Csa = V.Csa;
Pic = V.Pic;
Rsv = V.Rsv;
Rlv = V.Rlv;
Pvb = V.Pvb;
Rnet = V. Rnet;
ABP = V.ABP;
Rla = V.Rla;
e1= Rsab/(Vsa/Vsab)^2 -Rsa;
e2= Csa*(Psa-Pic) -Vsa;
e3= q*Rlv+Pvb-P3;
e4= (Psa-P3)/(Rsv+0.5*Rsa+Rnet) -q;
e5= ABP-q*(Rla+0.5*Rsa)-Psa;
Eqnsfcn = matlabFunction([e1, e2, e3, e4, e5], 'Vars',{[Rsa, Vsa, P3, q, Psa]});
B0 = rand(1,5)*100; %initial parameters
[B,fval] = fsolve(Eqnsfcn, B0);
Bc = num2cell(B);
[Rsa, Vsa, P3, q, Psa] = Bc{:}
The fsolve function requests that you allow it more function evaluations (and probably more iterations as well). You can do that with the optimoptions (link) function.

6 Comments

Thank you, I tried updating it with options but it's still not working. I'm not sure what parameters to change. I tried with different iteration values but I still get the error mentioned above.
Here's the code of what I changed:
options = optimoptions(@fmincon,'Algorithm','sqp','MaxIterations',1500)
B0 = zeros(1,5); %initial parameters
[B,fval] = fsolve(@(b)Eqnsfcn(b(1),b(2),b(3),b(4),b(5)), B0,options);
Bc = num2cell(B);
[Rsa, Vsa, P3, q, Psa] = Bc{:}
I'd be really grateful if you could help me fnd the real solutions to these equations!
You are not calling fsolve correctly with your ‘Eqnsfcn’ function.
Use my code as I posted it, and it should work:
B0 = rand(1,5)*100; %initial parameters
[B,fval] = fsolve(Eqnsfcn, B0);
Bc = num2cell(B);
[Rsa, Vsa, P3, q, Psa] = Bc{:}
Your optimoptions call appears to be correct.
I'm really sorry, I didn't notice that! I works now.
Many thanks for your help (and patience)! :)
As always, my pleasure!
No worries!
Follow -up question:
I changed the parameter ABP from being a constant to a variable:
%ABP=100;
ABP=60:0.1:180;
for i=1:length(ABP)
%...
e5= ABP(i)-q*(Rla+0.5*Rsa)-Psa;
options = optimoptions(@fmincon,'Algorithm','sqp','MaxIterations',1500);
Eqnsfcn = matlabFunction([e1, e2, e3, e4, e5], 'Vars',{[Rsa, Vsa, P3, q, Psa]});
end
So the only thing that changed is e5. I now get the error:
Undefined function 'matlabFunction' for input arguments of type 'cell'.
Error in eqsystem_O2_16_04_19_v1 (line 79)
Eqnsfcn = matlabFunction([e1, e2, e3, e4, e5], 'Vars',{[Rsa, Vsa, P3, q, Psa]});
I'm not sure how to fix it as I don't know how to avoid the cell structure in this case. Could you help me?
Much appreciated!
I am lost.
What do you want to do with ‘ABP’? If you want to change it in every iteration of fsolve, delete it from your syms list, create it as an additional parameter, add it to the argument list, and change it in a loop that evaluates ‘Eqnsfcn’.
First eliminate ‘ABP’ as a defined constant (comment it out), and chnange its name in the symbolic code to prevent later conflicts. Then, add it to the 'Vars' name-value pair as a separate argument (not one you want in the parameter vector), and then call fsolve in a loop.
Try this:
syms Rsa Vsa P3 q Psa Rsab Vsab Csa Pic Rsv Rlv Pvb Rnet ABPs Rla real
V = load('constants.mat');
Rsab = V.Rsab;
Vsab = V.Vsab;
Csa = V.Csa;
Pic = V.Pic;
Rsv = V.Rsv;
Rlv = V.Rlv;
Pvb = V.Pvb;
Rnet = V. Rnet;
% ABP = V.ABP;
Rla = V.Rla;
e1= Rsab/(Vsa/Vsab)^2 -Rsa;
e2= Csa*(Psa-Pic) -Vsa;
e3= q*Rlv+Pvb-P3;
e4= (Psa-P3)/(Rsv+0.5*Rsa+Rnet) -q;
e5= ABPs-q*(Rla+0.5*Rsa)-Psa;
Eqnsfcn = matlabFunction([e1, e2, e3, e4, e5], 'Vars',{[Rsa, Vsa, P3, q, Psa],ABPs});
B0 = rand(1,5); %initial parameters
options = optimoptions(@fsolve, 'MaxFunctionEvaluations',150000, 'MaxIterations',15000);
ABPv = 60:0.1:180;
[Rsa, Vsa, P3, q, Psa] = deal(zeros(1, numel(ABPv))); % Preallocate
for i=1:length(ABPv)
[B,fval(i,:)] = fsolve(@(b)Eqnsfcn(b,ABPv(i)), B0, options);
Rsa(i) = B(1);
Vsa(i) = B(2);
P3(i) = B(3);
q(i) = B(4);
Psa(i) = B(5);
end
That worked when I tested it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!