1.4. Higher-order ODEs#
The Euler method solves a first-order ODE of the form \(y' = f(t, y)\) so what happens when we want to solve a higher order ODE? The solution is we write a higher order ODE as system of first-order ODEs and apply the Euler method to each of these equations. For example consider the \(N\)-th order ODE
If we introduce functions \(y_1, y_2, \ldots, y_N\)[1] where \(y_1=y\), \(y_2 =y'\), \(y_3 =y''\) and so on then
This is a system of \(N\) first-order ODEs so we can apply the Euler method to each equation in the system to give an equivalent solution to the \(n\)th order ODE.
Rewrite the following ODE as a system of first-order ODEs
Solution (click to show)
First rearrange the ODE so that the highest derivative is the subject
then let \(y_1 = y\), \(y_2 = y'\), \(y_3 = y''\) then we can rewrite this ODE as
1.4.1. Solving systems of ordinary differential equations#
We have seen that we can rewrite an \(N\)-th order ODE into a system of \(N\) first-order ODEs and we can apply the Euler method to solve each one. Since we applying the same method to multiple equations it makes sense to group them for convenience. Consider a system of \(N\) first-order ODEs
Let \(\vec{y}\) and \(\vec{f}(t, \vec{y})\) be the vectors
then we can write the system in vector form
The Euler method so solving a system of first-order ODEs is
Where we have an initial value problem that is defined using a system of ODEs we need to know the initial value for each equation in the system, i.e., \(y_1(t_0), y_2(t_0), \ldots\). The solution is an \(n \times N\) matrix \(Y\) where the rows are formed from the solutions for \(\vec{y}_n\), i.e.,
The van der Pol equation is a model of a non-conservative oscillator with non-linear damping which is
where \(y\) is the position of the oscillator, \(\mu\) a parameter for the strength of the dampening and \(\dot{y}\) and \(\ddot{y}\) are the first and second-order derivatives with respect to time \(t\).
Use the Euler method to calculate the solution to the van der Pol equation with \(\mu=1\) over the domain \(t \in [0, 20]\) with initial conditions \(y(0)=2\) and \(\dot{y}(0)=0\) using a step length of \(h=0.1\).
Solution (click to show)
First we need to write the ODE as a system of two first-order ODEs. Let \(y_1=y\) and \(y_2=\dot{y}\) then
which can be written in vector form as
The initial conditions are \(y(0) = 2\) and \(\dot{y}(0) = 0\) so \(\vec{y}_0 = \begin{pmatrix} 2 , 0 \end{pmatrix}\). The values of \(t\) are
Calculating the first few steps of the Euler method with \(\mu=1\) and \(h=0.1\)
Continuing for the whole domain we get
The first column of \(Y\) is the solution \(y\) for the van der Pol equation which has been plotted in Fig. 1.9
1.4.2. Code#
The code used to compute the solutions to Example 1.3 is shown below (this assumes the functions solveIVP()
and euler()
have already been defined). Note that the y0
array and the values returned by the f()
function are \(1 \times 2\) arrays where the number of columns is the same as the number of equations in the system of ODEs.
# Define ODE function (van der Pol equation)
def f(t,y):
return np.array([ y[1], mu* (1 - y[0] ** 2) * y[1] - y[0] ])
# Define IVP parameters
tspan = [0, 20] # boundaries of the t domain
y0 = [2, 0] # initial values
h = 0.1 # step length
mu = 1 # dampening parameter
# Solve the IVP using the Euler method
t, y = solveIVP(f, tspan, y0, h, euler)
# Plot solution
fig, ax = plt.subplots()
plt.plot(t, y[:,0], "b-")
plt.xlabel("$t$", fontsize=12)
plt.ylabel("$y$", fontsize=12)
plt.show()
% Define ODE function (van der Pol equation)
f = @(t, y, mu) [ y(2), mu * (1 - y(1) ^ 2) * y(2) - y(1) ];
% Define IVP parameters
tspan = [0, 20]; % boundaries of the t domain
y0 = [2, 0]; % initial values
h = 0.1; % step length
mu = 1; % dampening parameter
% Solve the IVP using the Euler method
[t, y] = solveIVP(@(t,y)f(t, y, mu), tspan, y0, h, @euler);
% Plot solution
plot(t, y(:, 1), 'b', LineWidth=2)
xlabel('$t$', Fontsize=14, Interpreter='latex')
ylabel('$y$', Fontsize=14, Interpreter='latex')
axis padded