Skip to main content
\(\require{cancel}\newcommand{\domain}[1]{\operatorname{Dom}(#1)} \newcommand{\range}[1]{\operatorname{Range}(#1)} \newcommand{\linearspan}{\operatorname{span}} \newcommand{\abs}[1]{\lvert #1 \rvert} \newcommand{\set}[2]{\left\{ #1 \: \middle\vert \: #2 \right\}} \renewcommand{\vec}[1]{\mathbf{#1}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\Q}{\mathbb{Q}} \newcommand{\R}{\mathbb{R}} \DeclareMathOperator{\Lapl}{\mathcal{L}} \newcommand{\La}[1]{\Lapl \left\{ #1 \right\}} \newcommand{\invLa}[1]{\Lapl^{-1}\left\{ #1 \right\}} \newcommand{\intbyparts}[4]{\begin{tabular}{|rl|rl|}\hline $u$ \amp $#1$ \amp $dv$ \amp $#2$ \\ \hline $du$ \amp $#3$ \amp $v$ \amp $#4$ \\ \hline \end{tabular}} \newcommand{\identity}{\mathrm{id}} \newcommand{\notdivide}{{\not{\mid}}} \newcommand{\notsubset}{\not\subset} \newcommand{\swap}{\mathrm{swap}} \newcommand{\Null}{\operatorname{Null}} \newcommand{\half}{\text{ \nicefrac{1}{2}}} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section3.2Numerical Solution Methods

Place holder text

SubsectionEuler's Method

Euler's method is an algorithm for approximating solutions to first-order IVPs \begin{equation*} \frac{dy}{dt} = f(t,y) \quad y(t_0) = y_0. \end{equation*} The key idea of Euler's method and all numerical techniques is discretization. If a variable can take on any value between two specified values, it is called a continuous variable; otherwise, it is called a discrete variable. When the independent variable is time, \(t\text{,}\) we discretize it by imagining it to correspond with the ticks of a clock, just like how the second hand on a clock moves jerkily as opposed to smoothly from one second to the next.

Our discrete ticks are determined by a start time, \(t_0\text{,}\) and a tick size, or step size \(h = \Delta t\text{,}\) for example \begin{equation*} t_0, \underbrace{t_0+h}_{t_1}, \underbrace{t_0+2h}_{t_2}, \underbrace{t_0+3h}_{t_3}, \ldots \end{equation*}

The next step is to pick a starting point or initial condition: \((t_0, y_0)\text{,}\) then we use \(f(t,y)\) to generate a list of coordinate pairs which can be plotted, i.e. \((t_0, y_0), (t_1,y_1), (t_2,y_2), (t_3,y_3), \ldots\)

The above list of pairs is generated via the following recursive algorithm.

Example3.2.2Euler's Method By Hand

Use Euler's method to approximate a solution to the IVP: \begin{equation*} \frac{dy}{dt} = 3t-2y, \quad y(1)=5 \end{equation*} with a step size of \(h=\tfrac{1}{2}\text{,}\) for \(n=4\) steps.

When doing the algorithm by hand, it makes sense to organize your calculations into a table. Notice that in each row (except for the first row), \(y\) is equal to \(y + \Delta y\) from the previous row.

\(n\) \(t\) \(y\) \(\Delta y\) \(=\) \(h\cdot f(t,y)\)
\(0\) \(1\) \(5\) \(-3.5\) \(=\) \(\tfrac{1}{2}[3(1)-2(5)]\)
\(1\) \(1.5\) \(1.5\) \(0.75\) \(=\) \(\tfrac{1}{2}[3(1.5)-2(1.5)]\)
\(2\) \(2\) \(2.25\) \(0.75\) \(=\) \(\tfrac{1}{2}[3(2)-2(2.25)]\)
\(3\) \(2.5\) \(3.0\) \(0.75\) \(=\) \(\tfrac{1}{2}[3(2.5)-2(3.0)]\)
\(4\) \(3\) \(3.75\) \(\) \(=\) \(\tfrac{1}{2}[3()-2()]\)

It is not necessary to compute \(\Delta y\) in the last row. The output of the algorithm is just the data in the \(t\) and \(y\) columns which we can present as a single table

\(t\) \(y\)
\(1.0\) \(5.00\)
\(1.5\) \(1.50\)
\(2.0\) \(2.25\)
\(2.5\) \(3.00\)
\(3.0\) \(3.75\)

Since Sage is really just Python with lots of extra built-in math related functions we can implement the above algorithm as a Python function. Be aware that Sage has two kinds of functions, mathematical functions and Python functions. A Python function is a way to store an algorithm or procedure that can be used repeatedly. You will need to evaluate the euler Python function below once and all the other cells in your worksheet can use it.

The Python version of the algorithm should be fairly easy to understand, but perhaps the last line needs explanation. It uses the Python function zip. This function takes two lists and combines them into one. The following Sage cell has an example of how zip works.

Note: You will need to define a mathematical function f(t,y) in Sage before using the Python euler function. This f(t,y) function is the right hand side of the differential equation \(y' = f(t,y)\text{.}\) This is done in Sage by typing, for example: f(t,y) = t*y

Evaluating the following Sage cell which contains the Python implementation of Euler's method will not result in any output. It does create a new function that all of the other Sage cells on this page depend upon, so you must evaluate it before evaluating the other cells on this page.

Let's test our Python implementation of Euler's method with the population model. Sage's table function displays the output in an easy to read table.

Of course, once we have a list of coordinate pairs, it is natural to plot them. If you want points and lines then you will need to use the list_plot function to plot points, and the line function to plot lines.

We can use Sage to plot the output from Euler's method and an exact solution on the same set of axes for comparison. Below we do this for the IVP: \begin{equation*} \frac{dy}{dt} = \sin t, \quad y(0)=0. \end{equation*} which has particular solution, \(y(t) = -\cos(t)+1\text{.}\)

It is instructive to see how the choice of step size affects the approximation. We can see this by plotting multiple approximation curves on the same set of axes. Below we do this for the linear IVP: \begin{equation*} \frac{dy}{dt} + y = \cos t, \quad y(0)=1. \end{equation*} Which has particular solution: \begin{equation*} y(t) = \frac{1}{2} \left( \cos t + \sin t + e^{-t} \right). \end{equation*}

We can combine appoximate solutions with slope field plots. Below we do so for the IVP: \begin{equation*} \dfrac{dy}{dt} = -y + \cos t, \quad y(-\pi)=5, \end{equation*} which has solution \begin{equation*} y(t) = \frac{1}{2} \left( \cos t + \sin t + 11e^{-(t+\pi)} \right). \end{equation*}

SubsectionHeun Method or Improved Euler

Euler's method uses the left side endpoint of the interval \([t_n, t_{n+1}]\) to predict the slope for the whole interval. But that is rather unsymmetric, because it ignores the slope at the right side of the interval. What if we compute the slopes at both endpoints and then average them?

Below is an implementation of the Heun algorithm in Python.

Next we compare Euler and Heun on the same set of axes for the population model IVP, \begin{equation*} \frac{dy}{dt} = y, \quad y(0)=1, \end{equation*} with solution, \(y(t) = e^t\text{.}\)

SubsectionThe Runge–Kutta Method

SubsectionExercises

Exercise 1 must be done by hand, and you must show your work! The rest of the exercises should be done in Sage. Print your Sage worksheet and staple to your handwritten work for exercise 1.

1

Apply Euler's Method twice to approximate the solution to \begin{equation*} \frac{dy}{dt} = -2ty, \quad y(0)=2 \qquad y(t)=2e^{-t^2}, \end{equation*} on the interval [0,2], once with \(h=1\) and once with \(h=0.5\text{.}\) Create two tables showing your data. The first table is started for you below.

\(n\) \(t\) \(y\) \(\Delta y\) \(=\) \(h\cdot f(t,y)\)
\(0\) \(0\) \(2\) \(0\) \(=\) \((1)[-2(0)(2)]\)
\(1\) \(1\) \(2\) \(-4\) \(=\) \((1)[-2(1)(2)]\)
\(2\) \(2\) \(-2\) \(\) \(=\) \((1)[-2(2)(-2)]\)
\(3\) \(3\) \(\) \(\) \(=\) \((1)[-2(3)(~~)]\)
\(4\) \(4\) \(\) \(\) \(=\) \((1)[-2(4)(~~)]\)
2

Repeat the previous problem but use euler and table to generate \(t,y\) tables. One for \(h=1\) and one table for \(h=0.5\text{,}\) with \(n=4\text{.}\)

3

Numerically approximate the IVP, \begin{equation*} \frac{dy}{dt} = t^2 + y^2, \quad y(0)=1. \end{equation*} Use the Heun (improved Euler) algorithm with \(h=0.1\) and \(n=10\text{.}\) Plot your results.

4

Numerically approximate the IVP, \begin{equation*} \frac{dy}{dt} = -y + t, \quad y(1)=1 \end{equation*} Use both the Euler and Heun methods with \(h=0.2\) and \(n=10\text{.}\) Plot both results on the same set of axes.

5

Implement the Runge-Kutta algorithm in Python (Sage). Hint: copy and paste the code for euler into a Sage cell and then change the name to runge_kutta and add 4 lines to the main loop.

6

Use your implementation of Runge-Kutta to numerically approximate \begin{equation*} \frac{dy}{dt} = \cos(t\cdot y), \quad y(-2\pi) = 1. \end{equation*} Use \(h=0.5\) and do 25 steps, \(n=25\text{.}\) Plot your results.