Can't figure out how to finalize the code

1 view (last 30 days)
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)-(Q32*Cb3)-(Q34*Cb3)+(k13*Ca3*V3)==0 %for first order reaction
%(Q23*Cb2)+(Q43*Cb4)-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43*Cb4)-(Qout*Cb4)+(k24*(Ca4)^2*V4)]%=0
  2 Comments
Ive J
Ive J on 13 Dec 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);
Carey n'eville
Carey n'eville on 13 Dec 2020
I run the code with this addistion code bu it did'nt give result in command line

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Dec 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 second oreder reaction
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*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
Eqns_2nd_Order = 
sol = solve(Eqns_2nd_Order, [Ca1 Ca2 Ca3 Ca4 Cb1 Cb2 Cb3 Cb4])
sol = struct with fields:
Ca1: [8×1 sym] Ca2: [8×1 sym] Ca3: [8×1 sym] Ca4: [8×1 sym] Cb1: [8×1 sym] Cb2: [8×1 sym] Cb3: [8×1 sym] Cb4: [8×1 sym]
sol.Ca1
ans = 
sol.Ca2
ans = 
vpa(sol.Ca2, 3)
ans = 
  2 Comments
Carey n'eville
Carey n'eville on 13 Dec 2020
Edited: Carey n'eville on 13 Dec 2020
Omg this results are so complicated but impressive solution. Actually I need 1 result for each of Ca1 Ca2 Ca3 Ca4 Cb1 Cb2 Cb3 Cb4. What should I do in order to get 1 result for each of them? Because I need to compare results of Eqns_1st_Order and Eqns_2nd_Order
Walter Roberson
Walter Roberson on 13 Dec 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Chemistry in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!