How to Solve Differential Equations Using Euler’s Method: A Complete Tutorial

How to Solve Differential Equations Using Euler’s Method: A Complete Tutorial

Learning how to solve differential equations using Euler’s Method provides a way to approximate solutions when finding an exact solution is difficult or impossible. Many differential equations cannot be solved analytically. Numerical methods such as Euler’s Method provide another approach. Instead of finding an exact formula for the solution, we can calculate a sequence of approximate values that closely follows the actual solution.

In this tutorial, we will develop Euler’s Method from the idea of tangent-line approximations and explain the formula behind the method. We will examine when the method succeeds and when it can fail, discuss how to calculate numerical error, and work through several examples step by step. By the end, you will have an understanding of not only how to apply Euler’s Method, but also why it works and how to evaluate the accuracy of its approximations.

What Is Euler’s Method?

Euler’s method is a numerical technique used to approximate solutions to first-order ordinary differential equations. Instead of trying to find an exact formula for the solution, Euler’s Method uses information about the slope of the solution at known points to estimate where the solution will be at nearby points. By repeating this process, we can build an approximation to the solution over an interval.

Definition of Euler’s Method

Suppose we have a first-order differential equation written as

$$\frac{dy}{dx}=f(x,y)$$

along with an initial condition

$$y(x_0) = y_0.$$

The initial condition gives us a known point \( (x_0, y_0) \) on the solution curve. The goal of Euler’s Method is to use this starting point to approximate the value of \( y \) at other values of \( x \).

To do this, we choose a step size \( h \). The step size determines how far we move along the \( x \)-axis at each iteration. The next \( x \)-value is calculated using

$$x_{n + 1} = x_n + h.$$

At the point \( (x_n, y_n) \), the differential equation tells us the slope of the solution:

$$\frac{dy}{dx}=f(x_n, y_n).$$

We can then use this slope to estimate the next \( y \)-value:

$$y_{n+1} = y_n + h f(x_n, y_n).$$

This is the formula for Euler’s Method.

The Euler’s Method Formula

Combining the two equations gives the basic Euler iteration:

$$y_{n + 1} = y_n + h f(x_n, y_n)$$

where \( x_n \) is the current \( x \)-value, \( y_n \) is the current approximation to the solution, \( h \) is the step size, and \( f(x_n, y_n) \) is the slope of the solution at \( (x_n, y_n) \).

After calculating \( y_{n + 1} \), we move to the next point and repeat the process. In other words, each approximation becomes the starting point for the next approximation.

The process can be summarized as

$$(x_n, y_n) \longrightarrow (x_{n + 1}, y_{n + 1}) \longrightarrow (x_{n + 2}, y_{n + 2}) \longrightarrow \cdots$$

As we continue taking steps, the resulting points form an approximate solution to the differential equation.

The Geometric Interpretation

The formula for Euler’s Method becomes easier to understand when we look at its geometric meaning.

Recall that the derivative \( \frac{dy}{dx} \) represents the slope of a curve. Therefore, when we evaluate

$$f(x_n, y_n),$$

we are finding the slope of the solution curve at the current point \( (x_n, y_n) \).

We can use this slope to construct the tangent line at that point. Moving a horizontal distance \( h \) along this tangent line gives an estimate of the solution at \( x_{n+1} \).

In effect, Euler’s Method replaces a small portion of the curved solution with a straight-line approximation. We then repeat the process using the new point.

This can be viewed as repeatedly following tangent lines: The smaller the step size, the shorter each tangent-line approximation becomes. This generally allows the numerical solution to follow the actual solution more closely. For a review of tangent lines, please refer to the article Secant and Tangent Lines in Calculus: A Complete Beginner’s Guide.

Why Does Euler’s Method Work?

Euler’s method works because a differentiable function can be approximated locally by its tangent line. Since a differential equation gives us information about the slope of its solution, we can use that slope to estimate how the solution changes over a small interval. By repeatedly applying this idea, Euler’s Method produces a sequence of points that approximates the actual solution curve.

If you need a refresher on derivatives, please refer to the article How to Differentiate a Function Step by Step: A Beginner’s Guide before continuing.

The Connection to Tangent Lines

Recall that the derivative of a function represents its instantaneous rate of change. If \( y=f(x) \), then

$$\frac{dy}{dx} = f'(x)$$

gives the slope of the tangent line at a particular point.

Suppose we know the solution at \( x = x_n \), so that the solution passes through the point \( (x_n, y_n) \). If the differential equation is

$$\frac{dy}{dx} = f(x,y),$$

then the slope of the solution at \( (x_n, y_n) \) is

$$f(x_n, y_n).$$

The tangent line at this point can therefore be used to estimate the value of the solution a short distance away.

If we move a distance \( h \) along the \( x \)-axis, the change in \( x \) is

$$\Delta x = h.$$

Since slope is defined as

$$m = \frac{\Delta y}{\Delta x},$$

we can estimate the change in \( y \) by multiplying the slope by the change in \( x \):

$$\Delta y \approx h f(x_n, y_n).$$

Therefore, the next \( y \)-value can be approximated by

$$y_{n+1} \approx y_n + h f(x_n, y_n).$$

This gives the formula for Euler’s Method:

$$y_{n + 1} = y_n + h f(x_n, y_n).$$

The formula is therefore not arbitrary. It comes directly from using a tangent line to approximate a solution curve.

Repeated Tangent-Line Approximations

When using Euler’s Method, we do not use one tangent line to approximate the entire solution. Instead, we use a new tangent line at every step.

We begin with a known point

$$(x_0, y_0).$$

Using the differential equation, we calculate the slope at this point. We then use that slope to estimate the next point:

$$(x_1, y_1).$$

At the new point, we calculate a new slope and use it to estimate another point:

$$(x_2, y_2).$$

The process continues.

Each line segment provides a local approximation to the solution curve. The collection of these line segments forms the numerical approximation to the solution.

Why Does a Smaller Step Size Usually Improve the Approximation?

The tangent line provides a good approximation near the point where it touches the curve. However, as we move farther away from that point, the tangent line can begin to differ significantly from the actual solution.

A smaller step size keeps each approximation closer to the point where the slope was calculated. For example, using a step size of \( h=0.1 \) means that each tangent-line approximation is used over a shorter interval than a step size of \( h = 0.5 \).

Because the solution curve is generally better approximated over shorter intervals, decreasing the step size usually improves the accuracy of Euler’s Method.

However, there is a tradeoff. A smaller step size requires more iterations to travel across the same interval. Thus, improving accuracy generally requires additional calculations.

Euler’s method works by converting local slope information into a sequence of approximate points. When the step size is sufficiently small, and the problem behaves well numerically, these points can provide a useful approximation to the true solution.

When Does Euler’s Method Succeed?

Euler’s method does not need to produce an exact answer to be successful. In most applications, success means that the numerical approximation is sufficiently close to the true solution for the purpose at hand.

Smooth and Continuous Functions

If the function \( f(x, y) \) is smooth throughout the interval, the tangent-line approximations used by Euler’s Method can generally follow the solution reasonably well. When the slope changes gradually, the slope at the beginning of each step provides a better approximation to the behavior of the solution over that step.

The smoother the function, the more reasonable it is to expect the tangent-line approximations to remain close to the actual solution.

For a refresher on smooth functions, please refer to the articles Newton’s Method Explained for Beginners and The Ultimate Resource for Understanding the Secant Method.

For a review of continuous functions, please refer to the articles A Complete Introduction to the Bisection Method with Worked Examples and Solutions and How to Solve Equations Using Fixed Point Iteration: A Step-by-Step Guide.

When the Step Size Is Small

As the step size \( h \) decreases, each tangent-line approximation covers a smaller portion of the solution curve, generally making the numerical approximation more accurate.

When Does Euler’s Method Fail?

Euler’s method does not always produce a good approximation. Even when the calculations are performed correctly, the numerical solution can become inaccurate.

When the Step Size Is Too Large

Recall that Euler’s Method uses the slope at the beginning of each step to estimate the solution at the next point. If the step covers too much of the interval, the solution may change substantially before the next slope is calculated.

Reducing the step size can often improve the approximation.

When the Equation Is Stiff

Euler’s method can have particular difficulty with stiff differential equations, equations whose solutions grow or decay rapidly.

A stiff differential equation can contain components that change at very different rates. For these problems, Euler’s Method may require an extremely small step size to remain stable. Although reducing \( h \) may improve the approximation, doing so can require a very large number of calculations.

The important point is that Euler’s Method does not fail simply because it is a simple method. It fails when its assumptions about the behavior of the solution are no longer good enough for the problem being solved. Understanding these limitations helps us determine when to reduce the step size and when to consider a different numerical method.

Error Computation

Euler’s method provides an approximation to the solution of a differential equation rather than the exact solution. Because of this, it is important to understand how much the numerical approximation differs from the true solution. The error tells us how accurate our approximation is and provides a way to evaluate the performance of Euler’s Method.

There are several ways to measure error. The most common are absolute error, relative error, and percent error. When an exact solution is available, we can calculate these quantities directly. We can also examine the local and global truncation errors associated with the method.

Absolute Error

Suppose the exact value of the solution at a particular point is \( y_{\text{exact}} \), while Euler’s Method gives the approximation \( y_{\text{approx}} \).

The absolute error is defined as

$$\epsilon = |y_{\text{exact}} – y_{\text{approx}}|.$$

Relative Error

Absolute error tells us the size of the difference between the exact and approximate values, but it does not tell us how large that error is relative to the exact solution.

The relative error is

$$\epsilon = \frac{|y_{\text{exact}} – y_{\text{approx}}|}{|y_{\text{exact}}|}.$$

The relative error is useful when comparing errors for solutions that have very different magnitudes.

Percent Error

The relative error can be converted into a percentage by multiplying by \( 100% \):

$$\epsilon = \frac{|y_{\text{exact}} – y_{\text{approx}}|}{|y_{\text{exact}}|} \times 100%.$$

Local Truncation Error

The errors discussed above compare the numerical approximation with the exact solution. Numerical analysis also distinguishes between local truncation error and global truncation error.

The local truncation error measures the error introduced by a single Euler step when the calculation begins from the exact solution at the current point.

To understand where this error comes from, consider the Taylor expansion of the exact solution:

$$y(x + h) = y(x) + hy'(x) + \frac{h^2}{2}y”(x) + O(h^3).$$

Euler’s method uses only the first two terms:

$$y(x + h) \approx y(x) + hy'(x).$$

Therefore, the omitted terms in the Taylor expansion are responsible for the local truncation error.

For a review of Taylor series, please refer to the articles How to Calculate Limits in Calculus: Everything You Need to Know and Finite Difference Method for Numerical Differentiation: A Step-by-Step Guide.

For Euler’s Method, the local truncation error per step is on the order of

$$O(h^2).$$

This means that reducing the step size makes the error introduced during each step decrease rapidly.

Global Truncation Error

Although the local truncation error is \( O(h^2) \), Euler’s Method takes many steps to cover an interval, the errors introduced at individual steps can accumulate.

The resulting global truncation error is on the order of

$$O(h).$$

This is an important property of Euler’s Method. It means that Euler’s Method is a first-order numerical method.

Step-by-Step Procedure

Euler’s method approximates the solution of a first-order differential equation when an initial condition is known. By repeating the calculation, we can approximate the solution at any desired point within an interval.

Step 1: Write the Differential Equation in Standard Form

The first step is to write the differential equation in the form

$$\frac{dy}{dx} = f(x, y).$$

Step 2: Identify the Initial Condition

Next, identify the initial condition.

Suppose we are given

$$y(x_0) = y_0.$$

This tells us that the solution passes through the point

$$(x_0, y_0).$$

This is where the approximations begin.

Step 3: Determine the Step Size

The step size tells us how far we move along the \( x \)-axis during each iteration. If we want to approximate the solution from \( x = x_0 \) to \( x = x_{\text{final}} \) using \( n \) equally spaced steps, then

$$h = \frac{x_{\text{final}} – x_0}{n}.$$

Step 4: Calculate the Slope

Once you have the initial point and step size, calculate the slope at the current point.

The slope is given by

$$f(x_n, y_n).$$

Step 5: Calculate the Next \( x \)-Value

The next \( x \)-value is found by adding the step size to the current \( x \)-value:

$$x_{n + 1} = x_n + h.$$

Step 6: Calculate the Next \( y \)-Value

Now use the formula to calculate the next approximation:

$$y_{n + 1}= y_n + h f(x_n, y_n).$$

Step 7: Repeat the Process

The newly calculated point becomes the starting point for the next step. We continue this process until we reach the desired value of \( x \).

With this procedure in place, we can now apply Euler’s Method to complete differential equation problems and see how the calculations work in practice.

Worked Out Examples

The first example shows how to use Euler’s Method. In this example, we will be using techniques covered in the article A Step-by-Step Tutorial on Linear Differential Equations with Examples and Solutions.

Example 1: Use Euler’s Method to approximate the solution of \( \frac{dy}{dx} = x + y \), subject to the initial condition \( y(0) = 1 \) at \( x = 0.5 \). Use a step size of \( h = 0.1 \) and find the absolute error at \( x = 0.5 \).

Solution: Consider the function

$$f(x, y) = x + y.$$

Evaluating the function at \( (x_0, y_0) = (0, 1) \) we obtain

$$f(0, 1) = 0 + 1.$$

Adding we find

$$f(0, 1) = 1.$$

The next \( x \)-value is

$$x_1 = 0 + 0.1.$$

Adding gives

$$x_1 = 0.1.$$

The next \( y \)-value is

$$y_1 = 1 + 0.1(1).$$

Multiplying we get

$$y_1 = 1 + 0.1.$$

Adding gives

$$y_1 = 1.1.$$

Evaluating the function at \( (x_1, y_1) = (0.1, 1.1) \) we obtain

$$f(0.1, 1.1) = 0.1 + 1.1.$$

Adding we find

$$f(0.1, 1.1) = 1.2.$$

The next \( x \)-value is

$$x_2 = 0.1 + 0.1.$$

Adding gives

$$x_2 = 0.2.$$

The next \( y \)-value is

$$y_2 = 1.1 + 0.1(1.2).$$

Multiplying we get

$$y_2 = 1.1 + 0.12.$$

Adding gives

$$y_2 = 1.22.$$

Evaluating the function at \( (x_2, y_2) = (0.2, 1.22) \) we obtain

$$f(0.2, 1.22) = 0.2 + 1.22.$$

Adding we find

$$f(0.2, 1.22) = 1.42.$$

The next \( x \)-value is

$$x_3 = 0.2 + 0.1.$$

Adding gives

$$x_3 = 0.3.$$

The next \( y \)-value is

$$y_3 = 1.22 + 0.1(1.42).$$

Multiplying we get

$$y_3 = 1.22 + 0.142.$$

Adding gives

$$y_3 = 1.362.$$

Evaluating the function at \( (x_3, y_3) = (0.3, 1.362) \) we obtain

$$f(0.3, 1.362) = 0.3 + 1.362.$$

Adding we find

$$f(0.3, 1.362) = 1.662.$$

The next \( x \)-value is

$$x_4 = 0.3 + 0.1.$$

Adding gives

$$x_4 = 0.4.$$

The next \( y \)-value is

$$y_4 = 1.362 + 0.1(1.662).$$

Multiplying we get

$$y_4 = 1.362 + 0.1662.$$

Adding gives

$$y_4 = 1.5282.$$

Evaluating the function at \( (x_4, y_4) = (0.4, 1.5282) \) we obtain

$$f(0.4, 1.5282) = 0.4 + 1.5282.$$

Adding we find

$$f(0.4, 1.5282) = 1.9282.$$

The next \( x \)-value is

$$x_5 = 0.4 + 0.1.$$

Adding gives

$$x_5 = 0.5.$$

The next \( y \)-value is

$$y_5 = 1.5282 + 0.1(1.9282).$$

Multiplying we get

$$y_5 = 1.5282 + 0.19282).$$

Adding gives

$$y_5 = 1.72102.$$

Subtracting y from both sides of the differential equation gives

$$\frac{dy}{dx} – y = x.$$

This is a linear differential equation in standard form. The integrating factor is

$$\mu(x) = e^{-\int dx}.$$

Integrating we get

$$\mu(x) = e^{-x}.$$

Multiplying each term of the equation by the integrating factor, we obtain

$$e^{-x}\frac{dy}{dx} – e^{-x}y = xe^{-x}.$$

Rewriting the left-hand side as a product rule, this becomes

$$\frac{d}{dx}(e^{-x}y) = xe^{-x}.$$

Integrating both sides, we find

$$\int \frac{d}{dx}(e^{-x}y) dx = \int xe^{-x} dx.$$

To evaluate the integral on the right-hand side, choose \( u = x \) and \( dv = e^{-x}dx \), then \( du = dx \) and \( v = -e^{-x} \). Applying the integration by parts formula then gives

$$\int \frac{d}{dx}(e^{-x}y) dx = -xe^{-x} + \int e^{-x}dx.$$

Therefore,

$$e^{-x}y = -xe^{-x} – e^{-x} + C.$$

Multiplying both sides of the equation by \( e^x \) we obtain

$$e^xe^{-x}y = -xe^xe^{-x} – e^xe^{-x} + Ce^x.$$

Using exponential properties, we find

$$e^{x – x}y = -xe^{x – x} – e^{x – x} + Ce^x.$$

Simplifying this is equivalent to

$$e^0y = -xe^0 – e^0 + Ce^x.$$

Hence,

$$1y = -1x – 1 + Ce^x.$$

Our solution is then

$$y = -x – 1 + Ce^x.$$

Applying the initial condition, we get

$$y(0) = -0 – 1 + Ce^0.$$

Multiplying then gives

$$y(0) = 0 – 1 + Ce^0.$$

This is equivalent to

$$y(0) = 0 – 1 + 1C.$$

Hence,

$$y(0) = 0 – 1 + C.$$

Simplifying, we find

$$y(0) = -1 + C.$$

By the initial condition, this is equivalent to

$$1 = -1 + C.$$

Which implies

$$2 = C.$$

Substituting into the solution then gives

$$y = -x – 1 + 2e^x.$$

Evaluating the function at \( x = 0.5 \) we obtain

$$y(0.5) = -0.5 – 1 + 2e^{0.5}.$$

Exponentiating this is

$$y(0.5) = -0.5 – 1 + 2(1.648721).$$

Multiplying we get

$$y(0.5) = -0.5 – 1 + 3.297442.$$

Adding and subtracting gives

$$y(0.5) = 1.797442.$$

The absolute error is then

$$\epsilon = |1.797442 – 1.72102|.$$

Subtracting gives

$$\epsilon = |0.076422|.$$

Taking the absolute value, we arrive at a final answer of

$$\epsilon = 0.076422.$$

The next example illustrates one of the primary applications of numerical analysis, namely solving differential equations that cannot be solved analytically.

Example 2: Use Euler’s Method t o approximate the solution of \( \frac{dy}{dx} = \sin{x} + e^y \), subject to the initial condition \( y(0) = 0 \) at \( x = 1 \). Use a step size of \( h = 0.2 \).

Solution: Consider the function

$$f(x, y) = \sin{x} + e^y.$$

Evaluating the function at \( (x_0, y_0) = (0, 0) \) we obtain

$$f(0, 0) = \sin{0} + e^0.$$

Evaluating the sine and exponential gives

$$f(0, 0) = 0 + 1.$$

Adding we find

$$f(0, 0) = 1.$$

The next \( x \)-value is

$$x_1 = 0 + 0.2.$$

Adding gives

$$x_1 = 0.2.$$

The next \( y \)-value is

$$y_1 = 0 + 0.2(1).$$

Multiplying we get

$$y_1 = 0 + 0.2.$$

Adding gives

$$y_1 = 0.2.$$

Evaluating the function at \( (x_1, y_1) = (0.2, 0.2) \) we obtain

$$f(0.2, 0.2) = \sin{0.2} + e^{0.2}.$$

Evaluating the sine and exponential gives

$$f(0.2, 0.2) = 0.198669 + 1.221402.$$

Adding we find

$$f(0.2, 0.2) = 1.420072.$$

The next \( x \)-value is

$$x_2 = 0.2 + 0.2.$$

Adding gives

$$x_2 = 0.4.$$

The next \( y \)-value is

$$y_2 = 0.2 + 0.2(1.420072).$$

Multiplying we get

$$y_2 = 0.2 + 0.284014.$$

Adding gives

$$y_2 = 0.484014.$$

Evaluating the function at \( (x_2, y_2) = (0.4, 0.484014) \) we obtain

$$f(0.4, 0.484014) = \sin{0.4} + e^{0.484014}.$$

Evaluating the sine and exponential gives

$$f(0.4, 0.484014) = 0.389418 + 1.622575.$$

Adding we find

$$f(0.4, 0.484014) = 2.011993.$$

The next \( x \)-value is

$$x_3 = 0.4 + 0.2.$$

Adding gives

$$x_3 = 0.6.$$

The next \( y \)-value is

$$y_3 = 0.484014 + 0.2(2.011993).$$

Multiplying we get

$$y_3 = 0.484014 + 0.402398.$$

Adding gives

$$y_3 = 0.886413.$$

Evaluating the function at \( (x_3, y_3) = (0.6, 0.886413) \) we obtain

$$f(0.6, 0.886413) = \sin{0.6} + e^{0.886413}.$$

Evaluating the sine and exponential gives

$$f(0.6, 0.886413) = 0.564642 + 2.426410.$$

Adding we find

$$f(0.6, 0.886413) = 2.991053.$$

The next \( x \)-value is

$$x_4 = 0.6 + 0.2.$$

Adding gives

$$x_4 = 0.8.$$

The next \( y \)-value is

$$y_4 = 0.886413 + 0.2(2.991053).$$

Multiplying we get

$$y_4 = 0.886413 + 0.598210.$$

Adding gives

$$y_4 = 1.484623.$$

Evaluating the function at \( (x_4, y_4) = (0.8, 1.484623) \) we obtain

$$f(0.8, 1.484623) = \sin{0.8} + e^{1.484623}.$$

Evaluating the sine and exponential gives

$$f(0.8, 1.484623) = 0.717356 + 4.413304.$$

Adding we find

$$f(0.8, 1.484623) = 5.130660.$$

The next \( x \)-value is

$$x_5 = 0.8 + 0.2.$$

Adding gives

$$x_5 = 1.$$

The next \( y \)-value is

$$y_5 = 1.484623 + 0.2(5.130660).$$

Multiplying we get

$$y_5 = 1.484623 + 1.026132.$$

Adding gives a final answer of

$$y_5 = 2.510755.$$

Conclusion

Learning how to solve differential equations using Euler’s Method introduces numerical methods for first-order ordinary differential equations. Rather than finding an explicit formula, Euler’s Method uses the slope of the solution at known points to estimate the solution at nearby points. Repeating this process allows us to build a numerical approximation over an interval.

In this tutorial, we saw that Euler’s Method is based on tangent-line approximations. At each step, the differential equation provides the slope, which is used to estimate the next point on the solution curve. The step size plays an important role in the accuracy of the approximation: smaller step sizes generally produce more accurate results but require more calculations.

We also examined situations in which Euler’s Method succeeds and situations in which it can fail. The method tends to work well for smooth functions when an appropriate step size is used. However, large step sizes can make the approximation unreliable. Stiff differential equations can present an additional challenge because maintaining accuracy may require an impractically small step size.

Error analysis provides another important way to evaluate a numerical solution. When an exact solution is available, we can calculate the absolute, relative, and percentage errors. We also saw that Euler’s Method has a local truncation error of order O(h^2) and a global truncation error of order O(h), making it a first-order numerical method.

Further Reading

Simpson’s Rule Explained Step by Step: Everything You Need to Know – Numerical integration methods use step sizes just like Euler’s Method. Simpson’s Rule is the most accurate of these methods.

Frequently Asked Questions

The step size should be small enough to provide the desired accuracy. If the solution changes rapidly or has significant curvature, a smaller step size may be necessary.

A useful way to evaluate the choice of step size is to repeat the calculation with a smaller value of \( h \) and compare the results.