What is Euler's method?
4 min read

What is Euler's method?

What is Euler's method?

Quick explanation

Euler's method is a way of finding approximations of numerical values. Knowing a point of a function and its derivative at this point, we are able to approximate the value of the other close points, the closer the points are, the more exact the approximation will be. Perhaps this is a bit too abstract, so let's show how it's done.

Theory

Below is a representation of how Euler's method works intuitively. Let's say you have the function f(x)=e-x (crudely drawn in blue). You can estimate points on the graph (drawn in green) and find an approximation of the function f using Euler's method. The first thing needed is an initial value (initial point), in this case, we use f(0)=1 as that value, then we find the value of the slope (drawn in red, this can be done by calculating the derivative of f) at this point and trace a line with this slope going through the point, we then place another point on the line and start the process all over again.

finding points using Euler's method

Now your question may be: why would this ever be used? why can't we just find the real values of the points by plugging the x values in f(x)?
Well, in this example we have a function that can be easily expressed (f(x)=e-x). But what if f was a solution of a differential equation that is hard (or even impossible) to solve analytically? In this case, Euler's method can be used to approximately graph the function given that we know the initial conditions.

Alright now we have an idea of what Euler's method is and what it is used for, but is there a more "literal" way of implementing Euler's method? Of course, there is.
The idea is to express a point of the function as a function of a previous point (read it again). So basically, we'll have an equation with f(x+dx) alone on one side and f(x) on the other side. The way to set up an equation is to rewrite the derivatives differently and then isolate f(x+dx).

Example

If we want to reuse the same function as before as an example, we have to solve the differential equation which has f(x)=e-x as one of its solutions. Luckily for us, this is a classic differential equation that some of you might have recognized as:
df/dx + f = 0
which has for solution: f(x) = λe-x, λ∈ℝ
which basically means that λ can be any real number. Here, λ=1

Now using Euler's method, this is how we would set up the equation using the method explained above:

Now, two things are needed. First of all f(0) needs to be known (which is just f(0)=1 in this case) and then we have to choose what value to take for dx, as a general rule, a smaller dx gives more accurate values. Let's settle for dx=0.1 as that is the value I'll use in python later. Now that we have those two values, we can calculate each point:

f(0.1)=f(0)*(1-0.1)=1*0.9=0.9
f(0.2)=f(0.1)*(1-0.1)=0.9*0.9=0.81
f(0.3)=f(0.2)(1-0.1)=0.81*0.9=0.729
f(0.4)=f(0.3)*(1-0.1)=0.729*0.9=0.656
etc...

Graphing the example

Now that we were able to calculate the points manually, we might want to use a program to automate the process for us. This is the code I came up with:

import math as m
import numpy as np
import matplotlib.pyplot as plt

LAMBDA = 10
dx = 0.1

#ANALYTICAL
def f(x):
    return LAMBDA*(m.exp(-x))

x = np.linspace(0,10, 100)
y = list(map(f, x))
plt.plot(x,y, label='ANALYTICAL')


#EULER'S METHOD
x = [0]
y = [LAMBDA]
while x[-1]<10:
    x.append(x[-1]+dx)
    new_value = y[-1]*(1-dx)
    y.append(new_value)
    
plt.plot(x, y, label='EULER')

plt.legend()
plt.show()

And we get this graph:

comparison of a graph traced using the analytical function and a graph traced with Euler's method 

The difference between the two curves is only slightly visible, but if we increase dx to 0.5, the difference is more notable:

Thanks a lot for reading my article! Please consider subscribing to my newsletter, it's free and keeps me motivated!