Solving Differential Equations With Matlab
Solving Differential Equations With Matlab
1
Consider the initial value problem
dy
= xy, y(1) = 1.
dx
This initial value problem can be solved using the commands
>> y=dsolve(Eqn1,’y(1)=1’,’x’)
y =
exp(-1/2)*exp(x^2/2)
or
>> Init=’y(1)=1’;
y=dsolve(Eqn1,Init,’x’)
y =
exp(-1/2)*exp(x^2/2)
Now that we have solved the ODE, suppose that we want to plot the solution to get a rough
idea of its behavior. We immediately run into two minor difficulties: (1) our expression for
y(x) is not suited for array operations (.*,./,.^) and (2) y, as MATLAB returns it, is
actually a symbol (a symbolic object). The first of these obstacles is straightforward to fix,
using the command vectorize. For the second, we employ the useful command eval, which
evaluates or executes text strings that constitute valid MATLAB commands. Hence, we use
>> x=linspace(0,1,20);
>> z=eval(vectorize(y));
>> plot(x,z,’b’,’linewidth’,1.5)
>> xlabel(’x’)
>> ylabel(’y’)
0.95
0.9
0.85
0.8
y
0.75
0.7
0.65
0.6
0 0.2 0.4 0.6 0.8 1
x
You may notice a subtle point here, that eval evaluates strings (character arrays), and y, as
we have defined it, is a symbolic object. However, vectorize converts symbolic objects into
strings.
2
1.2 Second and Higher Order Equations
Suppose that we want to solve and plot the solution of the second order equation
0.2
0.18
0.16
0.14
0.12
0.1
y
0.08
0.06
0.04
0.02
0
0 0.2 0.4 0.6 0.8 1
x
The MATLAB output for y was omitted above for brevity. Clearly, the symbolic expression
MATLAB gives is not always particularly useful.
1.3 Systems
Suppose that we want to solve and plot solutions of the following system of three linear
ordinary differential equations:
dx
= x + 2y − z
dt
dy
= x+z
dt
dz
= 4x − 4y + 5z.
dt
First, to find a general solution, we proceed as before, except with each equation now braced
in its own pair of single quotation marks.
3
The syntax is
>> [x,y,z] = dsolve(’Dx=x+2*y-z’,’Dy=x+z’,’Dz=4*x-4*y+5*z’)
x =
- (C12*exp(t))/2 - (C10*exp(2*t))/2 - (C11*exp(3*t))/4
y =
(C12*exp(t))/2 + (C10*exp(2*t))/4 + (C11*exp(3*t))/4
z =
C12*exp(t) + C10*exp(2*t) + C11*exp(3*t)
If you use MATLAB to check your work, keep in mind that its choice of constants C1,
C2, and C3 probably won’t correspond with your own. For example, you might have C =
−2C1 + 1/2C3, and so the coefficients of exp(t) in the expression for x are combined.
Fortunately, there is no such ambiguity when initial values are assigned. Notice that since
no independent variable was specified, MATLAB uses its default, t. To solve an initial value
problem, we simply define a set of initial values and add them at the end of our dsolve
command.
Suppose that we have x(0) = 1, y(0) = 2, and z(0) = 3. Then
>> Inits=’x(0)=1, y(0)=2, z(0)=3’;
>> [x,y,z] = dsolve(’Dx=x+2*y-z’,’Dy=x+z’,’Dz=4*x-4*y+5*z’,Inits)
x =
6*exp(2*t) - (5*exp(3*t))/2 - (5*exp(t))/2
y =
(5*exp(3*t))/2 - 3*exp(2*t) + (5*exp(t))/2
z =
10*exp(3*t) - 12*exp(2*t) + 5*exp(t)
Finally, plotting this solution can be accomplished with the following commands:
>> t=linspace(0,0.5,25);
>> xx=eval(vectorize(x));
>> yy=eval(vectorize(y));
>> zz=eval(vectorize(z));
>> plot(t,xx,t,yy,t,zz)
25
x(t)
y(t)
z(t)
20
15
x(t), y(t), z(t)
10
0
0 0.1 0.2 0.3 0.4 0.5
t
4
2 Finding Numerical Solutions
MATLAB has a number of tools for numerically solving ordinary differential equations. We
will focus on the main two, the built-in functions ode23 and ode45, which implement versions
of 2nd/3rd order Runge-Kutta and 4th/5th-order Runge-Kutta methods, respectively.
1.9
1.8
1.7
1.6
1.5
y
1.4
1.3
1.2
1.1
1
0 0.1 0.2 0.3 0.4 0.5
x
5
Choosing the partition. In approximating this solution, the solver ode45 has selected
a certain partition of the interval [0, 0.5], and MATLAB has returned a value of y at each
point in this partition. It is often the case in practice that we would like to specify the
partition of values on which MATLAB returns an approximation. For example, we may only
want to approximate y(0.1), y(0.2), . . . , y(0.5). We can specify this by entering the vector of
values [0, 0.1, 0.2, 0.3, 0.4, 0.5] as the domain in ode45. That is,
>> xvalues=0:0.1:5
>> [x,y]=ode45(f,xvalues,1)
It is important to point out here that MATLAB continues to use roughly the same partition
of values that it originally chose. The only thing that has changed is the values at which it
is printing a solution. In this way, no accuracy is lost.
Options. Several options are available for MATLAB’s ode45 solver, giving you limited
control over the algorithm. Two important options are relative and absolute tolerance,
respectively RelTol and AbsTol in MATLAB. At each step of the ode45 algorithm, an error
is approximated for that step. If yk is the approximation of y(xk ) at step k, and ek is the
approximate error at this step, then MATLAB chooses its partition to ensure
where the default values are RelTol = 10−3 = 0.001 and AbsTol = 10−6 = 0.000001. Notice
that with this convention, if the magnitude of the solution |yk | gets large then the error can
be quite large and RelTol should be reduced. On the other hand, if the magnitude of the
solution is smaller than 10−6 then AbsTol must be reduced.
As an example we note that for the equation y 0 = xy 2 + y, with y(0) = 1, the exact
solution is
1
y(x) = .
1−x
(In order to see this, either simply compute y 0 and check it, or write the equation as (e−x y)0 =
e−x xy 2 , then set w = e−x y, so that w0 = ex xw2 . Now separate variables.) Clearly, the
solution will get large near x = 1, so let’s see what happens numerically. For this example,
we will use format long since small numbers are involved.
6
y =
1.000000000000000
1.333333067684270
1.999997861816372
4.000188105612916
Here, MATLAB gives an error message and fails to compute a value for y at x = 1 − 10−6 .
The exact value is y(1 − 10−6 ) = 106 , so the error would be roughly 106 · 10−3 = 103 . In the
following code, we correct this by changing RelTol to 10−6 .
>> Options = odeset(’RelTol’,1e-6)
Options =
AbsTol: []
BDF: []
Events: []
InitialStep: []
Jacobian: []
JConstant: []
JPattern: []
Mass: []
MassSingular: []
MaxOrder: []
MaxStep: []
NonNegative: []
NormControl: []
OutputFcn: []
OutputSel: []
Refine: []
RelTol: 1.000000000000000e-06
Stats: []
Vectorized: []
MStateDependence: []
MvPattern: []
InitialSlope: []
>> [x,y] = ode45(f,xvalues,1,Options)
x =
0
0.249999750000000
0.499999500000000
0.749999250000000
0.999999000000000
y =
1.0e+05 *
0.000010000000000
0.000013333328835
0.000019999973180
7
0.000039999922011
8.580803914885902
Here we have omitted the semicolon on the odeset command to see the options that MATLAB
has available. Note that MATLAB now gives a value
Of course, more accurate results can be obtained by taking RelTol to be even smaller.
function dy = firstode(x,y)
dy=x*y^2+y;
In this case, we only require one change in the ode45 command. To indicate the function
M-file, we must a pointer @. That is, we use the following commands:
>> xspan=[0,0.5];
>> y0=1;
>> [x,y]=ode45(@firstode,xspan,y0);
8
The function M-file containing the Lorenz equations appears below.
function dx = lorenz(t,x)
% Parameters
sigma=10;
beta=8/3;
rho=28;
% Differential Equations
dx=zeros(3,1);
dx(1)=sigma*(x(2)-x(1));
dx(2)=rho*x(1)-x(2)-x(1)*x(3);
dx(3)=x(1)*x(2)-beta*x(3);
Note that x is stored as x(1), y is stored as x(2), and z is stored as x(3). Additionally, dx is
a column vector which defines the ODEs. To solve the system, use the commands
>> x0=[-8 8 27];
>> tspan=[0,20];
>> [t,x]=ode45(@lorenz,tspan,x0)
The output for the last command consists of a column of times followed by a matrix with
three columns, the first of which corresponds to values of x at the associated times, and
similarly the second and third columns correspond to values of y and z, respectively. The
matrix has been denoted x in the standard calling ode45, and in general and coordinate of
the matrix can be specified as x(m, n), where m denotes the row and n denotes the column.
What we will be most interested in is referring to the columns of x, which correspond to the
components of the system. Along these lines, we can denote all rows or all columns by a
colon :. For example, x(:, 1) refers to all rows in the first column of the matrix x. That is, it
refers to all values of our original x component. Using this information, we can easily plot
the Lorenz strange attractor.
>> plot(x(:,1),x(:,3),’b’,’linewidth’,1.5)
>> xlabel(’x’)
>> ylabel(’y’)
45
40
35
30
25
z
20
15
10
5
-20 -15 -10 -5 0 5 10 15 20
x
9
Of course, we can also plot each component of the solution as a function of t:
>> subplot(3,1,1)
>> plot(t,x(:,1),’b’,’linewidth’,1.5)
>> subplot(3,1,2)
>> plot(t,x(:,2),’b’,’linewidth’,1.5)
>> subplot(3,1,3)
>> plot(t,x(:,3),’b’,’linewidth’,1.5)
20
x(t)
-20
0 5 10 15 20
t
50
y(t)
-50
0 5 10 15 20
t
50
z(t)
0
0 5 10 15 20
t
% Parameters
sigma=p(1);
beta=p(2);
rho=p(3);
% Differential Equations
dx=zeros(3,1);
dx(1)=sigma*(x(2)-x(1));
dx(2)=rho*x(1)-x(2)-x(1)*x(3);
dx(3)=x(1)*x(2)-beta*x(3);
10
We can now send parameter values with ode45.
>> p=[10 8/3 28];
>> [t,x] = ode45(@lorenz1,tspan,x0,[],p);
y 00 + 8y 0 + 2y = cos(x).
y10 = y2
y20 = −8y2 − 2y1 + cos(x).
3 Laplace Transforms
One of the most useful tools in mathematics is the Laplace transform. MATLAB has com-
mands for computing both Laplace transforms and inverse Laplace transforms. For example,
to compute the Laplace transform of f (x) = t2 , use the commands
>> syms t
>> laplace(t^2)
ans =
2/s^3
1
To invert F (s) = , use the commands
1+s
>> syms s
>> ilaplace(1/(1+s))
ans =
exp(-t)
y 00 − 3y 0 + 2y = 0
y(0) = 0
y(1) = 10,
11
where the conditions y(0) = 0 and y(1) = 10 are specified on the boundary of the interval
[0, 1]. Although our solution will typically extend beyond this interval, the most common
scenario in boundary value problems is the case in which we are only interested in values of
the independent variable between the specified endpoints. The first step in solving this type
of equation is to write it as a first order system with y1 = y and y2 = y 0 . Thus, we have
y10 = y2
y20 = −2y1 + 3y2 .
function dy = bvpexample(t,y)
dy=zeros(2,1);
dy(1)=y(2);
dy(2)=-2*y(1)+3*y(2);
Next, we specify the boundary conditions in another function M-file, which records boundary
residues.
res=[y0(1),y1(1)-10];
By residue, we mean the left-hand side of the boundary condition once it has been set to
0. In this case, the second boundary condition is y(1)=10, so its residue is y(1) − 10, which
is recorded in the second component of the vector that bc.m returns. The variables y0 and
y1 represent the solution at x = 0 and x = 1, respectively. The 1 in parentheses indicates
the first component of the vector. In the event that the second boundary condition was
y 0 (1) = 10, we would replace y1(1) − 10 with y1(2) − 10.
We are now in a position to begin solving the boundary value problem. In the following
code, we first specify a grid of x values for MATLAB to solve on and an initial guess for
the vector that would be given for an initial value problem [y(0), y 0 (0)]. Of course, y(0) is
known, but y 0 (0) must be a guess. Loosely speaking, MATLAB will solve a family of initial
value problems, searching for one for which the boundary conditions are met. We solve the
boundary value problem with the MATLAB solver bvp4c.
12
The syntax is as follows:
>> sol=bvpinit(linspace(0,1,25),[0 1]);
>> sol=bvp4c(@bvpexample,@bc,sol);
>> sol.x
>> sol.y
Observe that in this case MATLAB returns the solution as a structure whose first component
sol.x simply contains the values of x we specified. The second component of the structure
sol is sol.y which is a matrix containing as its first row the values of y(x) at the x grid points
we specified, and as its second row the corresponding values of y 0 (x).
5 Event Location
Typically, the ODE solvers in MATLAB terminate after solving the ODE over a specified
domain of the independent variable (the range we have referred to above as xspan or tspan).
In applications, however, we often would like to stop the solution at a particular value of the
dependent variable (for example, when an object fired from the ground reaches its maximum
height or when a population crosses some threshold value). As an example, suppose we
would like to determine the period of a pendulum. Since we do not know the appropriate
time interval (in fact, that is what we’re trying to determine), we would like to specify that
MATLAB solve the equation until the pendulum swings through some specified fraction
of its complete cycle and to give the time this took. In our case, we will record the time
it takes the pendulum to reach the bottom of its arc, and multiply this by 4 to arrive at
the pendulum’s period. (In this way, the event is independent of the pendulum’s initial
conditions.) The equation for a simple pendulum is
d2 θ g
2
= − sin θ,
dt l
which can be written as the system
y10 = y2
g
y20 = − sin y1 ,
l
where y1 = θ and y2 = θ0 . This system is defined in a function M-file with l = 1.
function dy = pendode(t,y)
% Parameters
g=9.81;
l=1;
% Differential Equations
dy=zeros(2,1)
dy(1)=y(2);
dy(2)=-(g/l)*sin(y(1));
13
In addition to this function M-file, we write an events function M-file pendevent.m that
specifies the event we are looking for.
% This function defines the event that our pendulum reaches its
% center point from the right.
In pendevent.m, the line lookfor=y(1) specifies that MATLAB should look for the event
y(1) = 0 (that is, θ(t) = 0). If we wanted to look for the event θ(t) = 1, we would
use lookfor=y(1)-1. The line stop=1 instructs MATLAB to stop solving when the event
is located, and the command direction=-1 instructs MATLAB to only accept events for
which y(2) (that is, θ0 ) is negative (if the pendulum starts to the right of its center, it will
be moving in the negative direction the first time it reaches the center point).
We can now solve the ODE up until the time our pendulum reaches the center point with
the following commands in the Command Window:
Here, y0 is a vector of initial data, for which we have chosen that the pendulum begins with
angle π/4 and with no initial velocity. The command ode45 returns a vector of times t, a
matrix of dependent variables y, the time at which the event occurred, te, and the values of
y when the event occurred, ye. In the event that a vector of events is specified, index vector
ie describes which event has occurred in each instance. Here, only a single event has been
specified, so ie = 1. In this case, we see that the event occurred at time t = 0.5215, and
consequently the period is P = 2.086 (within numerical errors). Though the exact period of
14
the pendulum is difficult to analyze numerically, it is not difficult to show through the small
angle approximation
q sin θ ≈ θ that for small θ the period of the pendulum is approximately
l
P = 2π g , which in our case gives P = 2.001. (While the small angle approximation gives
a period independent of θ, the period of a pendulum does depend on θ.)
In order to better understand this index ie, let’s verify that the time is the same for each
quarter swing. That is, let’s record the times at which θ = 0 and additionally the times at
which θ0 = 0 and look at the times between them. In this case, pendevent.m is replaced by
pendevent1.m.
% Glenn Lahodny Jr.
% Math 442 - Mathematical Modeling
% This function defines the event that our pendulum returns to its
% original position pi/4.
15
0.5216
1.0431
1.5646
>> ye
ye =
0.7854 -0.0000
-0.0000 -2.3972
-0.7853 0.0000
0.0000 2.3970
>> ie
ie =
2
1
2
1
We see that over a time interval [0, 2] the event times are approximately 0, 0.5216, 1.0431,
and 1.5646. Looking at the matrix ye, for which the first value in each row is an angular
position and the second is an angular velocity, we see that the first event corresponds with
the starting position, the second event corresponds with the pendulum’s hanging straight
down, the third event corresponds with the pendulum’s having swung entirely to the opposite
side, and the fourth event corresponds with the pendulum’s hanging straight down on its
return trip. It is now clear how ie works. It is 2 when the second event we specified occurs
and 1 when the first event we specified occurs.
6 Numerical Methods
Although we can solve ODEs in MATLAB without any knowledge of the numerical methods
in employs, it is often useful to understand the basic underlying principles. In this section, we
will use Taylor’s Theorem to derive methods for approximating the solutions to a differential
equation.
0 y 00 (c)
y(x1 ) = y(x0 ) + y (x0 )(x1 − x0 ) + (x1 − x0 )2 ,
2
16
where c ∈ (x0 , x1 ). Observing from our equation that y 0 (x0 ) = f (x0 , y(x0 )), we have
y 00 (c)
y(x1 ) = y0 + f (x0 , y(x0 ))(x1 − x0 ) + (x1 − x0 )2 .
2
If our partition P 00has small subintervals, then x1 − x0 will be small, and we can regard the
smaller quantity y 2(c) (x1 − x0 )2 as an error term. That is, we have
We can now compute y(x2 ) in a similar manner by using Taylor’s Theorem to write
y 00 (c)
y(x2 ) = y(x1 ) + y 0 (x1 )(x2 − x1 ) + (x2 − x1 )2 .
2
Again, it follows from the differential equation that y 0 (x1 ) = f (x1 , y(x1 )), and so
y 00 (c)
y(x2 ) = y(x1 ) + f (x1 , y(x1 ))(x2 − x1 ) + (x2 − x1 )2 .
2
y 00 (c)
If we drop the term 2
(x2 − x1 )2 as an error, then we have
where the value y(x1 ) required here can be approximated by the value from (2). More
generally, for any k = 1, 2, . . . , n − 1, we can approximate y(xk+1 ) from the relation
where y(xk ) will be known from the previous calculation. As with methods of numerical
integration, it is customary in practice to take our partition to consist of subintervals of
equal width,
xn − x0
(xk+1 − xk ) = ∆x = .
n
In the study of numerical methods for differential equations, this quantity is often denoted
by h. In this case, we have the general relationship
Example 6.1. Use Euler’s method with n = 10 to solve the differential equation
dy
= sin(xy), y(0) = π,
dx
on the interval [0, 1].
17
We will carry out the first few iterations in detail, and then we will write a MATLAB
M-file to carry it out in its entirety. First, the initial value y(0) = π gives us the values
x0 = 0 and y0 = π. If our partition is composed of subintervals of equal width, then
1
x1 = ∆x = 10 = 0.1, and by (3) we have
We now have the point (x1 , y1 ) = (0.1, π), and by (3) we have
We now have the point (x2 , y2 ) = (0.2, 3.1725), and by (3) we have
% Step Size
dx=(xn-x0)/n;
% Initial Conditions
x(1)=x0;
y(1)=y0;
% Euler’s Method
for k=1:n
x(k+1)=x(k)+dx;
y(k+1)=y(k)+f(x(k),y(k))*dx;
end
xvalues=x’;
yvalues=y’;
We can implement this file with the following code:
>> f=inline(’sin(x*y)’)
>> [x,y] = euler(f,0,1,pi,10)
>> plot(x,y,’b’,’linewidth’,1.5)
>> [x,y] = euler(f,0,1,pi,100);
>> plot(x,y,’b’,’linewidth’,1.5)
>> xlabel(’x’)
>> ylabel(’y’)
18
first order Taylor polynomial. More generally, if we use a Taylor polynomial of order n we
obtain the Taylor method of order n. In order to see how this is done, we will derive the
Taylor method of order 2. (Euler’s method is the Taylor method of order 1).
Again, letting P = [x0 , x1 , . . . , xn ] denote a partition of the interval [x0 , xn ] on which we
would like to solve (1), our starting point for the Taylor method of order 2 is to write down
the Taylor polynomial of order 2 (with remainder) for y(xk+1 ) about the point xk . That is,
according to Taylor’s Theorem,
y 00 (xk ) y 000 (c)
y(xk+1 ) = y(xk ) + y 0 (xk )(xk+1 − xk ) + (xk+1 − xk )2 + (xk+1 − xk )3 ,
2 3!
where c ∈ (xk , xk+1 ). As with Euler’s method, we drop off the error term (which is now
smaller), and our approximation is
0 y 00 (xk )
y(xk+1 ) ≈ y(xk ) + y (xk )(xk+1 − xk ) + (xk+1 − xk )2 .
2
We already know from our derivation of Euler’s method that y 0 (xk ) can be replaced with
f (xk , y(xk )). In addition to this, we now need an expression for y 00 (xk ). We can obtain this
by differentiating the original equation y 0 (x) = f (x, y(x)). That is,
d 0 d ∂f ∂f dy
y 00 (xk ) = y (x) = f (x, y(x)) = (x, y(x)) + (x, y(x)) ,
dx dx ∂x ∂y dx
where the last equality follows from a generalization of the Chain Rule to functions of two
variables. From this last expression, we see that
∂f ∂f ∂f ∂f
y 00 (xk ) = (xk , y(xk )) + (xk , y(xk ))y 0 (xk ) = (xk , y(xk )) + (xk , y(xk ))f (xk , y(xk )).
∂x ∂y ∂x ∂y
Replacing y 00 (xk ) with the right-hand side of this last expression, and replacing y 0 (xk ) with
f (xk , y(xk )), we conclude that
(xk+1 − xk )2
∂f ∂f
y(xk+1 ) ≈ y(xk )+f (xk , y(xk ))(xk+1 −xk )+ (xk , y(xk )) + (xk , y(xk ))f (xk , y(xk )) .
∂x ∂y 2
If we take subintervals of equal width ∆x = (xk+1 − xk ), this becomes
∆x2
∂f ∂f
y(xk+1 ) ≈ y(xk )+f (xk , y(xk ))(xk+1 −xk )+ (xk , y(xk )) + (xk , y(xk ))f (xk , y(xk )) .
∂x ∂y 2
Example 6.2. Use the Taylor method of order 2 with n = 10 to solve the differential
equation
dy
= sin(xy), y(0) = π,
dx
on the interval [0, 1].
We will carry out the first few iterations by hand and leave the rest as an exercise. To
begin, we observe that
f (x, y) = sin(xy)
fx (x, y) = y cos(xy)
fy (x, y) = x sin(xy).
19
If we let yk denote an approximation for y(xk ), then the Taylor method or order 2 becomes
(0.1)2
yk+1 = yk + sin(xk yk )(0.1) + [yk cos(xk yk ) + xk cos(xk yk ) sin(xk yk )] .
2
Beginning with the plot (x0 , y0 ) = (0, pi), we compute
y1 = π + π(0.005) = 3.1573,
which is closer to the correct value of 3.1572 than was the approximation of Euler’s method.
We can now use the point (x1 , y1 ) = (0.1, 3.1573) to compute
(0.1)2
y2 = 3.1573 + sin(0.1 · 3.1573)(0.1) + [3.1573 cos(0.1 · 3.1573) + 0.1 cos(0.1 · 3.1753)]
2
= 3.2035,
which again is closer to the correct value than the approximation for Euler’s method.
20