class7 - ode_matlab
TRANSCRIPT
Ordinary Differential Equations
Outline:1. ODE’s Solution Using MATLAB2. Mixing Process Example3. Level Process Example4. Multiple Reactions Process Example5. Reaction Process with input disturbance
Ordinary Differential Equations
• MATLAB has a collection of m-files, called the ODE suite to solve initial value problems of the form
M(t,y)dy/dt = f(t, y)y(t0) = y0
where y is a vector.• The ODE suite contains several procedures to
solve such coupled first order differential equations.
ODE Solution Using MATLABStep 1: Express the differential equation as a set of first-order ODEs
Step 2: Write an m-file to compute the state derivative
Step 3: Use one of the ODE solvers to solve the equations
[t, y] = ode_solver(‘myprob’, tspan, y0);
Timeindex
Solutionmatrix
ODEsolver
ODE filefor
derivatives
Solutiontime span
[t0 tf]
Initialconditions
M(t,y)dy/dt = f(t,y)
function dydt = myprob(t, y)
ODE Suite Solvers
• ode23: explicit, one-step Runge-Kutta low-order solver
• ode45: explicit, one-step Runge-Kutta medium order solver. First solver to try on a new problem
• ode113: multi-step Adams-Bashforth-Moulton solver of varying order
• ode23s: implicit, one-step modified Rosenbrock solver of order 2
• ode15s: implicit, multi-step numerical differentiation solver of varying order. Solver to try if ode45 fails or is too inefficient
Non-stiff equations Stiff equations
A single well-mixed reactor with one inflow and one outflow
Example 1: Mixing Process
Consider the CSTR, where Cin=50mol-g/m3, Q=5m3/min, V=100m3, C(0)=10mol-g/m3.
• Applying the conservation of mass,
• You get the differential equation as;
𝑉𝑑𝐶𝑑𝑡
=𝑄𝐶𝑖𝑛−𝑄𝐶
Accumulation=𝑖𝑛𝑝𝑢𝑡−𝑜𝑢𝑡𝑝𝑢𝑡+𝑟𝑒𝑎𝑐𝑡𝑖𝑜𝑛
Example 1: Solution
• identify the independent variable, t, the dependent variable, c and initial value, then we can rewrite the ODE as in explicit form as;𝑑𝐶𝑑𝑡
=1𝑉 (𝑄𝐶𝑖𝑛−𝑄𝐶 )= 𝑓 (𝑡 ,𝑐)
𝐶 (0 )=10the initial value
• Write m-file function for the ODE with initial value problem above as
%File Name: myODE1.mfunction dCdt = myODE1(t,C)
Q = 5; %m^3/minC_in = 50; %mg/m^3V = 100; %m^3
dCdt = (Q*C_in – Q*C)/V
Example 1: Solution (cont.)
• Solve the Example 1 by creating new m-file or type syntax in command window as below
%File Name: Example1_Solution.mtimeSpan = 1:1:200;C_0 = 10; %initial value[t,C] = ode45(‘myODE1’,timeSpan,C_0) %call Matlab solver
% display solution of C(t)plot(t,C,'r','LineWidth',2)xlabel('time')ylabel('Concentration, C [molg/m^3]')title('Concentration profile, C(t) in CSTR')
Example 1: Solution (cont.)Optional: you can write code below after call the Matlab solver
Parameter Value Unit
C_in 50 mol-g/m3
C (t = 0) 10 mol-g/m3
Q 5 m3/min
V 100 m3
Example 2: Level Process
A fluid of constant density starts to flow into an empty and infinitely large tank at 8 L/s. A pump regulates the outlet flow to a constant 4L/s. Q: Derive and solve the differential equation describing this process over a 100 second interval.
The volume profile is described as
Thus;
Since density is constant, then
Example 2: Solution
𝐴𝑐𝑐𝑢𝑚𝑢𝑙𝑎𝑡𝑖𝑜𝑛=𝑚𝑎𝑠𝑠𝑓𝑙𝑜𝑤𝑟𝑎𝑡 𝑒𝑖𝑛−𝑚𝑎𝑠𝑠𝑓𝑙𝑜𝑤𝑟𝑎𝑡 𝑒𝑜𝑢𝑡
𝑑 (𝜌𝑉 )𝑑 𝑡
=𝜌𝑞𝑖𝑛−𝜌𝑞𝑜𝑢𝑡
𝑑 (𝑉 )𝑑𝑡
=4
Example 2: Solution (cont.)
Q: Derive and solve the differential equation for this process over a 100 second.
%File Name: myODE2.mfunction dVdt = myODE2(t,V)dVdt = 4;
%File Name: Example2_Solution.mtimeSpan = [0 100];V_0 = 0; %initial value[t,V] = ode45(@myODE2,timeSpan,V_0) ;%call Matlab solverplot(t,V,‘b','LineWidth',2)xlabel('time')ylabel(‘Tank Volume, V [liter]')title(‘Volume profile, V(t) in infinity tank')
• The initial condition (at t = 0) of volume inside tank is 0 liter
Example 2: Solution (cont.)
Example 3: Multiple Reactions Process • The reactions are A B C occur within the tank
where, reaction #1 (A B) with rate constant, k1 and reaction #2 (BC) with rate constant k2.
• The following set of differential equations below describes the change of concentration for three species in the tank.
• Where k1=1 hr-1 and k2=2 hr-1 and at time t=0, Ca=5mol and Cb=Cc=0mol. Solve the system of equations and plot the change in concentration of each species over time.
• Select an appropriate time interval for the integration.
Q: Solve the differential mole balance in the CSTR.
Example 3: Multiple Reactions Process (Cont.)
• Optional : Ca, Cb and Cc variable must be defined within the same matrix, and so by calling Ca =C(1), Cb = C(2) and Cc = C(3), they are listed as common to matrix c.
• The following function file and run file are created to obtain the solution:
%File Name: myODE3.mfunction dCdt=myODE3(t,C)Ca=C(1), Cb=C(2), Cc=C(3) ;global k1 k2 ;dCdt=[-k1*Ca; k1*Ca - k2*Cb; k2*Cb]
Example 3: Solution
%File Name: Example3_Solution.mglobal k1 k2k1=1; k2=2;timeSpan=[0 5];C_0=[5 0 0]; %initial value[t,C] = ode45(@myODE3,timeSpan,C_0) ;%call Matlab solver%display profileplot(t,C(:,1),‘+b','LineWidth',2)xlabel('time, hr')ylabel('Concentration of each species, mols/hr')title(‘Concentration profiles, C(t) in CSTR')hold onplot(t,C(:,2),‘*r','LineWidth',2)plot(t,C(:,3),‘om','LineWidth',2)Legend(‘Ca’,’Cb’,’Cc’)hold off
Example 3: Solution (Cont.)
Example 3: Solution (Cont.)
Example 4: Reaction Process with input disturbance
A CSTR initially filled with 10mol/L of A is to be started up at specific conditions of inlet concentration, inlet flow rate and outlet flow rate. The CSTR suffer with a disturbance at inlet volumetric flow rate, while the outlet volumetric flow rate is kept constant.
0
10
0.005 /
10 /
0.15 /A
out
V L
k mol Ls
C mol L
v mol s
ν 𝑖𝑛 (𝑡 )={ 0 .20+0 .05 𝑡 ;0<𝑡 ≤80 .25−0 .05 𝑡 ;8<𝑡≤140 .15 ;𝑡>14
Example 4: Reaction Process with input disturbance (Cont.)
Input conditions
Output conditions
0
10
0.005 /
10 /
0.15 /A
out
V L
k mol Ls
C mol L
v mol s
Q: Solve the differential equations in the CSTR and plot the CA(t) and V(t) profile in same Figure.
• Identify the dependent and independent variables
• Differential equations mole balance of A:
• Differential equation mass balance in CSTR:
0( )inAA A A
vdCC C kC
dt V
in out
dVv v
dt
Example 4: Solution
𝐶𝐴 (𝑡 )= 𝑓 (𝐶𝐴 ,𝑉 ) ;𝑉 (𝑡 )= 𝑓 (ν 𝑖𝑛)
%File Name: myODE4.mfunction dXdt=myODE4(t,X)k = 0.005;v_out = 0.15;Ca_0 = 10;% Disturbance in input flow rateif( (t >0) & (t <=8) ); v_in = 0.20+0.05*t;elseif( (t>8) & (t <= 14) ); v_in = 0.25-0.05*t;else v_in = 0.15;end%VariablesV = X(1,:);Ca = X(2,:);% The differential equationsdXdt(1,:)= v_in - v_out;dXdt(2,:)= (v_in/V)*(Ca_0-Ca) - k*Ca;
Example 4: Solution (Cont.)
%File Name: Example4_Solution.mtimeSpan =[0 150];X_0=[10 10]; %initial value[t,X] = ode45('myODE4',timeSpan,X_0) ; %call Matlab solver
%display profilesubplot(2,1,1);plot(t,X(:,1),'b','LineWidth',2)ylabel('Volume, [liter]')xlabel('time, s')title('Volume profiles, V(t) in CSTR')subplot(2,1,2); plot(t,X(:,2),'r','LineWidth',2)xlabel('time, s')ylabel('Concentration,C_A [mols/liter]')title('Concentration profiles, C(t) in CSTR')
Example 4: Solution (Cont.)A: The concentration profile of A ,CA(t) and volume profile, V(t) profile can be obtained from code below:
Example 4: Solution (Cont.)
Extra: Comparison between ODE45 and ODE15s
%File Name: Example4_Solution_extra.mtimeSpan =[0 150];X_0=[10 10];%initial value[t,X] = ode45('myODE4',timeSpan,X_0) ;%call Matlab solver[t1,X1] = ode15s('myODE4',timeSpan,X_0);%display profilesubplot(2,1,1);plot(t,X(:,1),'b','LineWidth',2)hold onplot(t1,X1(:,1),'r','LineWidth',2)ylabel('Volume, [liter]')xlabel('time, s')legend('ODE45','ODE15s')hold offtitle('Volume profiles, V(t) in CSTR')subplot(2,1,2); plot(t,X(:,2),'b','LineWidth',2)hold onplot(t1,X1(:,2),'r','LineWidth',2)xlabel('time, s')ylabel('Concentration,C_A [mols/liter]')title('Concentration profiles, C(t) in CSTR')legend('ODE45','ODE15s')hold off
** Please ask for this code…