1. What a differential equation is; slope fields and Euler's method
Every equation solved so far has had a number as its unknown. A differential equation has a function as its unknown, related to its own derivatives — and solving one means finding every function that makes the relationship true. This opening lesson of the module builds the vocabulary, then two ways to understand a solution without solving the equation algebraically at all: a picture, and a numerical stepping scheme built directly from §3.1's linear approximation.
What a differential equation is
A differential equation relates an unknown function y(x) to its own derivatives. The simplest useful form is first-order (only y' appears, no higher derivatives):
\frac{dy}{dx}=f(x,y)
A solution is any function y(x) that makes this equation true for every x in some interval. Because differentiation loses a constant (§4.0's +C), a first-order differential equation typically has a whole family of solutions — the general solution — parametrized by one arbitrary constant. Pinning down a single member of that family requires an initial condition, y(x_0)=y_0, giving an initial value problem (IVP) with (generically) exactly one solution.
Example: \frac{dy}{dx}=y has general solution y=Ce^x for any constant C (check: \frac d{dx}(Ce^x)=Ce^x=y ✓) — this is §2.6's defining property of e^x, restated as a differential equation. The initial condition y(0)=1 pins down C=1, giving the single function y=e^x.
Slope fields
Even without solving \frac{dy}{dx}=f(x,y) algebraically, the equation itself specifies a slope at every point in the plane — plug any (x,y) into f, and that's the slope any solution curve must have if it happens to pass through that point. A slope field (or direction field) draws a short line segment of that slope at a grid of sample points, and any actual solution curve must, at every point it passes through, run tangent to the segment drawn there.
A slope field reveals a solution's qualitative shape without solving anything — where solutions curve up, curve down, level off, or diverge — purely by reading which way the segments point across the plane. This is the differential-equations analogue of using a function's sign information (§3.5) to sketch a curve without computing every point exactly.
Euler's method
A slope field shows direction; Euler's method turns that direction into an actual numerical approximation of the solution curve, by taking small steps, each one following the local slope for a short distance — exactly §3.1's linear approximation, applied repeatedly.
Starting from (x_0,y_0), with step size h:
x_{n+1}=x_n+h,\qquad y_{n+1}=y_n+h\cdot f(x_n,y_n)
Each step is literally the tangent-line approximation from §3.1: y(x_n+h)\approx y(x_n)+h\cdot y'(x_n)=y_n+h\cdot f(x_n,y_n), using the differential equation itself to supply the slope y'(x_n)=f(x_n,y_n) at each new point. Smaller h means more steps but less error accumulated per step — the identical accuracy-versus-cost tradeoff already seen in §4.11's numerical integration, since Euler's method is, at its core, a repeated first-order Taylor step (§8.2), and it inherits that method's O(h) error behavior: halving the step size roughly halves the total error.
Doing it in Python
Euler's method applied to \frac{dy}{dx}=x+y, y(0)=1, approximating y(0.5):
def f(x, y):
return x + y
x, y = 0, 1
h = 0.1
print(f"{'x':>5} {'y (Euler)':>12}")
for _ in range(5):
y = y + h * f(x, y)
x = x + h
print(f"{x:>5.2f} {y:>12.6f}")
Comparing Euler's approximation to the exact solution (y=2e^x-x-1, found by §13.2's integrating-factor technique):
from math import exp
def f(x, y):
return x + y
x, y = 0, 1
h = 0.1
for _ in range(5):
y = y + h * f(x, y)
x = x + h
exact = 2 * exp(0.5) - 0.5 - 1
print(f"Euler's approximation at x=0.5: {y:.6f}")
print(f"exact solution at x=0.5: {exact:.6f}")
print(f"error: {abs(y - exact):.6f}")
Watching the error shrink as the step size shrinks — confirming the O(h) behavior directly:
from math import exp
def f(x, y):
return x + y
def euler(h):
x, y = 0, 1
steps = int(0.5 / h)
for _ in range(steps):
y = y + h * f(x, y)
x = x + h
return y
exact = 2 * exp(0.5) - 0.5 - 1
for h in (0.1, 0.05, 0.025, 0.0125):
approx = euler(h)
print(f"h={h:>7}: approx={approx:.6f}, error={abs(approx-exact):.6f}")
Worked example
Use Euler's method with h=0.1 to approximate y(0.5) for \frac{dy}{dx}=x+y, y(0)=1.
| n | x_n | y_n | f(x_n,y_n)=x_n+y_n | y_{n+1}=y_n+0.1f(x_n,y_n) |
|---|---|---|---|---|
| 0 | 0.0 | 1.0000 | 1.0000 | 1.1000 |
| 1 | 0.1 | 1.1000 | 1.2000 | 1.2200 |
| 2 | 0.2 | 1.2200 | 1.4200 | 1.3620 |
| 3 | 0.3 | 1.3620 | 1.6620 | 1.5282 |
| 4 | 0.4 | 1.5282 | 1.9282 | 1.7210 |
\boxed{y(0.5)\approx1.7210}
Sanity check. The exact solution (found in §13.2 via the integrating factor) is y=2e^x-x-1, giving y(0.5)=2e^{0.5}-1.5\approx1.7974. Euler's approximation, 1.7210, is in the right ballpark — off by about 0.076, or roughly 4\% — consistent with the coarse step size h=0.1 and the known O(h) error rate; halving h should roughly halve this gap, exactly as the "Doing it in Python" section confirms numerically. ✓
Your turn
1. Verify that y=Ce^x is the general solution to \frac{dy}{dx}=y for any constant C, by direct substitution.
2. Use Euler's method with h=0.25 to approximate y(0.5) for \frac{dy}{dx}=y, y(0)=1 (two steps), and compare to the exact value y(0.5)=e^{0.5}.
3. True or false: a slope field can be drawn without ever solving the differential equation.
Solutions
1. \frac d{dx}(Ce^x)=Ce^x — this equals y=Ce^x exactly, for any value of C, confirming the entire family solves the equation simultaneously. (The specific value of C is only pinned down once an initial condition is supplied.)
2. f(x,y)=y, h=0.25.
Step 1: x_0=0,y_0=1. y_1=1+0.25(1)=1.25, x_1=0.25.
Step 2: f(x_1,y_1)=y_1=1.25. y_2=1.25+0.25(1.25)=1.25+0.3125=1.5625.
\boxed{y(0.5)\approx1.5625}
Exact: e^{0.5}\approx1.6487. Error \approx0.086 — a larger relative error than the worked example's, consistent with a much coarser step size (h=0.25 versus 0.1) being used over the same total interval.
3. True. A slope field only requires evaluating f(x,y) — the right-hand side of the differential equation — at a grid of points; it never requires knowing y(x) itself. This is exactly why slope fields are useful even for equations with no elementary closed-form solution at all: the qualitative picture is available immediately, regardless of whether an exact formula for y can ever be found.
Check yourself in code
Use Euler's method with h=0.1 to approximate y(0.5) for \frac{dy}{dx}=x+y, y(0)=1, printing y at each step.
Print exactly this:
x=0.1, y=1.100000
x=0.2, y=1.220000
x=0.3, y=1.362000
x=0.4, y=1.528200
x=0.5, y=1.721020
def f(x, y):
return x + y
x, y = 0, 1
h = 0.1
for _ in range(5):
y = y + h * f(x, y)
x = x + h
print(f"x={x:.1f}, y=...")
def f(x, y):
return x + y
x, y = 0, 1
h = 0.1
for _ in range(5):
y = y + h * f(x, y)
x = x + h
print(f"x={x:.1f}, y={y:.6f}")
A differential equation, \frac{dy}{dx}=f(x,y), is solved by an entire function rather than a number, typically an entire family of them distinguished by an arbitrary constant that an initial condition pins down. A slope field visualizes that family by plotting f's value as a short tangent segment at every point in the plane, and Euler's method turns those tangent directions into an actual numerical approximation, one linear-approximation step (§3.1) at a time — an O(h) method, exactly mirroring §4.11's numerical-integration error analysis.
Next: the first genuine solving technique — separable equations, where algebra alone (no numerical approximation required) recovers an exact formula for y(x).