I want to optimize an ODE system of equations with constraints
1 次查看(过去 30 天)
显示 更早的评论
Hi..I have a set of ODEs..I want to find the optimal value of kL and kH with these constraints:
0=<kL<=100
0=<kH<=100
Mass_C4_G-0.2985<0.1
Mass_C6_G-0.6498<0.1
Mass_C8_G-0.6147<0.1
Mass_C10_G-0.43917<0.1
Mass_C12_G-0.40398<0.1
Also all the values of Moles_Ci_G and Moles_Ci_L should be positive.
Can someone help me with the code?
clc
clear
close all
%%Parameters that I want to change
kL=0.1; % k_L_a_G_L_Light, 1st parameter
kH=0.1; % k_L_a_G_L_Heavy, 2nd parameter
%%Constants
Q0=100; % Q_G_in_ml_min
Q1=Q0.*1.66667e-8; % Q_G_in (m3/s)
Q2=0; % Q_G_in_C_G_in_C4
Q3=0; % Q_G_in_C_G_in_C6
Q4=0; % Q_G_in_C_G_in_C8
Q5=0; % Q_G_in_C_G_in_C10
Q6=0; % Q_G_in_C_G_in_C11
Q7=0; % Q_G_in_C_G_in_C12
P1=36e5; % P_Ethylene
T1=230+273.15; % T_Ethylene
T2=230+273.15; % T_ref
R=8.314; % Absolute Gas constant in the Universe
C1=P1/(R*T1); % C_G_in
F0=0.0179; % F_G_out_mmol_s
F1=F0.*1e-3; % F_G_out
VR=300e-6; %Volume of the reactor
VG=250e-6; %Volume of the gas
VL=50e-6; %Volume of the liquid
K1=3.24; % K_L_G_Ethylene
K2=2.23; % K_L_G_Butene
K3=1.72; % K_L_G_Hexene
K4=1.3; % K_L_G_Octene
K5=0.985; % K_L_G_Decene
K6=0.751; % K_L_G_Dodecene
K7=0.842; % K_L_G_Undecane
wc=(0.3+0.25)*1e-3; %Catalyst weight
k10=2.224e-4;
k20=1.533e-4;
k30=3.988e-5;
k40=1.914e-7;
k50=4.328e-5;
k60=2.506e-7;
k70=4.036e-5;
k80=1.062e-6;
k90=6.055e-7;
E1=109.53;
E2=15.229;
E3=7.881;
E4=44.454;
E5=9.438;
E6=8.426;
E7=10.911;
E8=12.537;
E9=7.127;
k1=k10.*exp(-E1.*(1/T1-1/T2)./R);
k2=k20.*exp(-E2.*(1/T1-1/T2)./R);
k3=k30.*exp(-E3.*(1/T1-1/T2)./R);
k4=k40.*exp(-E4.*(1/T1-1/T2)./R);
k5=k50.*exp(-E5.*(1/T1-1/T2)./R);
k6=k60.*exp(-E6.*(1/T1-1/T2)./R);
k7=k70.*exp(-E7.*(1/T1-1/T2)./R);
k8=k80.*exp(-E8.*(1/T1-1/T2)./R);
k9=k90.*exp(-E9.*(1/T1-1/T2)./R);
n1=C1.*VR; %Ethylene initial condition(moles)
n2=10; %Solvent initial condition(moles)
dNdt=@(t,x) [Q1.*C1-F1-VR.*kL.*(x(1)./VG-K1.*x(8)./VL);Q2-F1-VR.*kL.*(x(2)./VG-K2.*(x(9)./VL));Q3-F1-VR.*kL.*(x(3)./VG-K3.*(x(10)./VL));Q4-F1-VR.*kH.*(x(4)./VG-K4.*(x(11)./VL));Q5-F1-VR.*kH.*(x(5)./VG-K5.*x(12)./VL);Q6-F1-VR.*kH.*(x(6)./VG-K6.*x(13)./VL);Q7-F1-VR.*kH.*(x(7)./VG-K7.*x(14)./VL);VR.*kL.*(x(1)./VG-K1.*x(8)./VL)+wc.*(-2.*k1.*(x(8)./VL).^2-k2.*(x(8)./VL).*(x(9)./VL)-k3.*(x(8)./VL).*(x(10)./VL)-k5.*(x(8)./VL).*(x(11)./VL)-k7.*x(8).*x(12)./(VL.^2));VR.*kL.*(x(2)./VG-K2.*x(9)./VL)+wc.*(k1.*x(8).^2./VL.^2-k2.*x(8).*x(9)./VL.^2-2.*k4.*x(9).^2./VL.^2-k6.*x(9).*x(10)./VL.^2-k8.*x(9).*x(11)./VL.^2);VR.*kL.*(x(3)./VG-K3.*x(10)./VL)+wc.*(k2.*x(8).*x(9)./VL.^2-k3.*x(8).*x(10)./VL.^2-k6.*x(9).*x(10)./VL.^2-2.*k9.*x(10).^2./VL.^2);VR.*kH.*(x(4)./VG-K4.*x(11)./VL)+wc.*(k3.*x(8).*x(10)./VL.^2+k4.*x(9).^2./VL.^2-k5.*x(8).*x(11)./VL.^2-k8.*x(9).*x(11)./VL.^2);VR.*kH.*(x(5)./VG-K5.*x(12)./VL)+wc.*(k5.*x(8).*x(11)./VL.^2+k6.*x(9).*x(10)./VL.^2-k7.*x(8).*x(12)./VL.^2);VR.*kH.*(x(6)./VG-K6.*x(13)./VL)+wc.*(k7.*x(8).*x(12)./VL.^2+k8.*x(9).*x(10)./VL.^2+k9.*x(10).^2./VL.^2);VR.*kH.*(x(7)./VG-K7.*x(14)./VL)];
[t,x]=ode45(dNdt,[0,18000],[n1;0;0;0;0;0;0;0;0;0;0;0;0;n2]);
%moles of products
Moles_C2_G=x(end, 1);
Moles_C4_G=x(end,2);
Moles_C6_G=x(end,3);
Moles_C8_G=x(end,4);
Moles_C10_G=x(end,5);
Moles_C12_G=x(end,6);
Moles_C11H24_G=x(end,7);
Moles_C2_L=x(end, 8);
Moles_C4_L=x(end,9);
Moles_C6_L=x(end,10);
Moles_C8_L=x(end,11);
Moles_C10_L=x(end,12);
Moles_C12_L=x(end,13);
Moles_C11H24_L=x(end,14);
%Grams of products
Mass_C2_G=Moles_C2_G.*28;
Mass_C4_G=Moles_C4_G.*56;
Mass_C6_G=Moles_C6_G.*73;
Mass_C8_G=Moles_C8_G.*112;
Mass_C10_G=Moles_C10_G.*154;
Mass_C12_G=Moles_C12_G.*168;
Mass_C11H24_G=Moles_C11H24_G.*156;
Mass_C2_L=Moles_C2_L.*28;
Mass_C4_L=Moles_C4_L.*56;
Mass_C6_L=Moles_C6_L.*73;
Mass_C8_L=Moles_C8_L.*112;
Mass_C10_L=Moles_C10_L.*154;
Mass_C12_L=Moles_C12_L.*168;
Mass_C11H24_L=Moles_C11H24_L.*156;
2 个评论
采纳的回答
Torsten
2024-4-11
编辑:Torsten
2024-4-11
I ran your code with k in the order of 0.1,1 and 10, but it seems the k-values have amost no influence on the result of the integration. Since the concentrations you get are far off the end concentrations you prescribed, "fmincon" tells you that no feasible solution for the k values could be found.
kL = 0.1;
kH = 0.1;
p0 = [kL,kH];
lb = [0;0];
ub = [100;100];
obj = @(p)0;
%[c,ceq] = nonlcon(p0)
sol = fmincon(obj,p0,[],[],[],[],lb,ub,@nonlcon);
function [c,ceq] = nonlcon(p)
%%Parameters that I want to change
kL=p(1); % k_L_a_G_L_Light, 1st parameter
kH=p(2); % k_L_a_G_L_Heavy, 2nd parameter
%%Constants
Q0=100; % Q_G_in_ml_min
Q1=Q0.*1.66667e-8; % Q_G_in (m3/s)
Q2=0; % Q_G_in_C_G_in_C4
Q3=0; % Q_G_in_C_G_in_C6
Q4=0; % Q_G_in_C_G_in_C8
Q5=0; % Q_G_in_C_G_in_C10
Q6=0; % Q_G_in_C_G_in_C11
Q7=0; % Q_G_in_C_G_in_C12
P1=36e5; % P_Ethylene
T1=230+273.15; % T_Ethylene
T2=230+273.15; % T_ref
R=8.314; % Absolute Gas constant in the Universe
C1=P1/(R*T1); % C_G_in
F0=0.0179; % F_G_out_mmol_s
F1=F0.*1e-3; % F_G_out
VR=300e-6; %Volume of the reactor
VG=250e-6; %Volume of the gas
VL=50e-6; %Volume of the liquid
K1=3.24; % K_L_G_Ethylene
K2=2.23; % K_L_G_Butene
K3=1.72; % K_L_G_Hexene
K4=1.3; % K_L_G_Octene
K5=0.985; % K_L_G_Decene
K6=0.751; % K_L_G_Dodecene
K7=0.842; % K_L_G_Undecane
wc=(0.3+0.25)*1e-3; %Catalyst weight
k10=2.224e-4;
k20=1.533e-4;
k30=3.988e-5;
k40=1.914e-7;
k50=4.328e-5;
k60=2.506e-7;
k70=4.036e-5;
k80=1.062e-6;
k90=6.055e-7;
E1=109.53;
E2=15.229;
E3=7.881;
E4=44.454;
E5=9.438;
E6=8.426;
E7=10.911;
E8=12.537;
E9=7.127;
k1=k10.*exp(-E1.*(1/T1-1/T2)./R);
k2=k20.*exp(-E2.*(1/T1-1/T2)./R);
k3=k30.*exp(-E3.*(1/T1-1/T2)./R);
k4=k40.*exp(-E4.*(1/T1-1/T2)./R);
k5=k50.*exp(-E5.*(1/T1-1/T2)./R);
k6=k60.*exp(-E6.*(1/T1-1/T2)./R);
k7=k70.*exp(-E7.*(1/T1-1/T2)./R);
k8=k80.*exp(-E8.*(1/T1-1/T2)./R);
k9=k90.*exp(-E9.*(1/T1-1/T2)./R);
n1=C1.*VR; %Ethylene initial condition(moles)
n2=10; %Solvent initial condition(moles)
dNdt=@(t,x) [Q1.*C1-F1-VR.*kL.*(x(1)./VG-K1.*x(8)./VL);Q2-F1-VR.*kL.*(x(2)./VG-K2.*(x(9)./VL));Q3-F1-VR.*kL.*(x(3)./VG-K3.*(x(10)./VL));Q4-F1-VR.*kH.*(x(4)./VG-K4.*(x(11)./VL));Q5-F1-VR.*kH.*(x(5)./VG-K5.*x(12)./VL);Q6-F1-VR.*kH.*(x(6)./VG-K6.*x(13)./VL);Q7-F1-VR.*kH.*(x(7)./VG-K7.*x(14)./VL);VR.*kL.*(x(1)./VG-K1.*x(8)./VL)+wc.*(-2.*k1.*(x(8)./VL).^2-k2.*(x(8)./VL).*(x(9)./VL)-k3.*(x(8)./VL).*(x(10)./VL)-k5.*(x(8)./VL).*(x(11)./VL)-k7.*x(8).*x(12)./(VL.^2));VR.*kL.*(x(2)./VG-K2.*x(9)./VL)+wc.*(k1.*x(8).^2./VL.^2-k2.*x(8).*x(9)./VL.^2-2.*k4.*x(9).^2./VL.^2-k6.*x(9).*x(10)./VL.^2-k8.*x(9).*x(11)./VL.^2);VR.*kL.*(x(3)./VG-K3.*x(10)./VL)+wc.*(k2.*x(8).*x(9)./VL.^2-k3.*x(8).*x(10)./VL.^2-k6.*x(9).*x(10)./VL.^2-2.*k9.*x(10).^2./VL.^2);VR.*kH.*(x(4)./VG-K4.*x(11)./VL)+wc.*(k3.*x(8).*x(10)./VL.^2+k4.*x(9).^2./VL.^2-k5.*x(8).*x(11)./VL.^2-k8.*x(9).*x(11)./VL.^2);VR.*kH.*(x(5)./VG-K5.*x(12)./VL)+wc.*(k5.*x(8).*x(11)./VL.^2+k6.*x(9).*x(10)./VL.^2-k7.*x(8).*x(12)./VL.^2);VR.*kH.*(x(6)./VG-K6.*x(13)./VL)+wc.*(k7.*x(8).*x(12)./VL.^2+k8.*x(9).*x(10)./VL.^2+k9.*x(10).^2./VL.^2);VR.*kH.*(x(7)./VG-K7.*x(14)./VL)];
[t,x]=ode45(dNdt,[0,18000],[n1;0;0;0;0;0;0;0;0;0;0;0;0;n2]);
%moles of products
Moles_C2_G=x(end, 1);
Moles_C4_G=x(end,2);
Moles_C6_G=x(end,3);
Moles_C8_G=x(end,4);
Moles_C10_G=x(end,5);
Moles_C12_G=x(end,6);
Moles_C11H24_G=x(end,7);
Moles_C2_L=x(end, 8);
Moles_C4_L=x(end,9);
Moles_C6_L=x(end,10);
Moles_C8_L=x(end,11);
Moles_C10_L=x(end,12);
Moles_C12_L=x(end,13);
Moles_C11H24_L=x(end,14);
%Grams of products
Mass_C2_G=Moles_C2_G.*28;
Mass_C4_G=Moles_C4_G.*56;
Mass_C6_G=Moles_C6_G.*73;
Mass_C8_G=Moles_C8_G.*112;
Mass_C10_G=Moles_C10_G.*154;
Mass_C12_G=Moles_C12_G.*168;
Mass_C11H24_G=Moles_C11H24_G.*156;
Mass_C2_L=Moles_C2_L.*28;
Mass_C4_L=Moles_C4_L.*56;
Mass_C6_L=Moles_C6_L.*73;
Mass_C8_L=Moles_C8_L.*112;
Mass_C10_L=Moles_C10_L.*154;
Mass_C12_L=Moles_C12_L.*168;
Mass_C11H24_L=Moles_C11H24_L.*156;
c(1) = Mass_C4_G-0.2985-0.1;
c(2) = -(Mass_C4_G-0.2985)-0.1;
c(3) = Mass_C6_G-0.6498-0.1;
c(4) = -(Mass_C6_G-0.6498)-0.1;
c(5) = Mass_C8_G-0.6147-0.1;
c(6) = -(Mass_C8_G-0.6147)-0.1;
c(7) = Mass_C10_G-0.43917-0.1;
c(8) = -(Mass_C10_G-0.43917)-0.1;
c(9) = Mass_C12_G-0.40398-0.1;
c(10) = -(Mass_C12_G-0.40398)-0.1;
ceq = [];
end
7 个评论
更多回答(1 个)
William Rose
2024-4-11
You stated goals such as
Mass_C4_G-0.2985<0.1
I assume you really would like
abs(Mass_C4_G-0.2985)<0.
and likewise for C6, C8, C10, C12.
I put your code into a while loop. The loop runs until conditions 1 and 2 are met, or after 50 passes, whichever comes first. Inside each loop pass, the following happens:
- Make guesses for 0.1<kL<100 and 0.1<kH<100.
- Run simulation.
- Check condition 1: all mole quantities are positive
- Check condition 2: five inequality constraints are met: abs(Mass...)<0.1.
- Compute massError=sum of absolute deviations of 5 masses from their target values
Results: All 50 guesses for (kL,kH) satisfied condition 1. None of the 50 guesses satisfied condition 2. The histogram of massError, for the 50 trials, is below. The histogram shows that the massError was between 428.4 and 428.75, for all the trials, and was between 428.7 and 428.75 for 27 out of 50 trials. In other words, the massError did not vary much, despite the wide range of random guesses for kL and kH.
So I aded code to save the five individual mass errors on each pass, to determine which mass(es) are causing the (total) massError to be large. Then I re-ran the code with 100 passes, instead of 50. This set of 100 random (kL,kH) pairs is independent of the previous 50 pairs. See attached code.
Results: All 100 guesses for (kL,kH) satisfied condition 1. None of the 100 guesses satisfied condition 2.
I displayed the minimum, mean, and maximum error for all five species: C4_G, C6_G, C8_G, C10_G, C12_G, with this command
disp([min(results(:,6:10));mean(results(:,6:10));max(results(:,6:10))]);
1.6875 5.6660 5.3837 5.6904 410.0246
1.6935 5.6738 5.3963 5.7076 410.2164
1.6946 5.6753 5.3995 5.7110 410.2561
As you can see from the printout above, the errors for the individual species did not change much, and the mass error for C12_G was the largest, by far. This makes me pessimistic that you will find values for kL and kH that satisfy your constraints. Also interesting is that fact that all individual mass errors are positive.
Next steps I recommend: Put the simulation inside a function whose output is massError. Use fmincon() to find the values of kL and kH that minimize massError, within the allowed range for kL and kH. See if the individual constraints on the 5 masses are met at this minimum.
clear
c1=false; % condition 1: all masses>0 at end
c2=false; % condition 2: five masses within 0.1 of target values
i=0; % pass counter
while ~(c1 & c2) & i<20
%%Parameters that I want to change
kL=10^(-1+3*rand()); % k_L_a_G_L_Light, 1st parameter
kH=10^(-1+3*rand()); % k_L_a_G_L_Heavy, 2nd parameter
%%Constants
Q0=100; % Q_G_in_ml_min
Q1=Q0.*1.66667e-8; % Q_G_in (m3/s)
Q2=0; % Q_G_in_C_G_in_C4
Q3=0; % Q_G_in_C_G_in_C6
Q4=0; % Q_G_in_C_G_in_C8
Q5=0; % Q_G_in_C_G_in_C10
Q6=0; % Q_G_in_C_G_in_C11
Q7=0; % Q_G_in_C_G_in_C12
P1=36e5; % P_Ethylene
T1=230+273.15; % T_Ethylene
T2=230+273.15; % T_ref
R=8.314; % Absolute Gas constant in the Universe
C1=P1/(R*T1); % C_G_in
F0=0.0179; % F_G_out_mmol_s
F1=F0.*1e-3; % F_G_out
VR=300e-6; %Volume of the reactor
VG=250e-6; %Volume of the gas
VL=50e-6; %Volume of the liquid
K1=3.24; % K_L_G_Ethylene
K2=2.23; % K_L_G_Butene
K3=1.72; % K_L_G_Hexene
K4=1.3; % K_L_G_Octene
K5=0.985; % K_L_G_Decene
K6=0.751; % K_L_G_Dodecene
K7=0.842; % K_L_G_Undecane
wc=(0.3+0.25)*1e-3; %Catalyst weight
k10=2.224e-4;
k20=1.533e-4;
k30=3.988e-5;
k40=1.914e-7;
k50=4.328e-5;
k60=2.506e-7;
k70=4.036e-5;
k80=1.062e-6;
k90=6.055e-7;
E1=109.53;
E2=15.229;
E3=7.881;
E4=44.454;
E5=9.438;
E6=8.426;
E7=10.911;
E8=12.537;
E9=7.127;
k1=k10.*exp(-E1.*(1/T1-1/T2)./R);
k2=k20.*exp(-E2.*(1/T1-1/T2)./R);
k3=k30.*exp(-E3.*(1/T1-1/T2)./R);
k4=k40.*exp(-E4.*(1/T1-1/T2)./R);
k5=k50.*exp(-E5.*(1/T1-1/T2)./R);
k6=k60.*exp(-E6.*(1/T1-1/T2)./R);
k7=k70.*exp(-E7.*(1/T1-1/T2)./R);
k8=k80.*exp(-E8.*(1/T1-1/T2)./R);
k9=k90.*exp(-E9.*(1/T1-1/T2)./R);
n1=C1.*VR; %Ethylene initial condition(moles)
n2=10; %Solvent initial condition(moles)
dNdt=@(t,x) [Q1.*C1-F1-VR.*kL.*(x(1)./VG-K1.*x(8)./VL);Q2-F1-VR.*kL.*(x(2)./VG-K2.*(x(9)./VL));Q3-F1-VR.*kL.*(x(3)./VG-K3.*(x(10)./VL));Q4-F1-VR.*kH.*(x(4)./VG-K4.*(x(11)./VL));Q5-F1-VR.*kH.*(x(5)./VG-K5.*x(12)./VL);Q6-F1-VR.*kH.*(x(6)./VG-K6.*x(13)./VL);Q7-F1-VR.*kH.*(x(7)./VG-K7.*x(14)./VL);VR.*kL.*(x(1)./VG-K1.*x(8)./VL)+wc.*(-2.*k1.*(x(8)./VL).^2-k2.*(x(8)./VL).*(x(9)./VL)-k3.*(x(8)./VL).*(x(10)./VL)-k5.*(x(8)./VL).*(x(11)./VL)-k7.*x(8).*x(12)./(VL.^2));VR.*kL.*(x(2)./VG-K2.*x(9)./VL)+wc.*(k1.*x(8).^2./VL.^2-k2.*x(8).*x(9)./VL.^2-2.*k4.*x(9).^2./VL.^2-k6.*x(9).*x(10)./VL.^2-k8.*x(9).*x(11)./VL.^2);VR.*kL.*(x(3)./VG-K3.*x(10)./VL)+wc.*(k2.*x(8).*x(9)./VL.^2-k3.*x(8).*x(10)./VL.^2-k6.*x(9).*x(10)./VL.^2-2.*k9.*x(10).^2./VL.^2);VR.*kH.*(x(4)./VG-K4.*x(11)./VL)+wc.*(k3.*x(8).*x(10)./VL.^2+k4.*x(9).^2./VL.^2-k5.*x(8).*x(11)./VL.^2-k8.*x(9).*x(11)./VL.^2);VR.*kH.*(x(5)./VG-K5.*x(12)./VL)+wc.*(k5.*x(8).*x(11)./VL.^2+k6.*x(9).*x(10)./VL.^2-k7.*x(8).*x(12)./VL.^2);VR.*kH.*(x(6)./VG-K6.*x(13)./VL)+wc.*(k7.*x(8).*x(12)./VL.^2+k8.*x(9).*x(10)./VL.^2+k9.*x(10).^2./VL.^2);VR.*kH.*(x(7)./VG-K7.*x(14)./VL)];
[t,x]=ode45(dNdt,[0,18000],[n1;0;0;0;0;0;0;0;0;0;0;0;0;n2]);
%moles of products
Moles_C2_G=x(end, 1);
Moles_C4_G=x(end,2);
Moles_C6_G=x(end,3);
Moles_C8_G=x(end,4);
Moles_C10_G=x(end,5);
Moles_C12_G=x(end,6);
Moles_C11H24_G=x(end,7);
Moles_C2_L=x(end, 8);
Moles_C4_L=x(end,9);
Moles_C6_L=x(end,10);
Moles_C8_L=x(end,11);
Moles_C10_L=x(end,12);
Moles_C12_L=x(end,13);
Moles_C11H24_L=x(end,14);
%Grams of products
Mass_C2_G=Moles_C2_G.*28;
Mass_C4_G=Moles_C4_G.*56;
Mass_C6_G=Moles_C6_G.*73;
Mass_C8_G=Moles_C8_G.*112;
Mass_C10_G=Moles_C10_G.*154;
Mass_C12_G=Moles_C12_G.*168;
Mass_C11H24_G=Moles_C11H24_G.*156;
Mass_C2_L=Moles_C2_L.*28;
Mass_C4_L=Moles_C4_L.*56;
Mass_C6_L=Moles_C6_L.*73;
Mass_C8_L=Moles_C8_L.*112;
Mass_C10_L=Moles_C10_L.*154;
Mass_C12_L=Moles_C12_L.*168;
Mass_C11H24_L=Moles_C11H24_L.*156;
c1=length(x(end,(x(end,:)<0)))==0; % condition 1: all masses positive at end
c2=(abs(Mass_C4_G-0.2985)<0.1) &...
(abs(Mass_C6_G-0.6498)<0.1) &...
(abs(Mass_C8_G-0.6147)<0.1) &...
(abs(Mass_C10_G-0.43917)<0.1) &...
(abs(Mass_C12_G-0.40398)<0.1); % condition 2: five masses within 0.1 of target values
fprintf(' %i',i)
end % end while loop
fprintf('\nkL=%.2f, kH=%.2f.\n',kL,kH)
You replied to @Torsten: "I just want to know if this problem has any answers or not. I don't have a specific value for kL and kH in my mind"
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!