TExcelTAD
Home › Differential Equations › Slope Fields

Slope Fields

For a first-order equation y′=f(x,y)y' = f(x, y) you can see the solutions before solving anything. At every point (x,y)(x, y) the equation tells you the slope a solution through that point must have. Draw a short segment with that slope at many points and you get a slope field (or direction field). Solutions are the curves that follow the segments.

Reading the equation as slopes

Take y′=x−yy' = x - y. At (0,0)(0, 0) the slope is 0, at (2,0)(2, 0) it is 2, at (0,2)(0, 2) it is −2-2. Along the line y=xy = x every slope is 0: such a curve of constant slope is an isocline. Along y=x−1y = x - 1 every slope is x−(x−1)=1x - (x - 1) = 1, the same as the slope of that line itself, so y=x−1y = x - 1 is a solution: it follows its own segments.

python · try it yourself
import numpy as np

f = lambda x, y: x - y
xs = np.arange(-2, 3)
print("slopes of y' = x - y (rows y = 2 .. -2, columns x = -2 .. 2)")
for yv in range(2, -3, -1):
    print(f"y = {yv:2d}: " + " ".join(f"{f(xv, yv):3d}" for xv in xs))
slopes of y' = x - y (rows y = 2 .. -2, columns x = -2 .. 2)
y =  2:  -4  -3  -2  -1   0
y =  1:  -3  -2  -1   0   1
y =  0:  -2  -1   0   1   2
y = -1:  -1   0   1   2   3
y = -2:   0   1   2   3   4

Drawing the field

Matplotlib's quiver draws arrows. Normalising each arrow to the same length keeps the picture readable, since only the direction matters. On top we draw the exact solutions y=x−1+Ce−xy = x - 1 + Ce^{-x} (you will derive them in Linear Equations and the Integrating Factor).

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

X, Y = np.meshgrid(np.linspace(-2, 3, 21), np.linspace(-3, 3, 21))
S = X - Y
L = np.sqrt(1 + S**2)
fig, ax = plt.subplots(figsize=(6, 5))
ax.quiver(X, Y, 1 / L, S / L, angles="xy", color="gray", width=0.003)
x = np.linspace(-2, 3, 200)
for C in (-2, -1, 0, 1, 2):
    ax.plot(x, x - 1 + C * np.exp(-x), lw=2)
ax.set_ylim(-3, 3); ax.set_title("y' = x - y")
print("solutions bend toward the line y = x - 1")
solutions bend toward the line y = x - 1

Every solution approaches the straight-line solution y=x−1y = x - 1 as xx grows: the term Ce−xCe^{-x} dies out. A slope field shows this kind of long-term behaviour immediately.

Following the arrows numerically

"Follow the arrows" is also an algorithm: from (x0,y0)(x_0, y_0), move a small step hh in the direction of the segment, then repeat. This is Euler's method, and every numerical solver refines it.

python · try it yourself
import math

f = lambda x, y: x - y
x, y, h = 0.0, 2.0, 0.1
for _ in range(20):
    y += h * f(x, y)
    x += h
exact = x - 1 + 3 * math.exp(-x)
print(f"y(2) by following the arrows: {y:.4f}, exact: {exact:.4f}")
y(2) by following the arrows: 1.3647, exact: 1.4060

What a slope field does not show

A slope field cannot tell you whether two solutions might touch or whether a solution blows up in finite time; those questions belong to Existence and Uniqueness. It also hides the exact formula. But for autonomous equations y′=f(y)y' = f(y), where the slope only depends on yy, the field is the same along every horizontal line, and a one-dimensional picture is enough (Autonomous Equations).

In software

  • Vector-field plots (quiver, streamplot in Matplotlib) are standard in physics simulators and fluid visualisers.
  • Particle effects in games often move particles along a precomputed "flow field", a 2D version of a slope field.

Exercises

Exercise 1: Slopes by hand

For y′=y2−xy' = y^2 - x, find the slope at (1,2)(1, 2), (4,2)(4, 2) and (0,−1)(0, -1).

Answer

3, 0 and 1.

Solution

22−1=32^2 - 1 = 3, 22−4=02^2 - 4 = 0, (−1)2−0=1(-1)^2 - 0 = 1.

Exercise 2: Isoclines

Describe the isoclines of y′=x2+y2y' = x^2 + y^2. Where is the slope equal to 4?

Answer

Circles centred at the origin; slope 4 on the circle of radius 2.

Solution

The slope equals cc where x2+y2=cx^2 + y^2 = c, a circle of radius c\sqrt{c}. For c=4c = 4 the radius is 2. Slopes grow quickly away from the origin, so solutions become steep.

Exercise 3: A straight-line solution

Find the straight-line solution y=mx+by = mx + b of y′=2y−xy' = 2y - x.

Answer

y=x2+14y = \frac{x}{2} + \frac{1}{4}.

Solution

Substituting gives m=2(mx+b)−x=(2m−1)x+2bm = 2(mx + b) - x = (2m - 1)x + 2b for every xx. The left side has no xx term, so 2m−1=02m - 1 = 0, i.e. m=12m = \frac{1}{2}; matching constants, m=2bm = 2b, so b=14b = \frac{1}{4}. Check: y′=12y' = \frac{1}{2} and 2y−x=x+12−x=122y - x = x + \frac{1}{2} - x = \frac{1}{2}.

Test yourself

In the slope field of y′=f(x,y)y' = f(x, y), the segment at (a,b)(a, b) has slope…

  1. f(a,b)f(a, b)
  2. b/ab/a
  3. f′(a)f'(a)
  4. 1

An isocline is…

  1. a solution curve
  2. a curve along which the slope field has constant slope
  3. a vertical line
  4. the x-axis

For an autonomous equation y′=f(y)y' = f(y), the slope field…

  1. is the same along every horizontal line
  2. is the same along every vertical line
  3. is always zero
  4. has no solutions