$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$

# 8.2: Direction Fields and Numerical Methods

$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$

Learning Objectives

• Draw the direction field for a given first-order differential equation.
• Use a direction field to draw a solution curve of a first-order differential equation.
• Use Euler’s Method to approximate the solution to a first-order differential equation.

For the rest of this chapter we will focus on various methods for solving differential equations and analyzing the behavior of the solutions. In some cases it is possible to predict properties of a solution to a differential equation without knowing the actual solution. We will also study numerical methods for solving differential equations, which can be programmed by using various computer languages or even by using a spreadsheet program, such as Microsoft Excel.

## Creating Direction Fields

Direction fields (also called slope fields) are useful for investigating first-order differential equations. In particular, we consider a first-order differential equation of the form

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

An applied example of this type of differential equation appears in Newton’s law of cooling, which we will solve explicitly later in this chapter. First, though, let us create a direction field for the differential equation

$T′(t)=−0.4(T−72).$

Here $$T(t)$$ represents the temperature (in degrees Fahrenheit) of an object at time $$t$$, and the ambient temperature is $$72°F$$. Figure shows the direction field for this equation. Figure $$\PageIndex{1}$$: Direction field for the differential equation $$T′(t)=−0.4(T−72)$$. Two solutions are plotted: one with initial temperature less than $$72°F$$ and the other with initial temperature greater than $$72°F$$.

The idea behind a direction field is the fact that the derivative of a function evaluated at a given point is the slope of the tangent line to the graph of that function at the same point. Other examples of differential equations for which we can create a direction field include

$y'=3x+2y−4$

$y'=x^2−y^2$

$y'=\frac{2x+4}{y−2}.$

To create a direction field, we start with the first equation: $$y'=3x+2y−4$$. We let $$(x_0,y_0)$$ be any ordered pair, and we substitute these numbers into the right-hand side of the differential equation. For example, if we choose $$x=1$$ and $$y=2$$, substituting into the right-hand side of the differential equation yields

$$y′=3x+2y−4=3(1)+2(2)−4=3.$$

This tells us that if a solution to the differential equation $$y'=3x+2y−4$$ passes through the point $$(1,2)$$, then the slope of the solution at that point must equal 3. To start creating the direction field, we put a short line segment at the point $$(1,2)$$ having slope $$3$$. We can do this for any point in the domain of the function $$f(x,y)=3x+2y−4,$$ which consists of all ordered pairs $$(x,y)$$ in $$R^2$$. Therefore any point in the Cartesian plane has a slope associated with it, assuming that a solution to the differential equation passes through that point. The direction field for the differential equation $$y′=3x+2y−4$$ is shown in Figure. Figure $$\PageIndex{2}$$: Direction field for the differential equation $$y'=3x+2y−4$$.

We can generate a direction field of this type for any differential equation of the form $$y'=f(x,y).$$

Definition: Direction Field (Slope Field)

A direction field (slope field) is a mathematical object used to graphically represent solutions to a first-order differential equation. At each point in a direction field, a line segment appears whose slope is equal to the slope of a solution to the differential equation passing through that point.

## Using Direction Fields

We can use a direction field to predict the behavior of solutions to a differential equation without knowing the actual solution. For example, the direction field in Figure serves as a guide to the behavior of solutions to the differential equation $$y'=3x+2y−4.$$

To use a direction field, we start by choosing any point in the field. The line segment at that point serves as a signpost telling us what direction to go from there. For example, if a solution to the differential equation passes through the point $$(0,1),$$ then the slope of the solution passing through that point is given by $$y'=3(0)+2(1)−4=−2.$$ Now let $$x$$ increase slightly, say to $$x=0.1$$. Using the method of linear approximations gives a formula for the approximate value of $$y$$ for $$x=0.1.$$ In particular,

$L(x)=y_0+f′(x_0)(x−x_0)=1−2(x_0−0)=1−2x_0.$

Substituting $$x_0=0.1$$ into $$L(x)$$ gives an approximate $$y$$ value of $$0.8$$.

At this point the slope of the solution changes (again according to the differential equation). We can keep progressing, recalculating the slope of the solution as we take small steps to the right, and watching the behavior of the solution. Figure shows a graph of the solution passing through the point $$(0,1)$$. Figure $$\PageIndex{3}$$: Direction field for the differential equation $$y'=3x+2y−4$$ with the solution passing through the point $$(0,1).$$

The curve is the graph of the solution to the initial-value problem

$y'=3x+2y−4,y(0)=1.$

This curve is called a solution curve passing through the point $$(0,1).$$ The exact solution to this initial-value problem is

$y=−\frac{3}{2}x+\frac{5}{4}−\frac{1}{4}e^{2x},$

and the graph of this solution is identical to the curve in Figure

Exercise $$\PageIndex{1}$$

Create a direction field for the differential equation $$y'=x^2−y^2$$ and sketch a solution curve passing through the point $$(−1,2)$$.

Hint

Use $$x$$ and $$y$$ values ranging from $$−5$$ to $$5$$. For each coordinate pair, calculate $$y'$$ using the right-hand side of the differential equation. Go to this Java applet and this website to see more about slope fields.

Now consider the direction field for the differential equation $$y'=(x−3)(y^2−4)$$, shown in Figure. This direction field has several interesting properties. First of all, at $$y=−2$$ and $$y=2$$, horizontal dashes appear all the way across the graph. This means that if $$y=−2$$, then $$y'=0.$$ Substituting this expression into the right-hand side of the differential equation gives

$(x−3)(y^2−4)=(x−3)((−2)^2−4)=(x−3)(0)=0=y'.$

Therefore $$y=−2$$ is a solution to the differential equation. Similarly, $$y=2$$ is a solution to the differential equation. These are the only constant-valued solutions to the differential equation, as we can see from the following argument. Suppose $$y=k$$ is a constant solution to the differential equation. Then $$y′=0$$. Substituting this expression into the differential equation yields $$0=(x−3)(k^2−4)$$. This equation must be true for all values of $$x$$, so the second factor must equal zero. This result yields the equation $$k^2−4=0$$. The solutions to this equation are $$k=−2$$ and $$k=2$$, which are the constant solutions already mentioned. These are called the equilibrium solutions to the differential equation. Figure $$\PageIndex{4}$$: Direction field for the differential equation $$y'=(x−3)(y^2−4)$$ showing two solutions. These solutions are very close together, but one is barely above the equilibrium solution $$x=−2$$ and the other is barely below the same equilibrium solution.

Definition: Equilibrium Solutions

Consider the differential equation $$y'=f(x,y)$$. An equilibrium solution is any solution to the differential equation of the form $$y=c$$, where $$c$$ is a constant.

To determine the equilibrium solutions to the differential equation $$y'=f(x,y)$$, set the right-hand side equal to zero. An equilibrium solution of the differential equation is any function of the form $$y=k$$ such that $$f(x,k)=0$$ for all values of $$x$$ in the domain of $$f$$.

An important characteristic of equilibrium solutions concerns whether or not they approach the line $$y=k$$ as an asymptote for large values of $$x$$.

Definition: asymptotically Stable, Unstable and Semi-Stable Solutions

Consider the differential equation $$y′=f(x,y),$$ and assume that all solutions to this differential equation are defined for $$x≥x_0$$. Let $$y=k$$ be an equilibrium solution to the differential equation.

1. $$y=k$$ is an asymptotically stable solution to the differential equation if there exists $$ε>0$$ such that for any value $$c∈(k−ε,k+ε)$$ the solution to the initial-value problem $y′=f(x,y),y(x0)=c$ approaches $$k$$ as $$x$$ approaches infinity.
2. $$y=k$$ is an asymptotically unstable solution to the differential equation if there exists $$ε>0$$ such that for any value $$c∈(k−ε,k+ε)$$ the solution to the initial-value problem $y′=f(x,y),y(x0)=c$ never approaches $$k$$ as $$x$$ approaches infinity.
3. $$y=k$$ is an asymptotically semi-stable solution to the differential equation if it is neither asymptotically stable nor asymptotically unstable.

Now we return to the differential equation $$y'=(x−3)(y^2−4)$$, with the initial condition $$y(0)=0.5$$. The direction field for this initial-value problem, along with the corresponding solution, is shown in Figure. Figure $$\PageIndex{5}$$: Direction field for the initial-value problem $$y'=(x−3)(y^2−4),y(0)=0.5.$$

The values of the solution to this initial-value problem stay between $$y=−2$$ and $$y=2$$, which are the equilibrium solutions to the differential equation. Furthermore, as $$x$$ approaches infinity, $$y$$ approaches $$2$$. The behavior of solutions is similar if the initial value is higher than $$2$$, for example, $$y(0)=2.3$$. In this case, the solutions decrease and approach $$y=2$$ as $$x$$ approaches infinity. Therefore $$y=2$$ is an asymptotically stable solution to the differential equation.

What happens when the initial value is below $$y=−2$$? This scenario is illustrated in Figure, with the initial value $$y(0)=−3.$$ Figure $$\PageIndex{6}$$: Direction field for the initial-value problem $$y'=(x−3)(y^2−4),y(0)=−3.$$

The solution decreases rapidly toward negative infinity as $$x$$ approaches infinity. Furthermore, if the initial value is slightly higher than $$−2$$, then the solution approaches $$2$$, which is the other equilibrium solution. Therefore in neither case does the solution approach $$y=−2$$, so $$y=−2$$ is called an asymptotically unstable, or unstable, equilibrium solution.

Example $$\PageIndex{1}$$: Stability of an Equilibrium Solution

Create a direction field for the differential equation $$y'=(y−3)^2(y^2+y−2)$$ and identify any equilibrium solutions. Classify each of the equilibrium solutions as stable, unstable, or semi-stable.

Solution

The direction field is shown in Figure. Figure $$\PageIndex{7}$$: Direction field for the differential equation $$y'=(y−3)^2(y^2+y−2).$$

The equilibrium solutions are $$y=−2,y=1,$$ and $$y=3$$. To classify each of the solutions, look at an arrow directly above or below each of these values. For example, at $$y=−2$$ the arrows directly below this solution point up, and the arrows directly above the solution point down. Therefore all initial conditions close to $$y=−2$$ approach $$y=−2$$, and the solution is stable. For the solution $$y=1$$, all initial conditions above and below $$y=1$$ are repelled (pushed away) from $$y=1$$, so this solution is unstable. The solution $$y=3$$ is semi-stable, because for initial conditions slightly greater than $$3$$, the solution approaches infinity, and for initial conditions slightly less than $$3$$, the solution approaches $$y=1$$.

Analysis

It is possible to find the equilibrium solutions to the differential equation by setting the right-hand side equal to zero and solving for $$y.$$ This approach gives the same equilibrium solutions as those we saw in the direction field.

Exercise $$\PageIndex{2}$$

Create a direction field for the differential equation $$y'=(x+5)(y+2)(y^2−4y+4)$$ and identify any equilibrium solutions. Classify each of the equilibrium solutions as stable, unstable, or semi-stable.

Hint

First create the direction field and look for horizontal dashes that go all the way across. Then examine the slope lines directly above and below the equilibrium solutions.

The equilibrium solutions are $$y=−2$$ and $$y=2$$. For this equation, $$y=−2$$ is an unstable equilibrium solution, and $$y=2$$ is a semi-stable equilibrium solution. ## Euler’s Method

Consider the initial-value problem

$y′=2x−3,y(0)=3.$

Integrating both sides of the differential equation gives $$y=x^2−3x+C,$$ and solving for $$C$$ yields the particular solution $$y=x^2−3x+3.$$ The solution for this initial-value problem appears as the parabola in Figure. Figure $$\PageIndex{8}$$: Euler’s Method for the initial-value problem $$y′=2x−3,y(0)=3.$$

The red graph consists of line segments that approximate the solution to the initial-value problem. The graph starts at the same initial value of $$(0,3)$$. Then the slope of the solution at any point is determined by the right-hand side of the differential equation, and the length of the line segment is determined by increasing the $$x$$ value by $$0.5$$ each time (the step size). This approach is the basis of Euler’s Method.

Before we state Euler’s Method as a theorem, let’s consider another initial-value problem:

$y′=x^2−y^2,y(−1)=2.$

The idea behind direction fields can also be applied to this problem to study the behavior of its solution. For example, at the point $$(−1,2),$$ the slope of the solution is given by $$y'=(−1)^2−2^2=−3$$, so the slope of the tangent line to the solution at that point is also equal to $$−3$$. Now we define $$x_0=−1$$ and $$y_0=2$$. Since the slope of the solution at this point is equal to $$−3$$, we can use the method of linear approximation to approximate y near $$(−1,2)$$.

$L(x)=y_0+f′(x_0)(x−x_0).$

Here $$x_0=−1,y_0=2,$$ and $$f′(x_0)=−3,$$ so the linear approximation becomes

$L(x)=2−3(x−(−1))=2−3x−3=−3x−1.$

Now we choose a step size. The step size is a small value, typically $$0.1$$ or less, that serves as an increment for $$x$$; it is represented by the variable $$h$$. In our example, let $$h=0.1$$. Incrementing $$x_0$$ by $$h$$ gives our next $$x$$ value:

$x_1=x_0+h=−1+0.1=−0.9.$

We can substitute $$x_1=−0.9$$ into the linear approximation to calculate $$y_1$$.

$y_1=L(x_1)=−3(−0.9)−1=1.7.$

Therefore the approximate $$y$$ value for the solution when $$x=−0.9$$ is $$y=1.7$$. We can then repeat the process, using $$x_1=−0.9$$ and $$y_1=1.7$$ to calculate $$x_2$$ and $$y_2$$. The new slope is given by $$y'=(−0.9)^2−(1.7)^2=−2.08.$$ First, $$x_2=x_1+h=−0.9+0.1=−0.8.$$ Using linear approximation gives

$$L(x)=y_1+f′(x_1)(x−x_1)$$

$$=1.7−2.08(x−(−0.9))$$

$$=1.7−2.08x−1.872$$

$$=−2.08x−0.172$$.

Finally, we substitute $$x_2=−0.8$$ into the linear approximation to calculate $$y_2$$.

$$y_2=L(x_2)$$

$$=−2.08x_2−0.172$$

$$=−2.08(−0.8)−0.172$$

$$=1.492.$$

Therefore the approximate value of the solution to the differential equation is $$y=1.492$$ when $$x=−0.8.$$

What we have just shown is the idea behind Euler’s Method. Repeating these steps gives a list of values for the solution. These values are shown in Table, rounded off to four decimal places.

$$n$$ $$x_n$$ $$y_n$$ $$n$$ $$x_n$$ $$y_n$$ 0 1 2 3 4 5 −1 −0.9 −0.8 −0.7 −0.6 −0.5 2 1.7 1.492 1.3334 1.2046 1.0955 6 7 8 9 10 −0.4 −0.3 −0.2 −0.1 0 1.0004 1.9164 1.8414 1.7746 1.7156

Euler’s method

Consider the initial-value problem

$y'=f(x,y),y(x_0)=y_0.$

To approximate a solution to this problem using Euler’s method, define

$$x_n=x_0+nh$$

$$y_n=y_{n−1}+hf(x_{n−1},y_{n−1})$$.

Here $$h>0$$ represents the step size and $$n$$ is an integer, starting with $$1$$. The number of steps taken is counted by the variable $$n$$.

Typically $$h$$ is a small value, say $$0.1$$ or $$0.05$$. The smaller the value of $$h$$, the more calculations are needed. The higher the value of $$h$$, the fewer calculations are needed. However, the tradeoff results in a lower degree of accuracy for larger step size, as illustrated in Figure. Figure $$\PageIndex{9}$$: Euler’s method for the initial-value problem $$y′=2x−3,y(0)=3$$ with (a) a step size of $$h=0.5$$; and (b) a step size of $$h=0.25.$$

Example $$\PageIndex{2}$$: Using Euler’s Method

Consider the initial-value problem

$y′=3x^2−y^2+1,y(0)=2.$

Use Euler’s method with a step size of $$0.1$$ to generate a table of values for the solution for values of $$x$$ between $$0$$ and $$1$$.

Solution

We are given $$h=0.1$$ and $$f(x,y)=3x^2−y^2+1.$$ Furthermore, the initial condition $$y(0)=2$$ gives $$x_0=0$$ and $$y_0=2$$. Using Equation with $$n=0$$, we can generate Table.

Using Euler’s Method to Approximate Solutions to a Differential Equation
$$n$$ $$x_n$$ $$y_n=y_{n−1}+hf(x_{n−1},y_{n−1})$$
0 0 2
1 0.1 $$y_1=y_0+hf(x_0,y_0)=1.7$$
2 0.2 $$y_2=y_1+hf(x_1,y_1)=1.514$$
3 0.3 $$y_3=y_2+hf(x_2,y_2)=1.3968$$
4 0.4 $$y_4=y_3+hf(x_3,y_3)=1.3287$$
5 0.5 $$y_5=y_4+hf(x_4,y_4)=1.3001$$
6 0.6 $$y_6=y_5+hf(x_5,y_5)=1.3061$$
7 0.7 $$y_7=y_6+hf(x_6,y_6)=1.3435$$
8 0.8 $$y_8=y_7+hf(x_7,y_7)=1.4100$$
9 0.9 $$y_9=y_8+hf(x_8,y_8)=1.5032$$
10 1.0 $$y_{10}=y_9+hf(x_9,y_9)=1.6202$$

With ten calculations, we are able to approximate the values of the solution to the initial-value problem for values of $$x$$ between $$0$$ and $$1$$.

Exercise $$\PageIndex{3}$$

Consider the initial-value problem

$y′=x^3+y^2,y(1)=−2.$

Using a step size of $$0.1$$, generate a table with approximate values for the solution to the initial-value problem for values of $$x$$ between $$1$$ and $$2$$.

Hint

Start by identifying the value of $$h$$, then figure out what $$f(x,y)$$ is. Then use the formula for Euler’s Method to calculate $$y_1,y_2,$$ and so on.

 $$n$$ $$x_n) \( y_n=y_{n−1}+hf(x_{n−1},y_{n−1})$$ 0 1 −2 1 1.1 $$y_1=y_0+hf(x_0,y_0)=−1.5$$ 2 1.2 $$y_2=y_1+hf(x_1,y_1)=−1.1419$$ 3 1.3 $$y_3=y_2+hf(x_2,y_2)=−0.8387$$ 4 1.4 $$y_4=y_3+hf(x_3,y_3)=−0.5487$$ 5 1.5 $$y_5=y_4+hf(x_4,y_4)=−0.2442$$ 6 1.6 $$y_6=y_5+hf(x_5,y_5)=0.0993$$ 7 1.7 $$y_7=y_6+hf(x_6,y_6)=0.5099$$ 8 1.8 $$y_8=y_7+hf(x_7,y_7)=1.0272$$ 9 1.9 $$y_9=y_8+hf(x_8,y_8)=1.7159$$ 10 2 $$y_{10}=y_9+hf(x_9,y_9)=2.6962$$

Visit this website for a practical application of the material in this section.

## Key Concepts

• A direction field is a mathematical object used to graphically represent solutions to a first-order differential equation.
• Euler’s Method is a numerical technique that can be used to approximate solutions to a differential equation.

## Key Equations

• Euler’s Method

$$x_n=x_0+nh$$

$$y_n=y_{n−1}+hf(x_{n−1},y_{n−1})$$ ,where $$h$$ is the step size

## Glossary

asymptotically semi-stable solution
$$y=k$$ if it is neither asymptotically stable nor asymptotically unstable
asymptotically stable solution
$$y=k$$ if there exists $$ε>0$$ such that for any value $$c∈(k−ε,k+ε)$$ the solution to the initial-value problem $$y′=f(x,y),y(x_0)=c$$ approaches $$k$$ as $$x$$ approaches infinity
asymptotically unstable solution
$$y=k$$ if there exists $$ε>0$$ such that for any value $$c∈(k−ε,k+ε)$$ the solution to the initial-value problem $$y′=f(x,y),y(x_0)=c$$ never approaches $$k$$ as $$x$$approaches infinity
direction field (slope field)
a mathematical object used to graphically represent solutions to a first-order differential equation; at each point in a direction field, a line segment appears whose slope is equal to the slope of a solution to the differential equation passing through that point
equilibrium solution
any solution to the differential equation of the form $$y=c,$$ where $$c$$ is a constant
Euler’s Method
a numerical technique used to approximate solutions to an initial-value problem
solution curve
a curve graphed in a direction field that corresponds to the solution to the initial-value problem passing through a given point in the direction field
step size
the increment hh that is added to the xx value at each step in Euler’s Method