Problem 40 Define the vector
1.6 MATLAB PROGRAMMING
1.6.6 Solution of Differential Equations
The (numerical) solution of differential equations is one of the great strengths of MATLAB (and Simulink). The techniques for numerical solution of dif-ferential equations are considerably refined compared to earlier MATLAB versions. In the same way, the range of methods has been extended. At this point, we can only deal with the more or less direct methods based on the Runga-Kutta procedure for solving initial value problems. Beyond that, how-ever, methods are available for solving boundary value problems, differential equations with delays, and certain partial differential equations. These, how-ever, require extensive mathematical knowledge so that discussing them is beyond the scope of this book.
In the following we shall restrict ourselves to a discussion of the basic solution functions (“solvers”) ode23ode23 and ode45ode45 based on the Runge-Kutta method and their use for solving ordinary differential equations and systems of ordinary differential equations.
These methods are also fundamental for calculations in Simulink, as we shall see in Chapter 2.
We cannot (and will not) go into the mathematical background of these methods at this point. For this we refer to the relevant mathematical literature.
In order to show how, for example, ode23 is used for the numerical solution of differential equations, here is an excerpt from the description of ode23:
>> help ode23
ODE23 Solve non-stiff differential equations, low order method. [T,Y] = ODE23(ODEFUN,TSPAN,Y0) with TSPAN =
[T0 TFINAL] integrates the system of differential equations y' = f(t,y) from time T0 to TFINAL with initial conditions Y0.
Function ODEFUN(T,Y) must return a column vector corresponding
to f(t,y). Each row in the solution array Y corresponds to a time returned in the column vector T. To obtain solutions at specific times T0,T1,...,TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL].
[T,Y] = ODE23('F',TSPAN,Y0,OPTIONS) solves as above with default integration parameters replaced by values in OPTIONS, an argument created with the ODESET function. See ODESET for details.
...
The rest of this description deals with other options within this command.
These, however, are not relevant at the beginning.
The returned parameters for this function are the calculated time vector Tand the corresponding value matrix of the solution(s) Y.
As you can gather from this description, besides the initial conditionsy0
and the interval ([t0, tfinal]) over which the solution is to be calculated, the method ode23 must be supplied with one or a system of ordinary differential equations in an m-file. The file name will, in turn, be passed on to the ode23 method as a function handle.
The procedure is similar for ode45 and the other methods.
We now illustrate this procedure with a (classical) example.
Mathematical Pendulum
We are interested in the solution of the differential equation for the so-called mathematical pendulum,
¨α(t) = −g
l · sin (α(t)), g= 9.81 m
s2 . (1.4)
Here, α(t) is the angle that the pendulum (of length l) forms at time t relative to its equilibrium position (see Fig. 1.25).
Since we are dealing with a second order differential equation, its solution requires two initial conditions,
α(0) = α(0)
˙α(0)
, (1.5)
which define the starting position (deflection) of the pendulum and its initial velocity. These initial conditions are passed on to the integration method as the vector fory0.
α
Equilibrium position
1
FIGURE 1.25 The mathematical pendulum.
In order to describe the differential equation, in MATLAB we have to define how the derivatives of the unknown function α(t) depend on one another. This, of course, has to be done with the aid of the numerical values of the function (i.e., with the vectors of the functions and their derivatives at the subdivision points).
This we do using a MATLAB function pendde with which we describe the first and second derivatives and return them as a vector.
The structure of this function must satisfy a definite form, which we shall discuss further in the following. A complete overview can be obtained by entering odefile in the search form for MATLAB help.
The function has the following form:
function [alphadot] = pendde(t,alpha)
%
% Function pendde
%
% call: [alphadot] = pendde(t,alpha)
%
% Example of the solution of differential equations
% in MATLAB with ode23
%
% This function defines the differential equation
% for a mathematical pendulum (l=pendulum length).
% For MATLAB to proceed with this, the second order DE
% must initially be converted into a system of first order DEs.
%% Setting constants
l=10; % pendulum length
g=9.81; % acceleration of gravity m/s
%% Preliminary initialization
alphadot = [0;0];
%% Representation of the differential equation
% the first first order equation alphadot(1) = alpha(2);
% the second first order equation alphadot(2) = -(g/l)*sin(alpha(1));
This implementation obviously requires some comment.
First, we should note the following: in order for MATLAB’s ode23 to handle the Eq. (1.4), the equation has to be converted into a system of first order differential equations!
To do this, we set
α1(t) := α(t), (1.6)
α2(t) := ˙α(t) . (1.7)
Eq. (1.4) can then be rewritten in the form
˙α1(t)= α2(t), (1.8)
˙α2(t)= −g
l · sin (α1(t)) (1.9)
with the initial conditions
α(0) =
It is this representation to which ode23 and the accompanying representa-tion of the differential equarepresenta-tion connect in the m-file pendde.m.
The equation alphadot(1) = alpha(2); thus represents the first part of the system of equations (1.8) and alphadot(2) = -(g/l)*sin(alpha(1)); the second; this means that at each time point, the values on the right-hand side are passed on to the components of the vectors representing the derivatives.
The parameter t must also be passed on to pendde, even if it is not used within the function. It is required by ode23. Function ode23 processes the initial conditions first.
All differential equations to be solved using ode23 (or other meth-ods) essentially have to be brought into the form “vector of the first order derivatives equals a function of the solution vector” and so represented in a MATLAB file.
The following calls then provide a representation of the solution for the initial values α(0) = π4 and ˙α(0) = 0 within the interval [0, 20] and for a pendulum of length 10. The solution and its derivative (dashed curve) are shown in Fig. 1.26.
>> [t, solution] = ode23(@pendde, [0, 20], [pi/4,0]);
>> plot(t, solution(:,1),'r-',t, solution(:,2),'g--')
0 2 4 6 8 10 12 14 16 18 20
−0.2
−0.4
−0.6
−0.8 0 0.2 0.4 0.6 0.8
FIGURE 1.26 Solution of Eq. (1.4) with MATLAB’s ode23.
The following comments pertain to the calculated solution:
The solution algorithm works within a so-called step-size control. This means that where the solution varies little, large step sizes are used, and where it varies rapidly, small step sizes are used. This increases the efficiency of the algorithm. A look at the work space for the above example shows how
the step size (the vector t) was consecutively adjusted:
Still, it is often worthwhile to derive a solution with an equidistant step size. In that case the vector on which the solution is to be evaluated can be explicitly given in the call for ode23 :
>> [t, solution] = ode23(@pendde, (0:0.1:20), [pi/4,0]);
>> [t, solution]
The step size (see the vector t) is now, as desired, uniform with a separation of 0.1.
Another important comment touches on the fact that in this example the parameters g and l required in the description function pendde are defined with fixed values.
Thus, in the present version g and l can be varied only if the source text of pendde is modified. This cannot, of course, be the answer if, for example, the pendulum length is to be varied in the course of a multiple simulation.
Nevertheless, it is possible in principle to provide other parameters to the definition functions for the differential equations. The interested reader can turn to the related MATLAB documentation or the MATLAB help to examine this question and, if necessary, for dealing with Problem 77.
RC Low-pass Filter
We conclude this section with another, somewhat simpler example.
Fig. 1.27 is a circuit diagram of an RC combination with a voltage source.
FIGURE 1.27 RC combination with a voltage source.
The voltage at the output of the system obeys the following first order, linear differential equation:
d
dtu(t)= − 1
RCu(t)+ 1
RCu1(t) . (1.11)
We solve this differential equation numerically using MATLAB’s ode23 function, first by representing the differential equation in a MATLAB function named RCcomb as follows:
function [udot]= RCcomb(t,u)
%
% Function RCcomb
%
% call: [udot] = RCcomb(t,u)
%
% Example of the solution of differential equations
% in MATLAB with ode23
%
% This function defines the differential equation
% for an RC combination with voltage source u1(t).
R = 10000; % resistance R
C = 4.7*10e-6; % capacitance C
udot = 0; % preinitialization
% the equation is of first order
% u1(t) must be defined as
% a MATLAB function
udot = -(1/(R*C))*u + (1/(R*C))*u1(t);
The function u1(t) must be defined as a MATLAB function. In this example we have defined the function u1 as the unit step function. It is executed as follows in the m-file u1.m in the working directory:
function [step] = u1(t)
%
% function u1
%
% call: [step] = u1(t)
%
% Implementation of the unit step function
%
% ...
step = t>=0; % This is so simple!
The MATLAB commands
>> [t, solution] = ode23(@RCcomb, [0, 3], 0);
>> plot(t, solution)
>> grid
then provide the solution shown in Fig. 1.28 in the time interval[0, 3] s. Here, fixed values of 10 k and 4.7µF, which yield a time constant of τ = RC = 0.47 s, have been used.
This value can be read very nicely off the graph, since it is known that in that time the response to a step function reaches (1− e−1) times, or 63 % of the height of the step (here 1).
0 0.5 1 1.5 2 2.5 3 0
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
FIGURE 1.28 Step function response of the RC combination for 10 k and 4.7 µF.
PROBLEMS
Work out the following problems for practice in solving differential equations with MATLAB.
NOTE Solutions to all problems can be found in Chapter 4.
Problem 72
Write a function with which the differential equation of the mathematical pendulum in its linearized form,
¨α(t) = −g
l · α(t) (1.12)
can be solved.
Modify pendde.m accordingly.
Then compare the two numerical solutions for large initial deflections.
Problem 73
Determine the solution of Eq. (1.11) for this RC combination with a voltage source for different excitation functions (source voltages) u1(t).
Problem 74
Solve one of the examples of differential equations using the procedure ode45and compare this solution with that from ode23.
Problem 75
Solve the following differential equation for a growth process using MAT-LAB: ˙P(t) = α(P(t))β with α = 2.2, β = 1.0015 . (1.13)
Problem 76
Use MATLAB to solve the differential equation for the so-called VZ1-element (first-order delay VZ1-element),
d
dtν(t) + 1
Tν(t) = K
Tu(t) . (1.14)
At the same time, make some reasonable assumptions about K and T, with which you can propose a probability interpretation.
Problem 77
Solve the differential equation for the mathematical pendulum (Eq. 1.4) for different pendulum lengths using MATLAB. It should be possible to transfer the respective pendulum lengths by calling the solution algorithm (ode23).
In addition, after checking with MATLAB help, modify pendde.m in a way such that other parameters can be passed on through the definition function for the differential equation.
Problem 78
Solve the system of differential equations
˙y1(t)= − 2y1(t)− y2(t) y1(0)= 1, (1.15)
˙y2(t)= 4y1(t)− y2(t) y2(0)= 1 (1.16) with the aid of MATLAB. Compare the solution with the exact solution, which you can calculate by hand or by using the symbolics toolbox (see Section 1.8).
Problem 79
If only one return value is specified, the functions ode.. yield a structure that contains all the necessary statements about the solution. This structure can be processed by the function deval.
Familiarize yourself with the syntax of deval and solve Eq. (1.4) once again using ode23. Next, evaluate the result using deval for equidistant partition points separated by 0.01. Plot the solution using the return value of deval.