Hello everyone, I am building an mpc controller in matlab rather than using the built in 'mpc command'. I am facing some difficulties in building the mpc's matrices I thought about using for loop.my question is how to use for loop to generate these matrices? …thanks in advance
MATLAB: How to build a matrix with “sum” function in its elements
MATLABmpc
Related Solutions
%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)-(Q32*Cb3)-(Q34*Cb3)+(k23*(Ca3)^2*(V3))==0 %for second order reaction
%For reactor 4%(Q34*Cb3)-(Q43*Cb4)-(Qout*Cb4)+(k14*Ca4*V4)==0 %for first order reaction
%(Q34*Cb3)-(Q43*Cb4)-(Qout*Cb4)+(k24*(Ca4)^2*V4)==0 %for second order reaction
syms Ca1 Ca2 Ca3 Ca4 Cb1 Cb2 Cb3 Cb4Eqns_1st_Order=[(Qin*Cain)-(Q12*Ca1)-(k11*Cain*V1)%=0
(Q12*Ca1)+(Q32*Ca3)-(Q23*Ca2)-(k12*Ca2*V2)%=0(Q23*Ca2)+(Q43*Ca4)-(Q32*Ca3)-(Q34*Ca3)-(k13*Ca3*V3)%=0(Q34*Ca3)-(Q43*Ca4)-(Qout*Ca4)-(k14*Ca4*V4)%=0(Qin*Cbin)-(Q12*Cb1)+(k11*Cain*V1)%=0(Q12*Cb1)+(Q32*Cb3)-(Q23*Cb2)+(k12*Ca2*V2)%=0(Q23*Cb2)+(Q43*Cb4)-(Q32*Cb3)-(Q34*Cb3)+(k13*Ca3*V3)%=0(Q34*Cb3)-(Q43*Cb4)-(Qout*Cb4)+(k14*Ca4*V4)]%=0Eqns_1st_Order = 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)]%=0Eqns_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.Ca1ans = sol.Ca2ans = vpa(sol.Ca2, 3)ans =
If those variables are in the workspace, it should run without any problems.
Ca1 = ...;Ca2 = ...;Ca3 = ...;Ca4 = ...;Cb1 = ...;Cb2 = ...;Cb3 = ...;Cb4 = ...;x0 = [Ca1 Ca2 Ca3 Ca4 Cb1 Cb2 Cb3 Cb4];x = fsolve(@functiondescription, x0)
Best Answer