Upload
vhinzsanguinary
View
37
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Math. Credits to original uploader
Citation preview
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Numerical Solution of Differential Equations
Matlab Tutorial 3
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Introduction MATLAB has several routines for numerical integration ode45,
ode23, ode113, ode15s, ode23s, etc.
Here we will introduce two of them: ode45 and ode23
ode23 uses 2nd-order and ode45 uses 4th-order Runge-Kutta integration.
We will learn more about Runge-Kutta method in the class.
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Integration by ode23 and ode45: Matlab Command
[t, x] = ode45(‘xprime’, [t0,tf], x0)
where
xprime is a string variable containing the name of the m-file for the derivatives.
t0 is the initial time
tf is the final time
x0 is the initial condition vector for the state variables
t a (column) vector of time
x an array of state variables as a function of time
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Note We need to generate a m-file containing expressions for
differential equations first.
We’ll examine common syntax employed in generating the script or m-file
These objectives will be achieved through 2 examples: Example-1 on Single-Variable Differential Equation
Example-2 on Multi-Variable Differential Equation
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Differential Equation of a Single-Variable
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Example 1: Start-up time of a CSTR
Objective: Solve differential mole balance on a CSTR using MATLAB integration routine.Problem description: A CSTR initially filled in 2mol/L of A is to be started up with specified conditions of inlet concentration, inlet and outlet flow rates. The reactor volume and fluid density is considered to be constant. Refer to Lecture-15 classnotes for more details.
Reaction: A → BRate Kinetics: (-rA) = kCA
Initial Condition: at t=0, CA = CA,initial = 2 mol/L
0Ao Cv ,
ACv ,
V
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Example 1The following first-order differential equation in
single-variable (CA) is obtained from mole balance on A:
0Ao
AA C
V
vCk
V
v
dt
dC
)(
ovv
Recall, that mass balance yields
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
generating a m-file titled cstr.m
function dx=cstr (t, x)
% define constants
k=0.005; %mol/L-sV=10; % Reactor volume in Lvin=0.15; % Inlet volumetric flow rate in L/sCa0=10; % Inlet concentration of A in mol/L
%For convenience sake, declaring that variable x is CaCa=x
%define differential equationdx=(vin/V)*Ca0-(vin/V+k)*Ca;
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Script File: Common Syntax
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Purpose of function files
As indicated above, the function file generates the value of outputs every time it called upon with certain sets of inputs of dependent and independent variables
For instance the cstr.m file generates the value of output (dx), every time it is called upon with inputs of independent variable time (t) and dependent variable (x)
NOTE: For cstr.m file, the output dx is actually dCa/dt and x is equal to Ca.
function dx=cstr (t, x)
function output=function_name (input1, input2)
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Function File: Command Structure
function dx = CSTR (t, x)
Define constants (e.g. k, Ca0, etc.)
(Optional) Write equations in terms of constants
Define differential equations that define outputs (dx=…)
function output=function_name (input1, input2)
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
File & Function Name
Example: m-file titled cstr.m
function dx=cstr (t, x)
% define constants
k=0.005; %mol/L-sV=10; % Reactor volume in L
Function name should match file name
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Inputs and Outputs
Example: m-file titled cstr.m
function dx=cstr (t, x)
% define constants
k=0.005; %mol/L-sV=10; % Reactor volume in L
Inputs are independent variable (t) and dependent variable (x=Ca)
Output is differential, dx = dCa/dt
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Writing Comments
Example: m-file titled cstr.m
function dx=cstr (t, x)
% define constants
k=0.005; %mol/L-sV=10; % Reactor volume in L
Any text written after “ % ” symbol is considered to be commented
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Semicolon at the end of an expression
Example: m-file titled cstr.m
function dx=cstr (t, x)
% define constants
k=0.005; %mol/L-sV=10; % Reactor volume in L
Semi-colon simply suppresses SCREEN printing of the expression.
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
End of Script File: Common Syntax”
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Command for Integration of Differential Equation
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Example-1 enter the following MATLAB command
[t, x]=ode45(‘cstr’,[0 500],[2]’);
to see the transient responses, use plot functionplot(t, x);
Refer to slide-3 for syntax of above command
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Example-2: Multi-variable Differential Equations
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Example 2: CSTR Response to change in volumetric flow rate. Objective: Solve differential mole balance on a CSTR using MATLAB integration routine. Problem description: CSTR operating at SS is subjected to a small disturbance in inlet volumetric flow rate while the outlet
volumetric flow rate is kept constant. Both total mass balance and species mole balance must be solved simultaneously. Refer to Lecture-15 class notes for more details.
0Ao Cv ,
ACv ,
V
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Example 2First-order differential equation in two-variables – V(t)
and CA(t):
Equations (1) and (2) must be solved simultaneously.
AAAoA CkCC
V
v
dt
dC )( 0
vvdt
dVo
(1)
(2)
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Generating the script filefunction dx=cstr1 (t, x)%constantk=0.005; %mol/L-svout=0.15; % L/sCa0=10; %mol/L% The following expression describe disturbance in input flow rateif((t >0)&(t <=2)) vin=0.15+.05*(t) elseif((t>2)&(t<=4)) vin=0.25-0.05*(t-2);else vin=0.15;end% define x1 and x2V=x(1,:)Ca=x(2,:)% write the differential equationdx(1,:)=vin-vout;dx(2,:)=(vin/V)*(Ca0-Ca)-k*Ca;
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Script File: New Syntax
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Recognizing Multivariable System
function dx=cstr1 (t, x)% constantk=0.005; %mol/L-svout=0.15; % L/sCa0=10; %mol/L
The first important point to note is that x is a vector of 2 variables, x1 (=V) and x2(=Ca)
Also, dx is a vector of two differential equations associated with the 2 variables
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Defining arrays
% define x1 and x2V=x(1,:)Ca=x(2,:)
The value of these variables change as a function of time. This aspect is denoted in MATLAB syntax by defining the variable as an array.
Thus variable 1 can be indicated as x(1,:) and variable 2 as x(2,:)
For bookkeeping purposes or convenience sake, the two variables are re-defined as follows
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Defining differential equations
% write the differential equationdx(1,:)=vin-vout;dx(2,:)=(vin/V)*(Ca0-Ca)-k*Ca;
There are two differential equations – dV/dt and dCa/dt – that must be solved. These two equations are represented in vector form as “dx”
Two differential equations must be defined. The syntax is shown below
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
End of “Script File: New Syntax”
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Command for Integration of Differential Equation
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Example-2 enter the following MATLAB command
[t, x]=ode45(‘cstr1’,[0 500],[10 7.5]’);
to see the transient responses, use plot functionplot(t, x(:,1);
plot(t, x(:,2);
Initial conditions for the two variables, i.e. V=10 L and CA=7.5 mol/L at time t=0
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
Example-2 Did you spot any problems in the plots?
Do you see any transient response at all? Likely not. It’s all to do with the “integration step-size” [We’ll
learn more about this in Week-7 lectures]
Type the following Matlab commands options=odeset('Initialstep',.1) [t, x]=ode45('cstr1',[0 300],[10 7.5]',options)
Plot x1 and x2. (see command in previous slide)
February 2005 CHEE 222: Process Dynamics and Numerical Methods Prepared by Kunal Karan
End of Matlab Tutorial-3