Finite Difference Method for Boundary Value Problems
The finite difference method solves a boundary value problem such as y″ = f(x, y, y′), y(a) = α, y(b) = β by replacing the derivatives with difference quotients at grid points xᵢ = a + ih. The central difference y″(xᵢ) ≈ (yᵢ₋₁ − 2yᵢ + yᵢ₊₁)/h² turns a linear equation into a tridiagonal system for the unknown values yᵢ, with error O(h²).
Before this lesson: Numerical Differentiation, Shooting Method and Boundary Value Problems
The idea: one equation per grid point
The shooting method marches from one end and adjusts the starting slope. The finite difference method works on the whole interval at once. Split [a, b] into N equal pieces of width h = (b − a)/N, and let yᵢ stand for the unknown value y(xᵢ).
At each interior point x_1, \dots, x_{N-1}, replace y″ and y′ with the central differences from numerical differentiation. Each point gives one equation linking yᵢ₋₁, yᵢ and yᵢ₊₁, and the boundary conditions supply y₀ = α and y_N = β.
For a linear differential equation, the result is a linear system with nonzero entries only on three diagonals. Such a tridiagonal system can be solved in about 8N operations, so even thousands of grid points are cheap.
Finite difference method formulas
With h = (b − a)/N and xᵢ = a + ih, the central difference approximations are
y''(x_i) \approx \frac{y_{i-1} - 2y_i + y_{i+1}}{h^2},\qquad y'(x_i) \approx \frac{y_{i+1} - y_{i-1}}{2h}Both have error O(h²). For the linear equation y″ = p(x)y′ + q(x)y + r(x), substituting them and multiplying by h² gives, at each interior point i = 1, …, N − 1,
\left(1 + \frac{h}{2}p_i\right)y_{i-1} - \left(2 + h^2 q_i\right)y_i + \left(1 - \frac{h}{2}p_i\right)y_{i+1} = h^2 r_iMove the known y₀ = α and y_N = β to the right side, and solve the (N − 1) × (N − 1) tridiagonal system. When q(x) ≥ 0 and h|p(x)| ≤ 2, the matrix is diagonally dominant, the system has a unique solution, and elimination without pivoting (the Thomas algorithm) is stable.
How to use the finite difference method
- Choose N and set h = (b − a)/N and the grid points xᵢ = a + ih.
- At each interior point, replace y″ and y′ with central differences.
- Multiply by h² and collect the coefficients of yᵢ₋₁, yᵢ and yᵢ₊₁.
- Substitute the boundary values y₀ = α and y_N = β and move them to the right side.
- Solve the tridiagonal system by forward elimination and back substitution.
- Check accuracy by halving h: the error should drop by about a factor of 4.
Finite difference method examples
Example 1: One interior point
Solve y″ = 6x, y(0) = 0, y(1) = 3 with h = 0.5. The exact solution is y = x³ + 2x.
x_0 = 0,\quad x_1 = 0.5,\quad x_2 = 1;\quad y_0 = 0,\ y_2 = 3
Grid and boundary values.
\dfrac{y_0 - 2y_1 + y_2}{0.25} = 6(0.5) = 3Central difference at x₁, with h² = 0.25.
0 - 2y_1 + 3 = 0.75
Multiply by 0.25 and substitute y₀ and y₂.
-2y_1 = -2.25 \Rightarrow y_1 = 1.125
Subtract 3, then divide by −2.
y(0.5) = 0.125 + 1 = 1.125
Exact value.
Answer: y(0.5) ≈ 1.125, which is exact. The central difference is exact for cubics, because its error term involves the fourth derivative, which is 0 here.
Example 2: Three interior points for y″ = y
Solve y″ = y, y(0) = 0, y(1) = 1 with h = 0.25. The exact solution is y = sinh x / sinh 1.
y_{i-1} - 2y_i + y_{i+1} = 0.0625\,y_i \Rightarrow y_{i-1} - 2.0625\,y_i + y_{i+1} = 0Central difference times h² = 0.0625.
-2.0625y_1 + y_2 = 0,\quad y_1 - 2.0625y_2 + y_3 = 0,\quad y_2 - 2.0625y_3 = -1
Equations at x₁, x₂, x₃ with y₀ = 0 and y₄ = 1.
y_2 = 2.0625y_1
Solve the first equation for y₂.
y_3 = 2.0625y_2 - y_1 = (4.25390625 - 1)y_1 = 3.25390625y_1
Solve the second for y₃ and substitute.
2.0625y_1 - 2.0625(3.25390625y_1) = -1 \Rightarrow -4.648681640625\,y_1 = -1
Substitute into the third equation.
y_1 \approx 0.215115,\quad y_2 \approx 0.443674,\quad y_3 \approx 0.699963
Divide, then back-substitute.
\tfrac{\sinh 0.25}{\sinh 1} \approx 0.214952,\quad \tfrac{\sinh 0.5}{\sinh 1} \approx 0.443409,\quad \tfrac{\sinh 0.75}{\sinh 1} \approx 0.699724Exact values.
Answer: y(0.25) ≈ 0.2151, y(0.5) ≈ 0.4437 and y(0.75) ≈ 0.7000, each within 0.0003 of the exact values. This forward substitution is the idea behind the Thomas algorithm for tridiagonal systems.
Example 3: An equation with a first-derivative term
Solve y″ = −2y′, y(0) = 0, y(1) = 1 with h = 0.25. The exact solution is y = (1 − e^(−2x))/(1 − e^(−2)).
\dfrac{y_{i-1} - 2y_i + y_{i+1}}{h^2} = -2 \cdot \dfrac{y_{i+1} - y_{i-1}}{2h}Central differences for y″ and y′.
y_{i-1} - 2y_i + y_{i+1} = -0.25(y_{i+1} - y_{i-1})Multiply by h² = 0.0625; the right side becomes −h(yᵢ₊₁ − yᵢ₋₁).
0.75y_{i-1} - 2y_i + 1.25y_{i+1} = 0Collect terms (p = −2, so 1 + hp/2 = 0.75 and 1 − hp/2 = 1.25).
-2y_1 + 1.25y_2 = 0 \Rightarrow y_2 = 1.6y_1
Equation at x₁ with y₀ = 0.
0.75y_1 - 2(1.6y_1) + 1.25y_3 = 0 \Rightarrow y_3 = 1.96y_1
Equation at x₂: 1.25y₃ = 2.45y₁.
0.75(1.6y_1) - 2(1.96y_1) + 1.25 = 0 \Rightarrow -2.72y_1 = -1.25
Equation at x₃ with y₄ = 1.
y_1 \approx 0.459559,\quad y_2 \approx 0.735294,\quad y_3 \approx 0.900735
Divide, then back-substitute.
Answer: y(0.25) ≈ 0.4596, y(0.5) ≈ 0.7353 and y(0.75) ≈ 0.9007. The exact values are 0.455054, 0.731059 and 0.898464, so the errors are below 0.005.
Example 4: Checking that the error is O(h²)
Repeat Example 2 with N = 2, 4 and 8 and compare y(0.5) with the exact 0.443409.
-2.25y_1 + 1 = 0 \Rightarrow y_1 = \dfrac{1}{2.25} \approx 0.444444N = 2: one equation, since 2 + h² = 2.25.
| N | h | y(0.5) estimate | Error | Error ratio |
|---|---|---|---|---|
| 2 | 0.5 | 0.444444 | 1.035 × 10⁻³ | — |
| 4 | 0.25 | 0.443674 | 2.65 × 10⁻⁴ | 3.91 |
| 8 | 0.125 | 0.443476 | 6.66 × 10⁻⁵ | 3.98 |
Answer: each halving of h cuts the error by a factor near 4, confirming second-order accuracy, as for the trapezoidal rule and Heun’s method.
Example 5: Temperature in a rod (word problem)
A 1 m rod has both ends held at 100°C and loses heat to 20°C air, so its steady temperature satisfies T″ = 16(T − 20). Use h = 0.25 m to estimate the temperature at x = 0.25 and at the center.
T_{i-1} - 2T_i + T_{i+1} = 1 \cdot (T_i - 20)Central difference times h² = 0.0625; 16h² = 1.
T_{i-1} - 3T_i + T_{i+1} = -20Collect terms.
T_3 = T_1
The rod and its boundary values are symmetric about x = 0.5.
100 - 3T_1 + T_2 = -20 \Rightarrow T_2 = 3T_1 - 120
Equation at x₁ with T₀ = 100.
T_1 - 3T_2 + T_1 = -20 \Rightarrow 2T_1 - 3(3T_1 - 120) = -20
Equation at x₂, using T₃ = T₁.
-7T_1 + 360 = -20 \Rightarrow T_1 = \dfrac{380}{7} \approx 54.29Simplify and solve.
T_2 = 3(54.285714) - 120 \approx 42.86
Back-substitute.
| h | T(0.5) estimate | Error |
|---|---|---|
| 0.25 | 42.86 | 1.59 |
| 0.125 | 41.68 | 0.42 |
| 0.0625 | 41.37 | 0.11 |
Answer: T(0.25) ≈ 54.3°C and T(0.5) ≈ 42.9°C with h = 0.25. The exact center temperature is 20 + 80/cosh 2 ≈ 41.26°C; the table shows the error dropping by about 4 each time h is halved.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Forgetting to divide by h² | Writing y₀ − 2y₁ + y₂ = 6x₁ in Example 1 | Multiply the right side by h², or keep the h² in the denominator. |
| Leaving boundary values in the unknowns | Treating y₀ and y_N as unknowns with no equations | y₀ = α and y_N = β are known; move them to the right side. |
| Using a one-sided difference for y′ | (yᵢ₊₁ − yᵢ)/h in Example 3 | Use the central difference (yᵢ₊₁ − yᵢ₋₁)/(2h) to keep O(h²) accuracy. |
| Mixing up the coefficients of yᵢ₋₁ and yᵢ₊₁ | Swapping 0.75 and 1.25 in Example 3 | With p = −2, the coefficient of yᵢ₋₁ is 1 + hp/2 = 0.75 and of yᵢ₊₁ is 1.25. |
| Expecting exact answers on a coarse grid | Reporting 42.86°C as the rod’s center temperature | Refine h and check that the values settle; the error shrinks like h². |
Related methods
Shooting versus finite differences
The shooting method reuses initial value solvers and can be very accurate, but it struggles when solutions grow quickly. The finite difference method is more robust, gives all grid values at once and extends naturally to partial differential equations, at the cost of solving a linear system.
Nonlinear boundary value problems
For a nonlinear equation such as y″ = y², the difference equations form a nonlinear system. Solve it with Newton’s method for systems; the Jacobian is tridiagonal, so each Newton step is still cheap.
Derivative boundary conditions and PDEs
A condition such as y′(b) = 0 can be handled with a ghost point y_{N+1} = y_{N-1} (see Practice 10). The same difference quotients in two variables give the standard methods for the heat equation and Laplace’s equation.
Practice problems
- Solve y″ = 2, y(0) = 0, y(2) = 0 with h = 1, and compare with the exact solution y = x² − 2x.
- Write the difference equation at an interior point for y″ = 4y with h = 0.5.
- Use Problem 2 to solve y″ = 4y, y(0) = 0, y(1) = 2 with h = 0.5. Compare with the exact y(0.5) = 2 sinh 1 / sinh 2.
- Solve y″ = 0, y(0) = 1, y(2) = 3 with h = 0.5 by setting up and solving the 3 × 3 tridiagonal system.
- Solve y″ = −y′, y(0) = 0, y(1) = 1 with h = 0.5. The exact y(0.5) is (1 − e^(−0.5))/(1 − e^(−1)).
- A finite difference solution has error 0.004 with h = 0.1. Estimate the error with h = 0.05.
- A cable hangs between two poles 100 m apart, both 10 m high. Its height satisfies y″ = 0.002 (x in meters). Use h = 50 to find the height at the middle.
- How many unknowns are there for h = 0.01 on [0, 1], and why is the system still fast to solve?
- For y″ = q(x)y + r(x) with q ≥ 0, show that the matrix is diagonally dominant.
- Solve y″ = y, y(0) = 1, y′(1) = 0 with h = 0.5, using a ghost point y₃ = y₁ for the condition at x = 1.
Show answers
1.
y_0 - 2y_1 + y_2 = 1^2 \cdot 2 = 2
Central difference at x₁ = 1, times h² = 1.
0 - 2y_1 + 0 = 2 \Rightarrow y_1 = -1
Substitute y₀ = y₂ = 0 and solve.
Answer: y(1) ≈ −1, which matches the exact 1 − 2 = −1 (central differences are exact for quadratics)
2.
y_{i-1} - 2y_i + y_{i+1} = 0.25(4y_i) = y_iMultiply y″ = 4y by h² = 0.25.
Answer: yᵢ₋₁ − 3yᵢ + yᵢ₊₁ = 0
3.
0 - 3y_1 + 2 = 0
Use Problem 2 at x₁ = 0.5 with y₀ = 0 and y₂ = 2.
y_1 = \dfrac{2}{3} \approx 0.666667Solve.
\dfrac{2\sinh 1}{\sinh 2} \approx \dfrac{2.350402}{3.626860} \approx 0.648054Exact value.
Answer: y(0.5) ≈ 0.6667, about 0.019 above the exact 0.6481
4.
-2y_1 + y_2 = -1,\quad y_1 - 2y_2 + y_3 = 0,\quad y_2 - 2y_3 = -3
Equations with y₀ = 1 and y₄ = 3 moved right.
y_2 = 2y_1 - 1,\quad y_3 = 2y_2 - y_1 = 3y_1 - 2
Solve the first two equations forward.
(2y_1 - 1) - 2(3y_1 - 2) = -3 \Rightarrow -4y_1 + 3 = -3
Substitute into the third equation.
y_1 = 1.5,\quad y_2 = 2,\quad y_3 = 2.5
Solve and back-substitute.
Answer: (1.5, 2, 2.5), on the straight line y = 1 + x, which is the exact solution
5.
y_0(1 - 0.25) - 2y_1 + y_2(1 + 0.25) = 0
For y″ = −y′, the coefficients are 1 ∓ h/2 with h/2 = 0.25.
0 - 2y_1 + 1.25 = 0 \Rightarrow y_1 = 0.625
Substitute y₀ = 0, y₂ = 1 and solve.
\dfrac{1 - e^{-0.5}}{1 - e^{-1}} \approx \dfrac{0.393469}{0.632121} \approx 0.622459Exact value.
Answer: y(0.5) ≈ 0.625, within 0.003 of the exact 0.6225
6.
0.004 \cdot \left(\tfrac{1}{2}\right)^2 = 0.001Second order: halving h divides the error by about 4.
Answer: about 0.001
7.
\dfrac{10 - 2y_1 + 10}{2500} = 0.002Central difference at x = 50 with h² = 2500.
20 - 2y_1 = 5 \Rightarrow y_1 = 7.5
Multiply by 2500 and solve.
Answer: 7.5 m at the middle, a sag of 2.5 m; this is exact because the true shape is a parabola
8.
N = \dfrac{1}{0.01} = 100,\quad N - 1 = 99Number of interior unknowns.
Answer: 99 unknowns; the matrix is tridiagonal, so elimination takes about 8 × 99 operations instead of about 99³/3 for a full matrix
9.
\left|-(2 + h^2 q_i)\right| = 2 + h^2 q_i \ge 2 = |1| + |1|
Compare the diagonal entry with the two off-diagonal entries.
Answer: each diagonal entry is at least as large as the sum of the other entries in its row (strictly larger in the first and last rows), so the matrix is diagonally dominant and elimination without pivoting is stable
10.
1 - 2.25y_1 + y_2 = 0
Equation at x₁ = 0.5 with y₀ = 1 (2 + h² = 2.25).
y_1 - 2.25y_2 + y_1 = 0 \Rightarrow y_1 = 1.125y_2
Equation at x₂ = 1 with the ghost point y₃ = y₁.
1 - 2.53125y_2 + y_2 = 0 \Rightarrow y_2 = \dfrac{1}{1.53125} \approx 0.653061Substitute into the first equation.
y_1 = 1.125(0.653061) \approx 0.734694
Back-substitute.
Answer: y(0.5) ≈ 0.7347 and y(1) ≈ 0.6531; the exact values are 0.730763 and 1/cosh 1 ≈ 0.648054
Finite difference method FAQ
What is the finite difference method for boundary value problems?
It replaces the derivatives in a differential equation with difference quotients at evenly spaced grid points, which turns the boundary value problem into a system of algebraic equations for the values of y at those points.
What is the central difference formula for y″?
y″(xᵢ) ≈ (yᵢ₋₁ − 2yᵢ + yᵢ₊₁)/h². Its error is (h²/12)y⁽⁴⁾ plus higher-order terms, so it is second-order accurate.
Why is the finite difference matrix tridiagonal?
Each equation involves only a grid point and its two neighbors, so each row of the matrix has nonzero entries only on the main diagonal and the diagonals just above and below it.
Is the finite difference method better than the shooting method?
Neither is always better. Finite differences are more robust for problems where shots blow up and extend to PDEs, while shooting can use highly accurate initial value solvers with very little code.
Summary
- Replace y″ with (yᵢ₋₁ − 2yᵢ + yᵢ₊₁)/h² and y′ with (yᵢ₊₁ − yᵢ₋₁)/(2h) at each interior grid point.
- A linear boundary value problem becomes a tridiagonal linear system for y_1, \dots, y_{N-1}.
- The boundary values y₀ = α and y_N = β move to the right side.
- The method is second order: halving h cuts the error by about 4.
- Nonlinear problems lead to nonlinear systems, solved with Newton’s method.
Previous lesson: Shooting Method
Next lesson: Axioms of Probability, the first lesson of Probability Theory. See the full Probability Theory Index.
Back to the Numerical Methods Index