0% found this document useful (0 votes)
225 views

Class7 - Ode - Matlab

This document provides examples of solving ordinary differential equations (ODEs) using MATLAB. It outlines MATLAB's ODE suite for solving initial value problems and describes various solvers. It then gives 4 examples of applying the ODE solvers to chemical engineering problems involving mixing processes, level processes, multiple reactions, and a reaction process with input disturbances. The examples derive the relevant ODEs, write MATLAB functions to define the derivatives, and use ODE solvers to plot the solutions.

Uploaded by

Mohd Fauzi Zanil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

Class7 - Ode - Matlab

This document provides examples of solving ordinary differential equations (ODEs) using MATLAB. It outlines MATLAB's ODE suite for solving initial value problems and describes various solvers. It then gives 4 examples of applying the ODE solvers to chemical engineering problems involving mixing processes, level processes, multiple reactions, and a reaction process with input disturbances. The examples derive the relevant ODEs, write MATLAB functions to define the derivatives, and use ODE solvers to plot the solutions.

Uploaded by

Mohd Fauzi Zanil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Ordinary Differential Equations

Outline:
1. 2. 3. 4. 5. ODEs 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,y)dy/dt = f(t,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, y] = ode_solver(myprob, tspan, y0);


Time index Solution matrix ODE solver ODE file for derivatives Solution time span [t0 tf]
Initial conditions

ODE Suite Solvers


Non-stiff equations
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

Stiff equations
ode23s: implicit, onestep modified Rosenbrock solver of order 2 ode15s: implicit, multistep numerical differentiation solver of varying order. Solver to try if ode45 fails or is too inefficient

Example 1: Mixing Process


A single well-mixed reactor with one inflow and one outflow

Consider the CSTR, where Cin=50mol-g/m3, Q=5m3/min, V=100m3, C(0)=10mol-g/m3.

Example 1: Solution
Applying the conservation of mass,

Accumulation = +
You get the differential equation as;

=
identify the independent variable, t, the dependent variable, c and initial value, then we can rewrite the ODE as in explicit form as;

the initial value

1 = = (, ) 0 = 10

Example 1: Solution (cont.)


Write m-file function for the ODE with initial value problem above as
%File Name: myODE1.m function dCdt = myODE1(t,C) Q = 5; %m^3/min C_in = 50; %mg/m^3 V = 100; %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.m timeSpan = 1:1:200; C_0 = 10; %initial value [t,C] = ode45(myODE1,timeSpan,C_0) %call Matlab solver

Example 1: Solution (cont.)


Optional: you can write code below after call the 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')

Parameter C_in C (t = 0) Q V

Value 50 10 5 100

Unit mol-g/m3 mol-g/m3 m3/min 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.

Example 2: Solution

The volume profile is described as


=

Thus; ()

Since density is constant, then

() =4

Example 2: Solution (cont.) Q: Derive and solve the differential equation for this process over a 100 second.
%File Name: myODE2.m function dVdt = myODE2(t,V) dVdt = 4;

The initial condition (at t = 0) of volume inside tank is 0 liter %File Name: Example2_Solution.m timeSpan = [0 100]; V_0 = 0; %initial value [t,V] = ode45(@myODE2,timeSpan,V_0) ;%call Matlab solver plot(t,V,b','LineWidth',2) xlabel('time') ylabel(Tank Volume, V *liter+') title(Volume profile, V(t) in infinity tank')

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.

Example 3: Multiple Reactions Process


(Cont.)

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: Solution
The following function file and run file are created to obtain the solution:

%File Name: myODE3.m function 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]

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.

Example 3: Solution (Cont.)


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

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.

Example 4: Reaction Process with input disturbance (Cont.)


Input conditions

0.20 + 0.05 ; 0 < 8 = 0.25 0.05 ; 8 < 14 0.15 ; > 14

V 10L k 0.005m / Ls ol CA0 10m / L ol vout 0.15m / s ol

V 10L k 0.005m / Ls ol CA0 conditions ol Output 10m / L 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.

Example 4: Solution

Identify the dependent and independent variables = , ; = ( ) 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 (Cont.)


%File Name: myODE4.m function dXdt=myODE4(t,X) k = 0.005; v_out = 0.15; Ca_0 = 10; % Disturbance in input flow rate if( (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 %Variables V = X(1,:); Ca = X(2,:); % The differential equations dXdt(1,:)= v_in - v_out; dXdt(2,:)= (v_in/V)*(Ca_0-Ca) - k*Ca;

Example 4: Solution (Cont.)


A: The concentration profile of A ,CA(t) and volume profile, V(t) profile can be obtained from code below: %File Name: Example4_Solution.m timeSpan =[0 150]; X_0=[10 10]; %initial value [t,X] = ode45('myODE4',timeSpan,X_0) ; %call Matlab solver

%display profile subplot(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.)

Extra: Comparison between ODE45 and ODE15s


%File Name: Example4_Solution_extra.m timeSpan =[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 profile subplot(2,1,1); plot(t,X(:,1),'b','LineWidth',2) hold on plot(t1,X1(:,1),'r','LineWidth',2) ylabel('Volume, [liter]') xlabel('time, s') legend('ODE45','ODE15s') hold off title('Volume profiles, V(t) in CSTR') subplot(2,1,2); plot(t,X(:,2),'b','LineWidth',2) hold on plot(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

You might also like