system of 5 eq and 5 uknowns, FSOLVE error

2 views (last 30 days)
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

Star Strider
Star Strider on 15 Apr 2019
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
gorilla3
gorilla3 on 16 Apr 2019
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!
Star Strider
Star Strider on 16 Apr 2019
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)

Categories

Find more on Variables 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!