Can't figure out how to finalize the code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Carey n'eville
am 13 Dez. 2020
Kommentiert: Walter Roberson
am 13 Dez. 2020
I need to write a code in order to get Ca1 Ca2 Ca3 Ca4 Cb1 Cb2 Cb3 Cb4 by using this part which is given below. Only for "Eqns_2nd_Order". But I didn't figure out how can I do. So I need to help, could you help me please?
Eqns_2nd_Order=[(Qin*Cain)-(Q12*Ca1)-(k21*(Cain)^2*V1)%=0
(Q12*Ca1)+(Q32*Ca3)-(Q23*Ca2)-(k22*(Ca2)^2*(V2))%=0
(Q23*Ca2)+(Q43*Ca4)-(Q32*Ca3)-(Q34*Ca3)-(k23*(Ca3)^2*(V3))%=0
(Q34*Ca3)-(Q43*Ca4)-(Qout*Ca4)-(k24*(Ca4)^2*V4)%=0
(Qin*Cbin)-(Q12*Cb1)+(k21*(Cain)^2*V1)%=0
(Q12*Cb1)+(Q32*Cb3)-(Q23*Cb2)+(k22*(Ca2)^2*(V2))%=0
(Q23*Cb2)+(Q43*Cb4)-(Q32*Cb3)-(Q34*Cb3)+(k23*(Ca3)^2*(V3))%=0
(Q34*Cb3)-(Q43*Cb4)-(Qout*Cb4)+(k24*(Ca4)^2*V4)]%=0
%Cain=initial concentration of chemical A in units of ML^-3
%Cbin=initial concentration of chemical B in units of ML^-3
MAIN CODE:
%V1=volume of first reactor in L
%V2=volume of second reactor in L
%V3=volume of third reactor in L
%V4=volume of forth reactor in L
%k1=first order reaction rate in units of (L^3)(M^-1)(T^-1)
%k2=second order reaction rate in units of (L^3)(M^-1)(T^-1)
%Each reactor has different reaction rate
%M=mass in units of M
%Qin=inflow in units of (L^3)(T^-1)
%Qout=outflow in units of (L^3)(T^-1)
%Qxy=the flow from reactor x to reactor y (x&y can be 1,2,3..)
%UNITS
%The unit for V1, V2, V3 & V4 is L
%The unit for k1 is hr^-1
%The unit for k2 is L/(mg*hr)
%The unit for M is mg
%The unit for Qin, Qout,Q12, Q23, Q34, Q32, Q43 & Qxy is L/hr
%Given and calculated values
Qin=10;
Q12=10;
Q23=10+5; %Q23=Q12+Q32
Q34=15-5+3; %Q34=Q23-Q32+Q43
Qout=10;
Q32=5;
Q43=3;
Cain=1;
Cbin=0;
V1=25;
V2=75;
V3=100;
V4=25;
k11=0.05;
k12=0.1;
k13=0.5;
k14=0.1;
k21=0.04;
k22=0.08;
k23=0.4;
k24=0.08;
%% PARTA
%Mass Balance Eqns for Chemical Species A
%For reactor 1
%(Qin*Cain)-(Q12*Ca1)-(k11*Cain*V1)==0 %for first order reaction
%(Qin*Cain)-(Q12*Ca1)-(k21*(Cain)^2*V1)==0 %for second oreder reaction
%For reactor 2
%(Q12*Ca1)+(Q32*Ca3)-(Q23*Ca2)-(k12*Ca2*V2)==0 %for first order reaction
%(Q12*Ca1)+(Q32*Ca3)-(Q23*Ca2)-(k22*(Ca2)^2*(V2))==0 %for second oreder reaction
%For reactor 3
%(Q23*Ca2)+(Q43*Ca4)-(Q32*Ca3)-(Q34*Ca3)-(k13*Ca3*V3)==0 %for first order reaction
%(Q23*Ca2)+(Q43*Ca4)-(Q32*Ca3)-(Q34*Ca3)-(k23*(Ca3)^2*(V3))==0 %for second order reaction
%For reactor 4
%(Q34*Ca3)-(Q43*Ca4)-(Qout*Ca4)-(k14*Ca4*V4)==0 %for first order reaction
%(Q34*Ca,3)-(Q43*Ca,4)-(Qout*Ca,4)-(k24*(Ca4)^2*V4)==0 %for second order reaction
%Mass Balance Eqns for Chemical Species B
%For reactor 1
%(Qin*Cbin)-(Q12*Cb1)+(k11*Ca1*V1)==0 %for first order reaction
%(Qin*Cbin)-(Q12*Cb1)+(k21*(Ca1)^2*V1)==0 %for second oreder reaction
%For reactor 2
%(Q12*Cb1)+(Q32*Cb3)-(Q23*Cb2)+(k12*Ca2*V2)==0 %for first order reaction
%(Q12*Cb1)+(Q32*Cb3)-(Q23*Cb2)+(k22*(Ca2)^2*(V2))==0 %for second oreder reaction
%For reactor 3
%(Q23*Cb2)+(Q43*Cb4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)+(Q43*Cb4)-(Q32*Cb3)-(Q34*Cb3)+(k23*(Ca3)^2*(V3))%=0
(Q34*Cb3)-(Q43*Cb4)-(Qout*Cb4)+(k24*(Ca4)^2*V4)]%=0
2 Kommentare
Ive J
am 13 Dez. 2020
I see duplicates for Cb1,
syms Ca1 Ca2 Ca3 Ca4 Cb1 Cb2 Cb3 Cb4
eqns =[(Qin*Cain)-(Q12*Ca1)-(k21*(Cain)^2*V1) == 0,...
(Q12*Ca1)+(Q32*Ca3)-(Q23*Ca2)-(k22*(Ca2)^2*(V2)) == 0,...
(Q23*Ca2)+(Q43*Ca4)-(Q32*Ca3)-(Q34*Ca3)-(k23*(Ca3)^2*(V3)) == 0,...
(Q34*Ca3)-(Q43*Ca4)-(Qout*Ca4)-(k24*(Ca4)^2*V4) == 0,...
(Qin*Cbin)-(Q12*Cb1)+(k21*(Cain)^2*V1) == 0,...
(Q12*Cb1)+(Q32*Cb3)-(Q23*Cb2)+(k22*(Ca2)^2*(V2)) == 0,...
(Q23*Cb2)+(Q43*Cb4)-(Q32*Cb3)-(Q34*Cb3)+(k23*(Ca3)^2*(V3)) == 0,...
(Q34*Cb3)-(Q43*Cb4)-(Qout*Cb4)+(k24*(Ca4)^2*V4) == 0];
vars = [Ca1 Ca2 Ca3 Ca4 Cb1 Cb2 Cb3 Cb4];
solution = solve(eqns, vars);
Akzeptierte Antwort
Walter Roberson
am 13 Dez. 2020
%V1=volume of first reactor in L
%V2=volume of second reactor in L
%V3=volume of third reactor in L
%V4=volume of forth reactor in L
%k1=first order reaction rate in units of (L^3)(M^-1)(T^-1)
%k2=second order reaction rate in units of (L^3)(M^-1)(T^-1)
%Each reactor has different reaction rate
%M=mass in units of M
%Qin=inflow in units of (L^3)(T^-1)
%Qout=outflow in units of (L^3)(T^-1)
%Qxy=the flow from reactor x to reactor y (x&y can be 1,2,3..)
%UNITS
%The unit for V1, V2, V3 & V4 is L
%The unit for k1 is hr^-1
%The unit for k2 is L/(mg*hr)
%The unit for M is mg
%The unit for Qin, Qout,Q12, Q23, Q34, Q32, Q43 & Qxy is L/hr
%Given and calculated values
Qin=10;
Q12=10;
Q23=10+5; %Q23=Q12+Q32
Q34=15-5+3; %Q34=Q23-Q32+Q43
Qout=10;
Q32=5;
Q43=3;
Cain=1;
Cbin=0;
V1=25;
V2=75;
V3=100;
V4=25;
k11=0.05;
k12=0.1;
k13=0.5;
k14=0.1;
k21=0.04;
k22=0.08;
k23=0.4;
k24=0.08;
%% PARTA
%Mass Balance Eqns for Chemical Species A
%For reactor 1
%(Qin*Cain)-(Q12*Ca1)-(k11*Cain*V1)==0 %for first order reaction
%(Qin*Cain)-(Q12*Ca1)-(k21*(Cain)^2*V1)==0 %for second oreder reaction
%For reactor 2
%(Q12*Ca1)+(Q32*Ca3)-(Q23*Ca2)-(k12*Ca2*V2)==0 %for first order reaction
%(Q12*Ca1)+(Q32*Ca3)-(Q23*Ca2)-(k22*(Ca2)^2*(V2))==0 %for second oreder reaction
%For reactor 3
%(Q23*Ca2)+(Q43*Ca4)-(Q32*Ca3)-(Q34*Ca3)-(k13*Ca3*V3)==0 %for first order reaction
%(Q23*Ca2)+(Q43*Ca4)-(Q32*Ca3)-(Q34*Ca3)-(k23*(Ca3)^2*(V3))==0 %for second order reaction
%For reactor 4
%(Q34*Ca3)-(Q43*Ca4)-(Qout*Ca4)-(k14*Ca4*V4)==0 %for first order reaction
%(Q34*Ca,3)-(Q43*Ca,4)-(Qout*Ca,4)-(k24*(Ca4)^2*V4)==0 %for second order reaction
%Mass Balance Eqns for Chemical Species B
%For reactor 1
%(Qin*Cbin)-(Q12*Cb1)+(k11*Ca1*V1)==0 %for first order reaction
%(Qin*Cbin)-(Q12*Cb1)+(k21*(Ca1)^2*V1)==0 %for second oreder reaction
%For reactor 2
%(Q12*Cb1)+(Q32*Cb3)-(Q23*Cb2)+(k12*Ca2*V2)==0 %for first order reaction
%(Q12*Cb1)+(Q32*Cb3)-(Q23*Cb2)+(k22*(Ca2)^2*(V2))==0 %for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)+(k22*(Ca2)^2*(V2))%=0
(Q23*Cb2)+(Q43*Cb4)-(Q32*Cb3)-(Q34*Cb3)+(k23*(Ca3)^2*(V3))%=0
(Q34*Cb3)-(Q43*Cb4)-(Qout*Cb4)+(k24*(Ca4)^2*V4)]%=0
sol = solve(Eqns_2nd_Order, [Ca1 Ca2 Ca3 Ca4 Cb1 Cb2 Cb3 Cb4])
sol.Ca1
sol.Ca2
vpa(sol.Ca2, 3)
2 Kommentare
Walter Roberson
am 13 Dez. 2020
Are you expecting real values? If so then you might want to extract values from sol and create a mask of locations where the imaginary components are 0, and combine:
Ca1 = sol.Ca1;
Ca2 = sol.Ca2;
[...]
mask = all(imag([Ca1, Ca2, Ca3, Ca4, Ca5, Ca6, Ca7, Ca8]) == 0, 2);
rCa1= Ca1(mask);
rCa2 = Ca2(mask);
...
Afterwards, of the r* are not empty, they will correspond to solutions in which the components are all real-only.
It would not be surprising with these kinds of systems for there to be either no pure-real solutions or else two pure-real solutions.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!