Wichita State University Logo

Math 555: Differential Equations

2.7 Numerical Approximations: Euler's Method


2.7.1 Motivation and Background

In this chapter we have discussed methods of solution for a few specific classes of first-order differential equations, including linear, separable, and exact equations. Our approach to finding a solution has focused on identifying that our equation is one of these types or transforming it into such a type.

Generally, however, it is not possible to analytically determine a solution $y = \phi(t)$ to the initial value problem

$$ \dfrac{dy}{dt} = f(t,y),\qquad y(t_0) = y_0. $$
Most differential equations will require us to take a different approach, such as looking at a solution under particular restrictions or relying on a qualitative or computational (not analytic) approach. For example, we have used direction fields several times to give us an impression of the behavior of the solutions to an ODE, very useful qualitative information. The weakness is that it does not give us an obvious route to determine or compare values of the solutions. Here, we will introduce a method which addresses this shortcoming.

2.7.2 Euler's Method

Consider the differential equation

$$ \dfrac{dy}{dt} = y(3-ty) $$
with direction field Direction Field for y' = y(3-ty).

The usefulness of direction fields is that it gives us an impression of the way a function is changing as a function of $t$ and $y$. Each arrow represents a small tangent line segment of a solution to the ODE. Here, the direction field is presented with the arrows drawn very close together, to the point where it is easy to follow how one arrow leads to the next. This provides us with a qualitative impression of how the solutions behave, but couldn't it give us a quantitative one?

Suppose we form a piecewise linear approximate solution by connecting these small line segments. Would this give us a valid approximation? In order for it to, several things must be true:

  1. The construction of the approximate solution needs to be systematic and easy to understand. This means we need an algorithm to generate the approximation.
  2. The approximation needs to work. If we form a piecewise linear approximation of a solution to the ODE, is it close enough in form to an actual solution to be useful?
  3. The generated approximation needs to be accurate. A numerical solution to the ODE is only useful if it is relatively close to the behavior of an actual solution. We need to know how accurate the solution is. For instance, does the solution exist within a particular error band of the actual solution? How big is that band?

Fortunately, all three of these things are true. We will address the first two issues in this section, but issue 3 is beyond the scope of a first semester course in ODEs.

This technique of connecting small tangent line segments in order to form an approximation to the solution of an ODE was developed by Leonhard Euler (the most prolific mathematician in history) in the 1760's and is called Euler's method or the tangent line method .

Understanding the Technique

Suppose $y = \phi(t)$ is a solution to the initial value problem

$$ \dfrac{dy}{dt} = f(t,y),\qquad y(t_0) = y_0. $$
Our goal is to approximate this solution $\phi(t)$ using Euler's method. We begin by using the facts that the solution passes through the point $(t_0,y_0)$ and has a tangent line with slope $f(t_0,y_0)$ at this point. Using this information, we can write down the equation for line tangent to $\phi(t)$ at $t_0$

$$ y = y_0 + f(t_0,y_0)(t-t_0). $$
We know from calculus that the tangent line is a good approximation to the curve $\phi(t)$ a short distance from the point $t_0$ (taking care to make sure that the derivative does not change too much). Moving along the tangent line, we find the value $y_1$ by traveling a short distance from $t_0$ to $t_1$ using the equation

$$ y_1 = y_0 + f(t_0,y_0)(t_1-t_0). $$
as shown here Tangent line approximation to a curve

The value $\phi(t_1)$ is the actual value of the solution at $t_1$ and $y_1$ is the tangent line approximation of that value. They are obviously not equal, but that is a limitation of the method. To continue computing the approximation, we repeat this process by starting at $(t_1,y_1)$ and using tangent line formula at this point to find $y_2$

$$ y_2 = y_1 + f(t_1,y_1)(t_2-t_1). $$
This procedure is carried out many times, using the value from the previous value found to compute a new tangent line slope and point. Assuming we know the previous ordered pair $(t_n, y_n)$, we can compute the next value $y_{n+1}$ using the formula

$$ y_{n+1} = y_n + f(t_n,y_n)(t_{n+1}-t_n),\qquad n = 0,1,2,\ldots. $$
It is using this formula we can generate an approximate solution, choosing an initial value of $(-5,5\times 10^{-7})$ Approximate solution determined by Euler's method plotted on direction field

For the sake of comparison, we can graph this approximation alongside the actual solution (since this ODE is a Bernoulli equation ) to evaluate its accuracy.

Comparison of actual and approximate solutions to the equation y' = y(3-ty)

We see that in this case the approximation is quite good. In the regions were the derivative is small (and hence the tangents are close to horizontal), the approximation is extremely accurate. Where the approximation breaks down are the regions where there are large changes in the derivative of the function. This should not be a surprise. The more the derivative changes in a small interval, the more the function changes, and hence worsens the approximation. Typically, this can be combated by selecting a smaller step size $h = t_{n+1} - t_n$, but large changes in the derivative will still be harmful to the accuracy of Euler's method approximations.

2.7.3 Solving Problems Computationally

The previous graphical examples were generated with the programming language Python using code I have included at the bottom of this page. If you have familiarity with Python (or a desire to learn), the code is marked where changes are necessary to create graphs for other ODEs.

Knowledge of a scientific computing or programming language is extremely useful in the study of advanced mathematics. It is strongly recommended that you develop a familiarity with one of

  1. Python
  2. Matlab (or its open source alternative Octave )
  3. R

or a similar language as part of your studies. These tools are invaluable as ways to visualize the solutions of complex problems and to apply computational approaches to problems that cannot be solved via analytic means.

More generally, the capacity to translate a mathematical process to an algorithm implemented using technology will also teach you to be a better problem solver. As our technology gets more sophisticated, it becomes increasingly important for all of us to educate ourselves on how to interface with it better. Society is driven by progress, and increased technological capacity will undoubtedly lead to the tackling of more and more difficult problems. This means that more will be required of us as users of technology as time moves forward, not less. Stay proactive and vigilant about increasing your knowledge and skills, because there will always be more to learn and new technology to master.

Example

That said, Euler's method does not require one of these scientific languages to be put to use. In fact, it can be set up using something surprisingly simple: spreadsheets .

At its core, Euler's method is about generating a "table" of values. We are representing the approximate solution to the ODE $y(t)$ as a list of output values $\{y_j\}$ that correspond to a list of input values $\{t_j\}$. The $j^{th}$ element of the list $y_j$ is generated using the values from the previous step $t_{j-1}$ and $y_{j-1}$. Spreadsheets excel at this type of dynamic relational computation. This video demonstrates an example of how spreadsheets may be used to apply Euler's method to the initial value problem

$$ y' = t^2 + y^2,\qquad y(0) = 1 $$ in the interval $t\in [0,1]$ for several step sizes $h = 0.1,\ 0.05,\ 0.025,\ 0.01$.

Video Example

Here is the direction field for $t\in[0,1]$ and $y\in[0,2]$ for reference while watching the video: Direction Field for y' = t^2 + y^2

Direction Field with Euler's Method and Actual vs Approximate Solution

import numpy as np
import matplotlib.pyplot as plt

# Differential equation for slope field
def diff(x,y):
    v = y*(3-x*y) # change to your ODE
    DX = 1/np.sqrt(1+np.power(v,2))
    DY = v/np.sqrt(1+np.power(v,2))
    return DX, DY
# Euler's method
def euler(t0,y0,tn,stepsize):
    # generate vector for t range
    t = np.arange(t0,tn,stepsize) 
    y = y0*np.ones(len(t))
    for j in range(len(t)-1):
        slope = y[j]*(3-t[j]*y[j]) # change to your ODE
        y[j+1] = y[j] + slope*stepsize
    return t, y
# Generate grid points for slope field
t = np.linspace(-5,5,51) # change limits of the graph
y = np.linspace(-5,5,51) # change limits of the graph
T1 , Y1 = np.meshgrid(t,y)
# Calculate slope at each grid point
DT1, DY1 = diff(T1, Y1)
M = (np.hypot(DT1, DY1))
# This is to avoid any divisions by 0 when normalizing
M[ M == 0] = 1.
# Normalize the length of each arrow (optional)
DT1 /= M
DY1 /= M
# Compute approximate solution
T0 = -5   # initial value t_0
Y0 = 5e-7 # initial value y_0
TN = 5    # right side limit of approximation
stepsize = 0.01 # step size for tangent line approximation
T, Y = euler(T0,Y0,TN,stepsize)        
# Plot the slope field and approximate solution
plt.figure(figsize=(12,12))
plt.quiver(T1, Y1, DT1, DY1, color='#307fe2', units='xy', scale=6., zorder=3, width=0.025, headwidth=3, headlength=3, headaxislength=3, pivot='mid')
plt.plot(T,Y,'#ec008c',linewidth=2)
plt.title(r"Slope field $y'= y(3-ty)$ with approximate solution starting at $(-5,5\times 10^{-7})$, h = 0.01")
plt.grid(True)
plt.savefig('eulermethod.svg', transparent=True, bbox_inches='tight')

plt.figure(figsize=(12,6))
Phi = (9*np.exp(3*(T+5)))/(np.exp(3*(T+5))*(3*T-1) + 1.8e7+16) # Solution to IVP
actu = plt.plot(T,Phi,'#307fe2',linewidth=2,label='Actual')
appr = plt.plot(T,Y,'#ec008c',linewidth=2,label='Approximate')
plt.grid(True)
plt.title(r"Actual and Approximate Solutions for $y'= y(3-ty)$")
plt.legend()
plt.savefig('solutioncomparison.svg', transparent=True, bbox_inches='tight')
plt.show()

Creative Commons Logo - White


Your use of this self-initiated mediated course material is subject to our Creative Commons License .


Creative Commons Attribution-NonCommercial-ShareAlike 4.0

Creative Commons Logo - Black
Attribution
You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

Creative Commons Logo - Black
Noncommercial
You may not use the material for commercial purposes.

Creative Commons Logo - Black
Share Alike
You are free to share, copy and redistribute the material in any medium or format. If you adapt, remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.