Slope Fields
For a first-order equation you can see the solutions before solving anything. At every point 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 . At the slope is 0, at it is 2, at it is . Along the line every slope is 0: such a curve of constant slope is an isocline. Along every slope is , the same as the slope of that line itself, so is a solution: it follows its own segments.
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 (you will derive them in Linear Equations and the Integrating Factor).
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 as grows: the term 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 , move a small step in the direction of the segment, then repeat. This is Euler's method, and every numerical solver refines it.
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 , where the slope only depends on , the field is the same along every horizontal line, and a one-dimensional picture is enough (Autonomous Equations).
In software
- Vector-field plots (
quiver,streamplotin 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 , find the slope at , and .
Answer
3, 0 and 1.
Solution
, , .
Exercise 2: Isoclines
Describe the isoclines of . 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 where , a circle of radius . For 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 of .
Answer
.
Solution
Substituting gives for every . The left side has no term, so , i.e. ; matching constants, , so . Check: and .
Test yourself
In the slope field of , the segment at has slope…
- 1
An isocline is…
- a solution curve
- a curve along which the slope field has constant slope
- a vertical line
- the x-axis
For an autonomous equation , the slope field…
- is the same along every horizontal line
- is the same along every vertical line
- is always zero
- has no solutions