You are on page 1of 24

Ordinary Differential Equations

1. 2. 3. 4. 5. ODE’s Solution Using MATLAB Mixing Process Example Level Process Example Multiple Reactions Process Example 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 MATLAB Step 1: Express the differential equation as a set of first-order ODEs M(t. y0).y)dy/dt = f(t. Time index Solution matrix ODE solver ODE file for derivatives Solution time span [t0 tf] Initial conditions .y) Step 2: Write an m-file to compute the state derivative function dydt = myprob(t. y) Step 3: Use one of the ODE solvers to solve the equations [t. tspan. y] = ode_solver(‘myprob’.

Solver to try if ode45 fails or is too inefficient . onestep modified Rosenbrock solver of order 2 • ode15s: implicit. one-step Runge-Kutta low-order solver • ode45: explicit.ODE Suite Solvers Non-stiff equations • ode23: 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 Stiff equations • ode23s: implicit. multistep numerical differentiation solver of varying order.

where Cin=50mol-g/m3.Example 1: Mixing Process A single well-mixed reactor with one inflow and one outflow Consider the CSTR. . Q=5m3/min. C(0)=10mol-g/m3. V=100m3.

𝑑𝐶 𝑉 = 𝑄𝐶𝑖𝑛 − 𝑄𝐶 𝑑𝑡 • identify the independent variable.Example 1: Solution • Applying the conservation of mass. Accumulation = 𝑖𝑛𝑝𝑢𝑡 − 𝑜𝑢𝑡𝑝𝑢𝑡 + 𝑟𝑒𝑎𝑐𝑡𝑖𝑜𝑛 • You get the differential equation as. the dependent variable. t. c and initial value. the initial value 𝑑𝐶 1 = 𝑄𝐶𝑖𝑛 − 𝑄𝐶 = 𝑓(𝑡. 𝑐) 𝑑𝑡 𝑉 𝐶 0 = 10 . then we can rewrite the ODE as in explicit form as.

%initial value [t.C] = ode45(‘myODE1’. %m^3/min C_in = 50.timeSpan.C) Q = 5. %mg/m^3 V = 100. C_0 = 10.m timeSpan = 1:1:200.) • Write m-file function for the ODE with initial value problem above as %File Name: myODE1.Example 1: Solution (cont.m function dCdt = myODE1(t.C_0) %call Matlab solver . %m^3 dCdt = (Q*C_in – Q*C)/V • Solve the Example 1 by creating new m-file or type syntax in command window as below %File Name: Example1_Solution.

Example 1: Solution (cont. C(t) in CSTR') Parameter C_in C (t = 0) Q V Value 50 10 5 100 Unit mol-g/m3 mol-g/m3 m3/min m3 .'LineWidth'.2) xlabel('time') ylabel('Concentration. C [molg/m^3]') title('Concentration profile.'r'.) Optional: you can write code below after call the Matlab solver % display solution of C(t) plot(t.C.

Q: Derive and solve the differential equation describing this process over a 100 second interval.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. .

Example 2: Solution The volume profile is described as 𝐴𝑐𝑐𝑢𝑚𝑢𝑙𝑎𝑡𝑖𝑜𝑛 = 𝑚𝑎𝑠𝑠𝑓𝑙𝑜𝑤𝑟𝑎𝑡𝑒𝑖𝑛 − 𝑚𝑎𝑠𝑠𝑓𝑙𝑜𝑤𝑟𝑎𝑡𝑒𝑜𝑢𝑡 Thus. then 𝑑(𝑉) =4 𝑑𝑡 . 𝑑(𝜌𝑉) 𝑑𝑡 = 𝜌𝑞𝑖𝑛 − 𝜌𝑞𝑜𝑢𝑡 Since density is constant.

%initial value [t.V] = ode45(@myODE2.timeSpan.%call Matlab solver plot(t.2) xlabel('time') ylabel(‘Tank Volume.V) dVdt = 4.‘b'. • The initial condition (at t = 0) of volume inside tank is 0 liter %File Name: Example2_Solution.'LineWidth'. V *liter+') title(‘Volume profile. %File Name: myODE2.m timeSpan = [0 100].) Q: Derive and solve the differential equation for this process over a 100 second.m function dVdt = myODE2(t. V_0 = 0. V(t) in infinity tank') .Example 2: Solution (cont.V_0) .V.

Example 2: Solution (cont.) .

. reaction #1 (A B) with rate constant. k1 and reaction #2 (BC) with rate constant k2.Example 3: Multiple Reactions Process • The reactions are A B C occur within the tank where. • The following set of differential equations below describes the change of concentration for three species in the tank.

Q: Solve the differential mole balance in the CSTR. Solve the system of equations and plot the change in concentration of each species over time.Example 3: Multiple Reactions Process (Cont. Ca=5mol and Cb=Cc=0mol. .) • Where k1=1 hr-1 and k2=2 hr-1 and at time t=0. • Select an appropriate time interval for the integration.

Cb = C(2) and Cc = C(3). Cb and Cc variable must be defined within the same matrix. and so by calling Ca =C(1). k1*Ca . they are listed as common to matrix c. dCdt=[-k1*Ca. . Cc=C(3) . Cb=C(2).m function dCdt=myODE3(t.k2*Cb. global k1 k2 .C) Ca=C(1). k2*Cb] • Optional : Ca.Example 3: Solution • The following function file and run file are created to obtain the solution: %File Name: myODE3.

‘+b'.2) Legend(‘Ca’. C_0=[5 0 0].2) plot(t.m global k1 k2 k1=1.1).C_0) .'LineWidth'.’Cc’) hold off .‘om'.C(:.3).’Cb’.C(:.‘*r'.) %File Name: Example3_Solution. hr') ylabel('Concentration of each species.Example 3: Solution (Cont. timeSpan=[0 5].C(:.timeSpan.2).'LineWidth'. %initial value [t.%call Matlab solver %display profile plot(t. C(t) in CSTR') hold on plot(t.'LineWidth'.C] = ode45(@myODE3. mols/hr') title(‘Concentration profiles.2) xlabel('time. k2=2.

) .Example 3: Solution (Cont.

The CSTR suffer with a disturbance at inlet volumetric flow rate. inlet flow rate and outlet flow rate. .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. while the outlet volumetric flow rate is kept constant.

05𝑡 .05𝑡 .20 + 0.15 .) Input conditions ν𝑖𝑛 0.005m / Ls ol CA0 conditions ol Output 10m / L vout  0. 0 < 𝑡 ≤ 8 𝑡 = 0.15m / s ol V 10L k  0. . 𝑡 > 14 V 10L k  0.005m / Ls ol CA0 10m / L ol vout  0.15m / s ol Q: Solve the differential equations in the CSTR and plot the CA(t) and V(t) profile in same Figure. 8 < 𝑡 ≤ 14 0.25 − 0.Example 4: Reaction Process with input disturbance (Cont.

𝑉 . 𝑉 𝑡 = 𝑓(ν𝑖𝑛 ) • Differential equations mole balance of A: d v C i n A  (A A k CC C ) A  0 d V t • Differential equation mass balance in CSTR: d V  in  o t v vu d t .Example 4: Solution • Identify the dependent and independent variables 𝐶𝐴 𝑡 = 𝑓 𝐶𝐴 .

.15.20+0.) %File Name: myODE4.25-0.05*t. v_in = 0. % Disturbance in input flow rate if( (t >0) & (t <=8) ).k*Ca. dXdt(2. Ca_0 = 10. v_out = 0. % The differential equations dXdt(1.:)= v_in .15.:)= (v_in/V)*(Ca_0-Ca) .v_out. v_in = 0. end %Variables V = X(1.:). else v_in = 0.X) k = 0.Example 4: Solution (Cont.05*t. elseif( (t>8) & (t <= 14) ).:).005. Ca = X(2.m function dXdt=myODE4(t.

V(t) in CSTR') subplot(2. [liter]') xlabel('time.'LineWidth'. V(t) profile can be obtained from code below: %File Name: Example4_Solution.'LineWidth'. %initial value [t. X_0=[10 10].1). C(t) in CSTR') .2).CA(t) and volume profile.X] = ode45('myODE4'.1).1.X(:.'b'.C_A [mols/liter]') title('Concentration profiles. plot(t.Example 4: Solution (Cont.X_0) .2). s') ylabel('Concentration. s') title('Volume profiles.plot(t.m timeSpan =[0 150].'r'.1.timeSpan. %call Matlab solver %display profile subplot(2.) A: The concentration profile of A .2) ylabel('Volume.2) xlabel('time.X(:.

Example 4: Solution (Cont.) .

X_0) .'LineWidth'.1).2) ylabel('Volume.X(:. V(t) in CSTR') subplot(2.X(:.X1(:.2).C_A [mols/liter]') title('Concentration profiles.2) hold on plot(t1. C(t) in CSTR') legend('ODE45'.X] = ode45('myODE4'.%call Matlab solver [t1.2). plot(t. [liter]') xlabel('time.timeSpan.'b'.X_0).2) hold on plot(t1.2) xlabel('time.m timeSpan =[0 150]. s') legend('ODE45'.'r'.'b'.X1] = ode15s('myODE4'.X1(:.1). plot(t.1). s') ylabel('Concentration.2).timeSpan. %initial value [t.1. X_0=[10 10].'r'. %display profile subplot(2.'LineWidth'.'LineWidth'.'ODE15s') hold off ** Please ask for this code… .'ODE15s') hold off title('Volume profiles.1.'LineWidth'.Extra: Comparison between ODE45 and ODE15s %File Name: Example4_Solution_extra.