Using MATLAB To Solve Differential Equations Numerically
Using MATLAB To Solve Differential Equations Numerically
y y
1 x2y x
(1)
!"
and save it as .
The first line defines a function , a function of time and the state vector , which
returns the value of the right hand side of the system (1). Since the system is autonomous,
time does not appear explicitly. The next line initialises the function to be a matrix with 2
1
rows and 1 column i.e. a column vector consisting of zeroes. The two final lines then
compute the two coordinates of the right hand side. The lines end with a semi-colon to
prevent the result from being printed when the function is called.
To solve this equation numerically, type in the MATLAB command window
)'')' '
A new window opens with the plot. Here ) means everything in the first column in
and '' gives a red curve. The second column is shown in blue with a dash-dotted line.
Use
to see how to control the plot.
A plot in the phase plane of the solution may be obtained by
))
See figure 1.
Note, that in the MATLAB command window you can scroll through previous commands by using the up and down arrow keys. This is most useful for editing and reusing
previous commands.
The Runge-Kutta method used above is a good choice for a standard solver. However,
for some systems of differential equations the error control will force the solver to take
extremely small steps, and more advanced methods are needed. Such systems are called
stiff, and this occurs typically when the dynamics involve both slow and fast changes.
Methods for stiff problems are generally implicit, that is, in each time step a non-linear
set of algebraic equations must be solved. This is time-consuming, but the advantage is
that much larger steps can be taken and the overall performance is significantly improved.
To illustrate stiffness, consider a modified van der Pol equation
x
y y
10001 x2 y x
(((
(((" !"
and solve as before but with a very short time interval t 0 1:
2
(2)
10
12
14
16
18
20
0.5
1.5
2.5
3
2.5
1.5
0.5
F IGURE 1: MATLAB plots of a solution to the van der Pol equation (1). Top: Time traces.
Bottom: Phase plane.
*(**
The array has 3033 rows (and 1 column) so this is the number of steps. MATLAB also
has an implicit solver for stiff systems, &, which may be used as follows:
+&
Here, only 165 steps was needed and the computation was performed much faster.
In fact, the time interval considered is much too short to be of interest. These commands give the plots in figure 2:
1.5
0.5
0.5
1.5
2.5
500
1000
1500
2000
2500
3000
1500
1000
500
500
1000
1500
2.5
1.5
0.5
0.5
1.5
F IGURE 2: MATLAB plots of a solution to the van der Pol equation (2). Top: Time trace of x.
Bottom: Phase plane.
Rather than define individual -files for the different versions of the van der Pol
equation, it is convenient to consider a parametrised family
x
y y
1 x2 y x
(3)
and then afterwards specify the relevant values of the parameter . Parameters may be
transported as follows. Define the file with the following contents
,
" !"
(The variable , is a dummy variable which is not important here.) To obtain a solution with 1, type
-
,
,
#
$ ,
. /
#01$ -
1)1)
#01$ -
1)1)
In the procedure -
is the name of the -file defining the equations,
is the time interval wanted for the solutions, , is of the form #
$ and defines the plotting window in the phase plane, and is the name of a
MATLAB differential equation solver.
When called, a plotting window opens, and the cursor changes into a cross-hair. Clicking with the left mouse button at a point in the phase space gives the orbit through that
point. First the equations are integrated forwards in time and this part of the orbit is plotted. Then the same is done backwards in time. In either time direction the orbit may
become unbounded, and a warning may be printed in the MATLAB command window.
This is generally of no consequence, but if you have chosen
to large, computations may be unacceptably slow. Then press control-c in the MATLAB command
window to break the execution and adjust the time interval. When an orbit is drawn you
can continue clicking at points, and when you are satisfied click any other mouse button
in the window, and the procedure terminates.
Figure 3 is created by the call
5
3
3
''#( *($ # * *
-
,
,
#
$ ,
. /
#01$ -
# $
1)1)
#01$ -
# $
1)1)
The phase portrait in figure 3 can then be also be obtained by the call
''#( *($ # * *