Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ordinary Differential Equations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Module 2

Ordinary Differential Equations

MATLAB provides several powerful approaches to integrate sets of initial value, ordinary
differential equations. In this module we will discuss two approaches: (1) using functions called
by various ‘ode’ routines from a MATLAB command window or script file, and (2) using
functions called by a block from a SIMULINK diagram.

After reviewing this module, you should be able to:

• Write a basic function file containing differential equations to be integrated by MATLAB


ode routines

• Write a more advanced function file, with parameters based through an argument list, and
containing differential equations to be integrated by MATLAB ode routines

• Write a function (more specifically, an S-Function) file containing differential equations


to be integrated by SIMULINK

• Generate the SIMULINK block diagram containing an S-Function block

The sections of this module are:

2.1 MATLAB ode solvers - Basic


2.2 MATLAB ode solvers - Options
2.3 SIMULINK S-functions - Basic
2.4 SIMULINK S-functions - Advanced

© 16 January 01 - B.W. Bequette


2.1 MATLAB ode

MATLAB has several different routines for numerical integration; all use the same basic call
statements:

[t,x]= ode45('xprime',tspan,x0) % nonstiff, medium order


[t,x]= ode23('xprime',tspan,x0) % nonstiff, low order
[t,x]= ode113('xprime',tspan,x0) % nonstiff, variable order
[t,x]= ode15s('xprime',tspan,x0) % stiff, variable order
[t,x]= ode23s('xprime',tspan,x0) % stiff, low order
[t,x]= ode23t('xprime',tspan,x0) % moderately stiff, trapezoidal
[t,x]= ode23tb('xprime',tspan,x0) % stiff, low order

ode45 uses fourth-order and ode23 uses second-order Runge-Kutta integration. All routines
use a variable integration step size (∆t is not constant). The integration step size is adjusted by
the routine to provide the necessary accuracy, without taking too much computation time.

To use these routines, the user must first generate an m-file to evaluate the state variable
derivatives. The m-file must be named ‘xprime.m’, and contain the following statement in the
first line of the file

function xdot = xprime(t,x);

where ‘xprime’ is the name of the function routine (usually selected as something meaningfully
related to the system of equations) ‘xdot’ is the vector of time derivatives of the states, ‘t’ is time
and ‘x’ is the vector of states. ‘xdot’ must be generated as a column vector.

The following command is given to perform the integration

[t,x]= solver('xprime',tspan,x0)

where

solver : is any of the integration routines (ode45, etc.)


xprime : is a string variable containing the name of the m-file for the state derivatives
tspan : is the vector of time values (row vector)
x0 : is the initial condition vector for the state variables (column vector).

The arrays that are returned are

t : a (column) vector of time


x : an array of state variables as a function of time (column 1 is state 1, etc.).

2
For example, if the time vector has 50 elements, and there are three state variables, then the state
variable vector has the 50 rows and three columns. After the integration is performed, if the
student wishes to plot all three variables as a function of time, she/he simply enters

plot(t,x)

If you only want to plot the second state variable, then the command plot(t,x(2,:)) is given.

Example 2.1 Van de Vusse Reaction

Consider the following set of differential equations that describe the van de Vusse reaction
scheme in an isothermal, continuously mixed reactor (CSTR).

dCA F
dt
=
V
( )
CAf − CA − k1CA − k3CA2

dCB F
= − CB + k1CA − k2 CB
dt V

The parameter values are

k1 = 5 6 min −1 k2 = 5 3 min −1 k3 = 1 6 mol l ⋅ min

The input values used in the following simulation are

F V = 4 7 min −1 CAf = 10 mol l

The differential equations are placed in a file named vdv_ode.m

function xdot = vdv_ode(t,x);


%
% Solves the two differential equations modeling the van de vusse reaction
% scheme in an isothermal CSTR. The states are the concentration of A and
% B in the reactor.
%
% [t,x] = ode45(vdv_ode,[0 5],x0)
% integrates from t = 0 to t = 5 min, with initial conditions
% ca0 = x0(1) and cb0 = x0(2), and x0 is a column vector
% 16 Jan 99
% b.w. bequette
%
% since the states are passed to this routine in the x vector,
% convert to natural notation

ca = x(1);
cb = x(2);

% the parameters are:

3
k1 = 5/6; % rate constant for AB (min^-1)
k2 = 5/3; % rate constant for BC (min^-1)
k3 = 1/6; % rate constant for 2AD (mol/(l min))

% the input values are:

fov = 4/7; % dilution rate (min^-1)


caf = 10; % mol/l

% the modeling equations are:


dcadt = fov*(caf-ca) - k1*ca –k3*ca*ca;
dcbdt = -fov*cb + k1*ca – k2*cb;

% now, create the column vector of state derivatives

xdot = [dcadt;dcbdt];

% end of file

In the MATLAB Command window, enter the initial conditions and run ode45

»x0 = [2;1.117]
»[t,x] = ode45('vdv_ode',[0 5],x0);
»subplot(2,1,1),plot(t,x(:,1),'k'), xlabel('t'), ylabel('ca')
»subplot(2,1,2),plot(t,x(:,2),'k'), xlabel('t'), ylabel('cb')

The plots are shown in Figure 1. Notice that the system converges to the steady-state values of
CA=3, CB=1.117.

4
3

2.8

2.6
ca

2.4

2.2

2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t

1.15

1.1
cb

1.05

0.95
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t

Figure 1. Transient Response to Initial Conditions Perturbed from the Steady-State Values
(CA(0) = 2, CB(0) = 1.117).

2.2 MATLAB ode - options

The MATLAB integration routines have additional options (such as changing the default
tolerances for integration) that can be set by the user. Also, parameters for the differential
equations may be passed through the argument list to the function m-file.

[t,x]= ode45('xprime',tspan,x0,options,P1,P2,...)
[t,x]= ode23('xprime',tspan,x0,options,P1,P2,...)
[t,x]= ode113('xprime',tspan,x0,options,P1,P2,...)
[t,x]= ode15s('xprime',tspan,x0,options,P1,P2,...)
[t,x]= ode23s('xprime',tspan,x0,options,P1,P2,...)
[t,x]= ode23t('xprime',tspan,x0,options,P1,P2,...)
[t,x]= ode23tb('xprime',tspan,x0,options,P1,P2,...)

5
To use the default options (generally recommended) and pass through parameter values, simply
enter [ ] for the options element in the argument list. For example, create an ‘xprime.m’ m-file
for the differential equations:

function xdot = xprime(t,x,P1,P2,…)


...

and enter

[t,x]= ode45('xprime',tspan,x0,[ ],P1,P2,...);

in the MATLAB command window.

A typical application might be to be able to change the magnitude of an input variable. For
example, consider the van de Vusse reactor shown in Example 2.1. The first line of the
vdv_ode.m file could be modified in the following fashion

function xdot = vdv_ode(t,x,delstep);

where delstep represents the change in the manipulated input. The file is further changed by the
following line

fov = 4/7 + delstep; % dilution rate (min^-1)

so that delstep represents a change from the nominal input value of 4/7 min-1. If the magnitude of
the step change is 0.1, then the line entered in the MATLAB command window is

[t,x]= ode45('vdv_ode',[0 5],x0,[ ],0.1)

2.3 SIMULINK sfun (.mdl files)

One of the powers of SIMULINK is the ability to construct block diagrams that provide an
intuitive approach to simulating feedback control systems. Often we use linear transfer functions
to simulate the behavior of linear systems. A block titled “sfun ” can obtained from the
“nonlinear” library in SIMULINK and placed in a SIMULINK diagram. When simulations are
performed, the equations in the sfun m-file are solved simultaneously with other blocks in the
block diagram. The sfun file must be given a name with a ‘.m’ extension, while the block
diagram is given a ‘.mdl’ extension.

Example 2.2 Van de Vusse Reaction

6
The van de Vusse reactor from Example 2.1 is studied here. The block diagram shown in Figure
2 was first generated. Notice that there are two inputs and two outputs shown; mux and demux
blocks are used since inputs and outputs are passed to and returned from S-Functions as vectors.
Notice that the S-Function has been titled vdv_sfun.m. The SIMULINK block diagram should
also be titled and given a .mdl extension.

t
Clock
time

4/7 ca
f/v
Mux vdv_sfun Demux conc of A

10 Mux S-Function Demux


cb
caf
conc of B

Figure 2. SIMULINK Diagram for an open-loop simulation of the van de Vusse reactor.

The vdv_sfun function file is shown below. The vectors ‘t’, ‘x’ and ‘u’ contain time, states and
inputs, respectively. The ‘flag ’ parameter is passed from SIMULINK, depending on the
simulation status. A value of flag = 0, for example, sets up the simulation with information
about the number of states, etc. A value of flag = 1 solves for the derivatives of the states with
respect to time and passes these derivatives in the sys vector back to SIMULINK. A value of
flag = 3 passes the values of the outputs back to SIMULINK through the sys vector.

Before running the simulation, “double click” on the S-Function block to see what parameters or
initial conditions must be set before the simulation. In this case, the initial conditions must be set
in the MATLAB workspace, using the xi variable. Also, go to the “parameters” pull-down
menu in SIMULINK to set the final time for integration and the differential equation solution
method.

For the simulation shown, enter

» x0 = [2;1.117]

to obtain the same plot shown in Figure 1.

function [sys,x0,str,ts] = vdv_sfun(t,x,u,flag,x0)


%
% Solves the two differential equations modeling the van de vusse reaction
% scheme in an isothermal CSTR. The states are the concentration of A and
% B in the reactor.
%

7
% Use this as an sfun function in a simulink diagram
%
% 23 Jan 99
% b.w. bequette

% process condition:
% dilution rate (fov) = 4/7;
% feed concentration of a (caf) = 10;
% x1s = cas = 3.0;
% x2s = cbs = 1.117;
% x0 = [x1i;x2i] = [cai;cbi] = initial conditions;

switch flag

case 0 % initialization;

sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);

str = [];
ts = [0 0];

case 1 % derivatives;

% it is convenient to use common notation for states

ca = x(1);
cb = x(2);

% also, the inputs are

fov = u(1);
caf = u(2);

% parameter values

k1 = 5/6; % rate constant for A-->B (min^-1)


k2 = 5/3; % rate constant for B-->C (min^-1)
k3 = 1/6; % rate constant for 2A-->D (mol/(l min))

% state derivatives

dcadt = fov*(caf-ca) - k1*ca -k3*ca*ca;


dcbdt = -fov*cb + k1*ca - k2*cb;

sys = [dcadt;dcbdt];

8
case 3 % outputs;

sys = [x(1);x(2)];

case {2, 4, 9}
sys = [];

otherwise
error(['unhandled flag = ',num2str(flag)]);

end

2.4 SIMULINK sfun (.mdl files) - Advanced

The previous example of the use of SIMULINK and S-functions was quite limited. In the
example, process parameters were imbedded in the S-function (.m file). Parameters can be passed
through argument lists, so that the same files can be used for many different systems by simply
changing parameter values. Some more advanced features are illustrated in the example below.

Example 2.3 Van de Vusse Reactor, extended

The SIMULINK diagram shown in Example 2.2 (Figure 2) merely recreated the same simulation
(perturbation of initial conditions from steady-state) as the MATLAB ode file from Example 2.1.
A real power of SIMULINK is to create complex block diagrams with additional lags on sensors
and actuators, and applying changes to the input variables. The SIMULINK .mdl file shown in
Figure 2 is revised as shown below in Figure 3. The “Van de Vusse Reactor” block is actually
a subsystem that contains the S-function to simulate the reactor, and additional lags for sensors
and actuators. “Double clicking” to “open-up” this subsystem yields the diagram shown in
Figure 4. Constraints are placed on the input variables, to assure that they remain within physical
bounds. Notice that the lags are represented by special transfer function blocks that do not
require the inputs and outputs of the blocks to be in deviation variable form. These special blocks
can be obtained by opening the “Blocksets & Toolboxes ” block on the “Library:
simulink”, then choosing “simulink extras” and “additional linear”.

Kinetic parameters are passed into the S-function file by adding the parameters after x0 in the
argument list:

function [sys,x0,str,ts] = vdv_sfun(t,x,u,flag,x0,k1,k2,k3)

The values of the kinetic parameters are then entered in the MATLAB command window before
beginning the simulation.

9
t
Clock time

ca

conc a
FOV dilution rate conc A

conc A feed conc B cb


Cain Van de Vusse Reactor conc b

remember to set initial conditions


0 < FOV (dilution rate) <10 min^-1
x0(1) = ca0, x0(2) = cb0
0 < Cain < 20 mol/liter
x0=[3.0;1.117]

Figure 3. SIMULINK Diagram for an open-loop simulation of the van de Vusse reactor. The
Simulation Diagram of Figure 2 has been extended. The “Van de Vusse Reactor” block is
composed of a “subsystem” shown in Figure 4.

1 1
1 1
0.1s+1 Mux vdv_sfun Demux 0.1s+1
dilution rate conc A
Saturation valve lag meas lag
(w/ init output) Mux Van de Vusse Demux (w/ init out)
Reactor S-function
2 1
conc A feed 2
Saturation1 0.1s+1
conc B
meas lag
(with init output)
Figure 4. The Subsystem of the Block Shown in Figure 3. Notice that addition actuator and
sensor lags have been placed on the diagram.

The simulation diagram of Figure 3 is “run” by entering the initial conditions in a MATLAB
command window:

»x0 = [3.0;1.117];

Setting the dilution rate step function to move from 4/7 to 4/7 + 0.1 at t = 1 minute, starting the
simulation, then entering the following commands to obtain the plots shown in Figure 5.

»plot(t,cb)
»plot(t,cb,'k')
»subplot(2,1,1),plot(t,ca])

10
»xlabel('time, min')
»ylabel('ca, mol/liter')
»subplot(2,1,2),plot(t,cb)
»xlabel('time, min')
»ylabel('cb, mol/liter')

Notice the characteristic inverse response shown in bottom plot.

3.03

3.025

3.02
ca, mol/liter

3.015

3.01

3.005

3
0 1 2 3 4 5 6
time, min

1.123

1.122

1.121
cb, mol/liter

1.12

1.119

1.118

1.117

1.116
0 1 2 3 4 5 6
time, min

Figure 5. Responses of the concentration of A and B to a small step change in the dilution rate
(from 4/7 to 4/7 + 0.1 min-1).

Summary

We have shown how to write function files to be used by MATLAB and SIMULINK to integrate
sets of ordinary differential equations. For more information on options used by the MATLAB
ode routines, type

help ode45

11
12

You might also like