This material has been adapted from material by Fergus Cooper and others from the "Essential Mathematics" module at the Doctoral Training Centre, University of Oxford.
This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1
Systems of differential equations 1
Learning outcomes
Understand the concept of a coupled linear system of differential equations
Recognise the three methods to solve a linear system, and when each is useful
Be able to turn a coupled linear system into a single second order differential equation
Be able to eliminate time to find the paths of the solutions in the plane
Be able to use matrix diagonalisation to solve a linear system in terms of its eigenvalues and eigenvectors
YouTube lecture recording from October 2020
A recording of this lecture from October 2020; the material has changed a little since.
Why systems of differential equations?
In lectures 9 to 11 we solved differential equations for a single quantity, such as a population or a concentration.
Most biological systems have several quantities that change together, each affecting the others.
For example:
These give systems of differential equations, of the form
dtdyi=fi(y1,…,yn,t),
for i=1,…,n, where the rate of change of each quantity can depend on all of the others.
In order to understand these systems, we must first understand coupled linear systems.
In this lecture we solve them using the eigenvalues and eigenvectors from lecture 13, and in lectures 15 and 16 we use them to understand nonlinear systems.
Recap: eigenvalues and eigenvectors
We will use the matrix
A=(1210)
throughout this lecture.
SymPy gives its eigenvalues (each with its multiplicity):
import sympy as sp
A = sp.Matrix([[1,1],[2,0]])A.eigenvals()
{−1:1,2:1}
and its eigenvectors (each with its eigenvalue and multiplicity):
A.eigenvects()
−1,1,−211,2,1,11
So λ1=−1, with eigenvector v1=(−1/2,1)T, and λ2=2, with eigenvector v2=(1,1)T.
Recap: diagonalisation
Recall that if a matrix A has eigenvectorsv1 and v2, with eigenvaluesλ1 and λ2, then with the eigenvectors in the columns of P and the eigenvalues on the diagonal of D,
A=PDP−1,P=(v1v2),D=(λ100λ2).
(This is also true for general n×n matrices with a full set of eigenvectors.)
In our example,
P=(−21111),D=(−1002).
Coupled ODEs
The simplest system is a 2×2linear system, of the form
dtdxdtdy=ax+by,=cx+dy,
where a, b, c and d are constants.
The two equations are coupled: the rate of change of x depends on y, and the rate of change of y depends on x, so we cannot solve either equation on its own.
We have three methods of analysing them mathematically:
Turn them into one second order equation
Divide one by the other, to get one equation independent of t
Perform matrix diagonalisation (extends to n×n problems)
We will use each of them on the same example.
Example
Solve
dtdx=x+y,dtdy=2x,
subject to x(0)=0 and y(0)=3.
Method 1: second order
We start with
dtdxdtdy=x+y,=2x.
Differentiate the first equation, and use the second to replace dy/dt:
dt2d2x=dtdx+dtdy=dtdx+2x.
This is one second order equation for x alone.
Once we have found x, the first equation gives y=dx/dt−x.
Note
Solving the second order equation
We have not covered equations like this one elsewhere in the course, but they can be solved directly.
In this equation, x and its derivatives are only multiplied by constants, so we can try a solution of the form x=eλt.
Substitute it in, and divide by eλt (which is never zero):
λ2eλtλ2−λ−2(λ+1)(λ−2)=λeλt+2eλt=0=0
So λ=−1 or λ=2.
These are the eigenvalues of A, because ∣A−λI∣=0 is the same equation:
∣A−λI∣=1−λ21−λ=(1−λ)(−λ)−2=λ2−λ−2.
Any sum of the two solutions is also a solution, so the general solution is
x=K1e−t+K2e2t,
where K1 and K2 are constants.
Two constants need two conditions.
We have x(0)=0, and from the first equation dx/dt=x+y=3 at t=0:
K1+K2=0,−K1+2K2=3,
so K1=−1 and K2=1.
So x=e2t−e−t, and
y=dtdx−x=(2e2t+e−t)−(e2t−e−t)=e2t+2e−t.
Method 2: eliminate t
We start with
dtdxdtdy=x+y,=2x.
Then, dividing one by the other:
dxdy=dx/dtdy/dt⟹dxdy=x+y2x.
This one equation describes the paths that the solutions follow in the (x,y) plane, but not how fast they move along them, because t no longer appears.
This method is most useful when the new equation is separable.
Here it is not, but it can be solved with the substitution y=vx, as the note below shows.
Note
Solving dy/dx=2x/(x+y)
With y=vx, the product rule gives dy/dx=v+xdv/dx, so
v+xdxdv=1+v2⟹xdxdv=1+v2−v−v2=−1+v(v+2)(v−1).
This is separable.
Using partial fractions, as in lecture 10,
Multiplying by 3, taking exponentials, and putting back v=y/x, the paths are the curves
(y+2x)(y−x)2=K,
where K is a constant.
With K=0 they include the straight lines y=−2x and y=x, which are the directions of the two eigenvectors.
For our example, x=0 and y=3 give K=27.
As an example where eliminating t works well, take the system
dtdx=−y,dtdy=4x.
Dividing, dy/dx=−4x/y, which is separable:
∫ydy21y24x2+y2=−∫4xdx=−2x2+c=C
where C=2c.
These are ellipses, one for each value of C.
At (1,0), dy/dt=4>0, so the solutions go round anticlockwise.
The solution that starts at (1,0) is x=cos2t, y=2sin2t, which goes once round the ellipse with C=4 every π units of time.
Below, it is shown at t=π/8, and the grey arrows show the direction of (dx/dt,dy/dt) at each point:
Method 3: diagonalisation
Let
x=(xy).
Then
dtdx=x+y,dtdy=2x⟹dtdx=Ax,
where A is the matrix from the recap:
A=(1210).
Substitute A=PDP−1:
dtdxP−1dtdxdtd(P−1x)=PDP−1x=DP−1x=D(P−1x)
To get the second line we multiply both sides on the left by P−1.
For the third, P−1 is a matrix of constants, so it can move inside the derivative.
We can now introduce a new variable z=P−1x, so that
dtdz=Dz,ordtd(z1z2)=(λ100λ2)(z1z2).
But now, because the matrix is diagonal, the system is no longer coupled.
The first equation only involves z1 and the second only involves z2, so we can solve each one individually:
Finally, we can substitute back to find the solution of the original coupled system.
Since z=P−1x, we have x=Pz, and the columns of P are the eigenvectors:
So the solution is a sum of the eigenvectors, each growing or shrinking at a rate set by its eigenvalue.
This works in the same way for an n×n system, with n eigenvectors.
In our example,
(xy)=c1(−211)e−t+c2(11)e2t.
Using our initial conditions, x(0)=0 and y(0)=3, at t=0:
(03)=c1(−211)+c2(11).
So −c1/2+c2=0 and c1+c2=3, which gives c1=2 and c2=1.
Finally, we have a solution:
x(t)y(t)=−e−t+e2t,=2e−t+e2t.
This is the same solution that method 1 gave.
The solution in the (x,y) plane
Below, the green curve is the path of this solution, with the dot at t=0.
The grey arrows show the direction of (dx/dt,dy/dt) at each point, and the dashed lines are the directions of the two eigenvectors.
Going back in time, the e−t term is the largest, so the solution comes in along v1.
As t grows, the e2t term takes over, and the solution heads out along v2.
In lecture 16 we will see that the eigenvectors and eigenvalues tell us about the paths near a steady state in the same way.
Checking with SymPy
SymPy can solve the system, initial conditions included:
which agrees with our solution.
Without ics, dsolve gives the general solution, with constants C1 and C2.
Summary
A coupled system of linear first order differential equations can be solved by turning it into one second order equation, by eliminating t (which gives the paths of the solutions, but not their timing), or by diagonalisation.
Diagonalisation extends to n×n systems, and gives the solution as a sum of the eigenvectors, x=c1v1eλ1t+c2v2eλ2t, so the eigenvalues set how fast each part grows or shrinks.
The best method depends on the system and on what we need to ask about it.
In Python, SymPy's dsolve solves a system, and can check our answers.
Introductory problems
Tip
For more practice, an AI tool can write new problems like the ones below.
Ask it for problems of the same type and difficulty, or for problems set in your own field.
For example, you could ask it to "write five ecology problems on systems of linear differential equations, at the level of these ones".
Work them on paper first, then ask the tool to check your answers and to explain any step you are unsure of.
Introductory problems 1
Find the general solution to the following system of ODEs:
dtdx=x,anddtdy=y.
Sketch the form of the solution in the x,y plane, using arrows to indicate where the solution moves over time.
The two equations are not coupled, so we can solve each one separately:
∫xdx=∫dt∫ydy=∫dt⟹x=Aet,⟹y=Bet
where A and B are constants.
Dividing, y/x=B/A is constant, so each solution moves along a straight line through the origin.
Both x and y grow exponentially, so every solution moves directly away from the origin, which is a steady state.
import numpy as np
from matplotlib import pyplot as plt
x, y = np.meshgrid(np.linspace(-2,2,21), np.linspace(-2,2,21))plt.streamplot(x, y, x, y)plt.xlabel('x')plt.ylabel('y')
Introductory problems 2
Take the general decoupled linear system
dtdx=ax,anddtdy=by.
Integrate the two equations separately to solve for x and y in terms of t.
If you start at t=0, x(0)=0, y(0)=0 what happens to the solution over time?
If you start at a general position x(0)=x0, y(0)=y0 what happens to the solution as t→∞?
What if a and b are both negative?
What if only one of a or b is negative? What if either x0 or y0 is negative?
Either by eliminating t from the original equations or by eliminating t from your solutions to part 1., find a general solution of the system. (Why not try both methods?)
Sketch this solution for
a>0, b>0, a=b
a>0, b<0, a=−b.
Separating variables in each equation,
x=x0eatandy=y0ebt
where x0 and y0 are the values at t=0.
With x0=y0=0, x=0 and y=0 for all time.
The origin is a steady state, so the solution stays there.
As t→∞:
If a and b are both positive, x and y both grow exponentially, and the solution moves away from the origin.
If a and b are both negative, x and y both decay to zero, and the solution approaches the origin.
If only b is negative, y→0 while x grows, so the solution approaches the x axis and moves off towards x→±∞ (and if only a is negative, the same happens with x and y swapped).
The signs of x and y never change, because eat and ebt are always positive.
So, if x0 or y0 is negative, the solution behaves in the same way, but stays in the quadrant where it started (for example, it moves towards x→−∞ if x0<0 and a>0).
For a=b>0, y=Kx, which gives straight lines through the origin, with every solution moving away from it (as in Introductory problems 1).
For a=−b>0, y=K/x, which gives hyperbolas with the axes as asymptotes.
Here x grows while y decays, so each solution moves along its hyperbola away from the y axis and towards the x axis.
import numpy as np
from matplotlib import pyplot as plt
x, y = np.meshgrid(np.linspace(-2,2,21), np.linspace(-2,2,21))fig,(left, right)= plt.subplots(1,2, figsize=(10,4))left.streamplot(x, y, x, y)# a = b = 1left.set_title('a = b')right.streamplot(x, y, x,-y)# a = 1, b = -1right.set_title('a = -b')
Main problems
Main problems 1
By reformulating the following system as one first order equation (i.e eliminating t), find the general solution to:
dtdx=−y,anddtdy=x.
Sketch the form of the solutions in the x,y plane.
where r2=2C is a constant.
The solutions are circles centred on the origin.
At (1,0), dx/dt=0 and dy/dt=1, so the solutions go round anticlockwise.
import numpy as np
from matplotlib import pyplot as plt
x, y = np.meshgrid(np.linspace(-2,2,21), np.linspace(-2,2,21))plt.streamplot(x, y,-y, x)plt.gca().set_aspect('equal')plt.xlabel('x')plt.ylabel('y')
Main problems 2
Again by eliminating t and reformulating the system as one first order equation, find the general solution to the following system of ODEs:
dtdx=y,anddtdy=x.
Sketch the form of the solutions in the x,y plane.
Dividing the two equations,
dxdy=yx⟹∫ydy=∫xdx⟹y2=x2+C
So, the general solution is y2−x2=C, where C is a constant.
The solutions are hyperbolas, with the lines y=x and y=−x as asymptotes (these lines are themselves solutions, with C=0).
On the line y=x, dx/dt=x, so solutions move away from the origin.
On the line y=−x, dx/dt=−x, so solutions move towards it.
Every other solution comes in towards the origin along y=−x, then turns and leaves along y=x.
import numpy as np
from matplotlib import pyplot as plt
x, y = np.meshgrid(np.linspace(-2,2,21), np.linspace(-2,2,21))plt.streamplot(x, y, y, x)plt.gca().set_aspect('equal')plt.xlabel('x')plt.ylabel('y')
Main problems 3
Find the eigenvalues and two independent eigenvectors v1 and v2 of the matrix
A=(1141).
Put the vectors v1 and v2 as the columns of a 2×2 matrix P. Find P−1 and show (by calculation) that P−1AP is diagonal. What are the entries of this matrix? What do they correspond to?
Find the general solution of the system
dtdx=x+4y,anddtdy=x+y.
Find the particular solution subject to x(0)=0 and y(0)=2.
Sketch the trajectory (the x(t),y(t) coordinates over time) in the x,y plane.
Draw the eigenvectors v1 and v2 on the same figure.
What happens as t→∞?
What about t→−∞?
What is dy/dx at a general point on the y-axis?
The eigenvalues come from
∣A−λI∣=(1−λ)2−4=λ2−2λ−3=(λ−3)(λ+1)=0
so λ1=3 and λ2=−1.
For λ1=3, (A−3I)v=0 gives −2u+4v=0, so u=2v and v1=(2,1)T.
For λ2=−1, (A+I)v=0 gives 2u+4v=0, so u=−2v and v2=(−2,1)T.
This is diagonal, and its entries are the eigenvalues λ1 and λ2, in the same order as the eigenvectors in P.
The system is dv/dt=Av, with v=(x,y)T.
As in the lecture, let z=P−1v.
Then dz/dt=P−1APz, so
dtdz1=3z1dtdz2=−z2⟹z1=c1e3t,⟹z2=c2e−t
Since v=Pz, the general solution is
(xy)=c1(21)e3t+c2(−21)e−t
where c1 and c2 are constants.
At t=0, x=2c1−2c2=0 and y=c1+c2=2, so c1=c2=1, and
x(t)=2e3t−2e−t,y(t)=e3t+e−t
The trajectory passes through (0,2) at t=0.
As t→∞, the e3t terms dominate, so (x,y)≈e3t(2,1).
The trajectory heads off to infinity in the direction of v1, with x and y both growing.
As t→−∞, the e−t terms dominate, so (x,y)≈e−t(−2,1).
Going back in time, the trajectory came in from infinity in the direction of v2, with x→−∞ and y→∞.
On the y axis, x=0, so
dxdy=dx/dtdy/dt=x+4yx+y=4yy=41
So, every trajectory crosses the y axis with gradient 1/4.
import numpy as np
from matplotlib import pyplot as plt
x, y = np.meshgrid(np.linspace(-4,4,21), np.linspace(-4,4,21))plt.streamplot(x, y, x +4* y, x + y, color='lightgray')t = np.linspace(-1.5,0.5,200)plt.plot(2* np.exp(3* t)-2* np.exp(-t), np.exp(3* t)+ np.exp(-t), label='trajectory')plt.plot([-4,4],[-2,2],'--', label='direction of v1')plt.plot([4,-4],[-2,2],'--', label='direction of v2')plt.xlim(-4,4)plt.ylim(-4,4)plt.xlabel('x')plt.ylabel('y')plt.legend()
Extension problems
Extension problems 1
The force on a damped harmonic oscillator is
f=−kx−mνdtdx,
where x is a displacement, k>0 is a spring force constant, m>0 is the mass and ν>0 is the strength of the damping.
Use Newton's 2nd law of motion to write down an equation for the acceleration d2x/dt2.
Make the substitution
y=dtdx(and hence dtdy=dt2d2x)
to obtain a system of two first-order linear ODEs.
Newton's second law says that force equals mass times acceleration, so
mdt2d2x=−kx−mνdtdx⟹dt2d2x=−mkx−νdtdx
With y=dx/dt, the second derivative becomes dy/dt, and dx/dt becomes y:
dtdx=y,dtdy=−mkx−νy
In matrix form, this is
dtd(xy)=(0−k/m1−ν)(xy)
which we can solve with the methods from the lecture.