Ordinary Differential Equations
Ordinary Differential Equations
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.
• Write a more advanced function file, with parameters based through an argument list, and
containing differential equations to be integrated by MATLAB ode routines
MATLAB has several different routines for numerical integration; all use the same basic call
statements:
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
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.
[t,x]= solver('xprime',tspan,x0)
where
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.
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
ca = x(1);
cb = x(2);
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))
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).
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:
and enter
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
where delstep represents the change in the manipulated input. The file is further changed by the
following line
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
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.
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
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.
» x0 = [2;1.117]
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;
ca = x(1);
cb = x(2);
fov = u(1);
caf = u(2);
% parameter values
% state derivatives
sys = [dcadt;dcbdt];
8
case 3 % outputs;
sys = [x(1);x(2)];
case {2, 4, 9}
sys = [];
otherwise
error(['unhandled flag = ',num2str(flag)]);
end
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.
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:
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
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')
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