Search

Search papers and lessons

Finite Difference Method

Level 3 · 14 min read

The finite difference method replaces a derivative with a difference between nearby function values, turning a differential equation into algebra a computer can solve.

On this page

  • Colour = topic mix
  • Calculus
  • Computational methods
  • Discrete math
  • Sign in to see your progress

TL;DR

A derivative is a limit of differences: the slope at a point is the change in the function over a tiny step, divided by that step. The finite difference method stops short of the limit. It picks a small but finite step hh, replaces each derivative in an equation with a difference over that step, and solves the resulting algebraic equations on a grid of points. The cost is a controlled error that shrinks as hh shrinks, and a stability condition that limits how large a time step can be.

Where you'll see it: in weather and climate models, heat conduction and fluid simulations, option pricing, and any code that solves a differential equation numerically without a closed-form answer.

Intuition

Suppose you know a car's position at two moments one second apart, and you want its speed. The average speed over that second is the change in position divided by one second. That number is not the instantaneous speed, but if the car is not accelerating wildly, it is close. Shrink the interval to a tenth of a second and the estimate tightens. The finite difference method is that same idea applied to any function and any derivative.

The reason it is needed is practical. Most differential equations describing real systems, such as the flow of heat through a metal plate or air over a wing, have no formula for the answer. A computer cannot store a function at every point of a continuum. It can store the function at a finite set of points, a grid, and compute with those values. Replace every derivative with a difference between neighbouring grid values, and the differential equation becomes a large system of algebraic equations.

The plot below shows the central idea in one picture. The true slope of sin⁡x\sin x at x=1x = 1 is the slope of the tangent line. A difference over a finite step uses a secant line through two nearby points, which is close to the tangent when the step is small.

−0.500.511.522.5−0.500.51xy
  • sin x
  • tangent at x = 1
  • secant over h = 0.1
The forward difference (secant through x = 1 and x = 1.1) has slope close to the true slope of the tangent at x = 1. The gap between the two slopes is the error.

The math

The basic formulas

Let ff be a smooth function, and let h>0h \gt 0 be a step. Three standard approximations to the first derivative are

f′(x)≈f(x+h)−f(x)h(forward difference),f'(x) \approx \frac{f(x+h) - f(x)}{h} \qquad \text{(forward difference)}, f′(x)≈f(x)−f(x−h)h(backward difference),f'(x) \approx \frac{f(x) - f(x-h)}{h} \qquad \text{(backward difference)}, f′(x)≈f(x+h)−f(x−h)2h(central difference).f'(x) \approx \frac{f(x+h) - f(x-h)}{2h} \qquad \text{(central difference)}.

Here f(x+h)f(x+h) is the function evaluated one step to the right of xx, and f(x−h)f(x-h) one step to the left. A second derivative can be approximated by

f′′(x)≈f(x+h)−2f(x)+f(x−h)h2.f''(x) \approx \frac{f(x+h) - 2f(x) + f(x-h)}{h^2}.

The forward and backward differences look at only one side of xx. The central difference uses both sides, and that symmetry is what gives it a better accuracy.

Where the error comes from

Expand ff around xx with the Taylor series:

f(x+h)=f(x)+hf′(x)+h22f′′(x)+h36f′′′(x)+⋯f(x+h) = f(x) + h f'(x) + \frac{h^2}{2} f''(x) + \frac{h^3}{6} f'''(x) + \cdots

Substitute this into the forward difference and divide by hh:

f(x+h)−f(x)h=f′(x)+h2f′′(x)+h26f′′′(x)+⋯\frac{f(x+h) - f(x)}{h} = f'(x) + \frac{h}{2} f''(x) + \frac{h^2}{6} f'''(x) + \cdots

The difference from the true derivative is h2f′′(x)+⋯\frac{h}{2} f''(x) + \cdots. This is a truncation error that is proportional to hh, so the forward difference is said to be first-order accurate. Halving hh halves the error.

Now expand both f(x+h)f(x+h) and f(x−h)f(x-h) and subtract. The even-power terms cancel, leaving

f(x+h)−f(x−h)2h=f′(x)+h26f′′′(x)+⋯\frac{f(x+h) - f(x-h)}{2h} = f'(x) + \frac{h^2}{6} f'''(x) + \cdots

The error is proportional to h2h^2, so the central difference is second-order accurate. Halving hh cuts the error by about a factor of four. The cancellation of the even terms is the entire reason the central difference is better, and it is worth remembering: symmetric stencils cancel error terms that one-sided stencils keep.

The same method gives the second-derivative formula. Its error is also of order h2h^2.

Solving a differential equation on a grid

Take the one-dimensional heat equation with diffusivity α\alpha,

∂u∂t=α∂2u∂x2,\frac{\partial u}{\partial t} = \alpha \frac{\partial^2 u}{\partial x^2},

where u(x,t)u(x,t) is the temperature. Place grid points xi=i Δxx_i = i\,\Delta x and time levels tn=n Δtt_n = n\,\Delta t, and write uinu_i^n for the approximate temperature at (xi,tn)(x_i, t_n). Use a forward difference in time and the central second difference in space:

uin+1−uinΔt=αui+1n−2uin+ui−1n(Δx)2.\frac{u_i^{n+1} - u_i^n}{\Delta t} = \alpha \frac{u_{i+1}^n - 2u_i^n + u_{i-1}^n}{(\Delta x)^2}.

Solve for the new value:

uin+1=uin+r(ui+1n−2uin+ui−1n),r=α Δt(Δx)2.u_i^{n+1} = u_i^n + r\left(u_{i+1}^n - 2u_i^n + u_{i-1}^n\right), \qquad r = \frac{\alpha\, \Delta t}{(\Delta x)^2}.

This is an explicit scheme. Every value at the new time level is computed from values at the old level, so each step is a simple update.

Stability

The explicit scheme has a limit. The update amplifies high-frequency wiggles in the solution, and if rr is too large the errors grow without bound. For this scheme the condition is

r=α Δt(Δx)2≤12.r = \frac{\alpha\,\Delta t}{(\Delta x)^2} \le \frac{1}{2}.

This comes from von Neumann stability analysis. It is the diffusion counterpart of the Courant–Friedrichs–Lewy condition, which says for wave-like equations that a numerical method cannot propagate information faster than the grid allows. Refining the grid in space forces a smaller time step, so explicit schemes become expensive as resolution increases. Implicit schemes, which solve a linear system for the new level, avoid this limit at the price of more work per step.

Worked examples

Example
Derivative of sine by finite differences

Estimate f′(1)f'(1) for f(x)=sin⁡xf(x) = \sin x. The exact value is cos⁡1=0.540302\cos 1 = 0.540302 to six decimals.

  1. Use step h=0.1h = 0.1. Evaluate sin⁡1.1=0.891207\sin 1.1 = 0.891207, sin⁡1=0.841471\sin 1 = 0.841471, sin⁡0.9=0.783327\sin 0.9 = 0.783327.
  2. Forward difference: 0.891207−0.8414710.1=0.497364\dfrac{0.891207 - 0.841471}{0.1} = 0.497364. Error =0.497364−0.540302=−0.042938= 0.497364 - 0.540302 = -0.042938.
  3. Central difference: 0.891207−0.7833270.2=0.539402\dfrac{0.891207 - 0.783327}{0.2} = 0.539402. Error =0.539402−0.540302=−0.000900= 0.539402 - 0.540302 = -0.000900.
  4. Compare with the predicted errors. The forward error should be about h2f′′(1)=0.05×(−sin⁡1)=−0.042074\frac{h}{2} f''(1) = 0.05 \times (-\sin 1) = -0.042074, which differs from the measured error only by the next Taylor term, about −0.0009-0.0009. The central error should be about h26f′′′(1)=0.016×(−cos⁡1)=−0.000900\frac{h^2}{6} f'''(1) = \frac{0.01}{6} \times (-\cos 1) = -0.000900, which agrees with the measured −0.000900-0.000900.

Interpretation: at the same step size, the central difference is about 50 times more accurate here. Its error scales as h2h^2, and the forward error as hh.

Example
Heat diffusion on a three-point grid

Take α=1\alpha = 1, a grid with three interior points, zero temperature at both ends, and an initial profile u=(0,1,0)u = (0, 1, 0). Use r=0.4r = 0.4, which satisfies the stability condition.

  1. Point 1: u11=0+0.4 (0−2⋅0+1)=0.4u_1^{1} = 0 + 0.4\,(0 - 2 \cdot 0 + 1) = 0.4.
  2. Point 2: u21=1+0.4 (0−2⋅1+0)=1−0.8=0.2u_2^{1} = 1 + 0.4\,(0 - 2 \cdot 1 + 0) = 1 - 0.8 = 0.2.
  3. Point 3: by symmetry u31=0.4u_3^{1} = 0.4.
  4. The new profile is (0.4,0.2,0.4)(0.4, 0.2, 0.4). Its total is 1.01.0, the same as before.

Interpretation: the peak spread out into its neighbours, as heat should. The total heat in the interior stayed the same here, because the zero-temperature boundaries were not reached yet.

Now repeat the same step with r=0.6r = 0.6, which violates the condition. Point 2 becomes 1+0.6(−2)=−0.21 + 0.6(-2) = -0.2. A negative temperature appears from a profile that started nonnegative, which is unphysical. Repeating the step with this rr makes the oscillations grow, and the solution blows up.

Example
A cubic is differentiated exactly

Let f(x)=x3f(x) = x^3, so f′′(x)=6xf''(x) = 6x. Find f′′(2)f''(2) with the central second difference and h=0.5h = 0.5.

  1. Evaluate: f(2.5)=15.625f(2.5) = 15.625, f(2)=8f(2) = 8, f(1.5)=3.375f(1.5) = 3.375.
  2. Numerator: 15.625−2⋅8+3.375=3.015.625 - 2 \cdot 8 + 3.375 = 3.0.
  3. Divide by h2=0.25h^2 = 0.25: 3.0/0.25=123.0 / 0.25 = 12.
  4. The exact value is 6×2=126 \times 2 = 12.

Interpretation: the error term of the second difference involves the fourth derivative, h212f(4)(x)\frac{h^2}{12} f^{(4)}(x), and for a cubic that is zero. The method is exact for polynomials up to degree three. This is a useful sanity check when implementing a scheme.

Tradeoffs and failure modes

The method is simple, fast and flexible, but three problems show up in practice.

SituationWhat goes wrongBetter choice
Step hh made very small to get accuracyRound-off error grows: subtracting nearly equal numbers loses digits, so the error rises again as hh shrinksChoose hh near the balance point, about ε\sqrt{\varepsilon} (roughly 10−810^{-8}) for a forward difference in double precision, and nearer ε1/3\varepsilon^{1/3} for a central one, or use a higher-order stencil
Explicit time stepping on a fine gridThe Courant or stability limit forces tiny time steps, and the run becomes very slowImplicit schemes such as Crank–Nicolson or backward Euler, which trade extra work per step for a looser limit
Irregular geometry or complex boundariesGrid points fit a rectangle, not a curved shape, and the stencils become messyFinite element or finite volume methods, which build the discretisation on arbitrary meshes
Solution with sharp fronts, such as shocksA fixed grid smears or oscillates near the front, and the stencil loses accuracyAdaptive mesh refinement or upwind schemes that respect the direction of information flow
Unexpected oscillations or blow-upOften a stability violation, not a coding bugCheck the stability condition for the scheme before debugging the code

A further trap is to trust the output because it looks smooth. A finite difference solution is an approximation of an exact solution, and its accuracy must be checked. The standard approach is to refine the grid and see whether the answer converges at the expected rate, or to test on a problem with a known answer.

History

The idea of replacing derivatives with differences is as old as calculus. Leonhard Euler's 1768 method for solving ordinary differential equations, which steps forward using the slope at the current point, is a finite difference scheme in its simplest form.

The method became a practical tool for partial differential equations in the early twentieth century. In 1910, Lewis Fry Richardson attempted a numerical weather forecast by hand, using finite differences on a grid of cells over Europe, and published the approach in 1922 in his book on numerical weather prediction. His forecast failed, largely because of errors in the initial data, but the method was sound.

The stability question was settled in 1928, when Richard Courant, Kurt Friedrichs and Hans Lewy showed that explicit difference schemes for hyperbolic equations must satisfy a condition linking the time step, the grid spacing and the wave speed. The condition is named after them. The arrival of electronic computers after the Second World War made finite difference methods routine, and they remain central to computational physics and engineering.

Check yourself

QuestionFor f(x)=exf(x) = e^x at x=0x = 0 (where f′(0)=1f'(0) = 1), a forward difference with h=0.01h = 0.01 gives roughly 1.005021.00502. What is the error, and what would you expect from h=0.005h = 0.005?Show answer

The error is 1.00502−1=0.005021.00502 - 1 = 0.00502. The forward error is about h2f′′(0)=0.012×1=0.005\frac{h}{2} f''(0) = \frac{0.01}{2} \times 1 = 0.005, which matches. Since the forward error is first order, halving hh to 0.0050.005 should roughly halve the error to about 0.00250.0025. Round-off error is negligible at this step size, so the prediction holds.

QuestionCompute the central difference approximation to f′(1)f'(1) for f(x)=x2f(x) = x^2 with h=0.1h = 0.1. Why is the answer exact?Show answer

Evaluate f(1.1)=1.21f(1.1) = 1.21 and f(0.9)=0.81f(0.9) = 0.81. Then 1.21−0.810.2=0.400.20=2\frac{1.21 - 0.81}{0.2} = \frac{0.40}{0.20} = 2. The exact derivative is f′(1)=2f'(1) = 2. The answer is exact because the error term of the central difference is proportional to f′′′f''', and for a quadratic f′′′f''' is zero. The symmetric stencil cancels all the even-power terms of the Taylor expansion, leaving only the odd terms, and the first odd term beyond f′f' vanishes here.

QuestionAn explicit heat solver uses α=2\alpha = 2, Δx=0.05\Delta x = 0.05, and Δt=0.001\Delta t = 0.001. Is it stable? If not, what is the largest stable time step?Show answer

Compute r=α Δt(Δx)2=2×0.0010.0025=0.8r = \frac{\alpha\,\Delta t}{(\Delta x)^2} = \frac{2 \times 0.001}{0.0025} = 0.8. Since 0.8>0.50.8 \gt 0.5, the scheme is unstable. The largest stable step satisfies 2 Δt0.0025≤12\frac{2\,\Delta t}{0.0025} \le \frac{1}{2}, which gives Δt≤0.000625\Delta t \le 0.000625. Halving the grid spacing would cut the stable step by a factor of four.

QuestionA colleague says that making the step hh as small as possible in a central difference will always give the most accurate derivative. Explain what is wrong with this claim.Show answer

Truncation error falls as h2h^2, but round-off error rises as hh shrinks, because the difference f(x+h)−f(x−h)f(x+h) - f(x-h) subtracts two nearly equal numbers and loses significant digits. Past a certain point, making hh smaller makes the total error worse. The most accurate step is a balance between the two, and it depends on the precision of the arithmetic and the size of the derivatives involved. The claim assumes exact arithmetic, which a computer does not provide.

Sources