Finite difference coefficients calculator

Type a stencil and a derivative order to get the finite difference weights as exact fractions, the order of accuracy, the leading error term and the full Taylor table, with a line of MATLAB and Python to paste. A built-in test plots the error against dx, so you can see where truncation ends and round-off begins.

Central, forward, backward, skewed and staggered stencils all work. The calculator opens on the Taylor-table challenge of Skill-Lync's CFD courses, and the second tab checks the coefficients in your report. Free, no sign-up, runs in your browser.

Exact rational arithmetic · checked against sympy (Fornberg's algorithm) on 99 stencils · Updated 20 Nov 2026

Offsets like -2, -1, 0, 1, 2 or -2:2; fractions (1/2) for staggered points. The weights c multiply f(x + s·h) and the sum is divided by hᵐ. Up to 11 points.

Test it on a function
Order of accuracy4error ∝ h⁴
Leading error term−1/90 · h⁴ · f⁽⁶⁾(x)course thumb rule says 4 (right here)

f″(x) ≈ [−f(x − 2h) + 16 f(x − h) − 30 f(x) + 16 f(x + h) − f(x + 2h)] / (12 h²)

PointWeight cfhf′h²f″h³f‴h⁴f⁽⁴⁾h⁵f⁽⁵⁾h⁶f⁽⁶⁾
f(x − 2h)−1/121−22−4/32/3−4/154/45
f(x − h)4/31−11/2−1/61/24−1/1201/720
f(x)−5/21000000
f(x + h)4/3111/21/61/241/1201/720
f(x + 2h)−1/121224/32/34/154/45
Σ c × column001000−1/90
MATLABd2f = (-f(x - 2*dx) + 16*f(x - dx) - 30*f(x) + 16*f(x + dx) - f(x + 2*dx)) / (12*dx^2);Pythond2f = (-f(x - 2*dx) + 16*f(x - dx) - 30*f(x) + 16*f(x + dx) - f(x + 2*dx)) / (12*dx**2)
Error of the finite difference against dx, log-log10⁻¹⁰10⁻⁸10⁻⁶10⁻⁴10⁻²10⁻⁴10⁻³10⁻²10⁻¹10⁰central, 5 pointsdx (step size), log scale|error|

Solid: the error your code would print. Dashed: the truncation model |C f⁽ᵐ⁺ᵖ⁾| hᵖ. Dotted: the round-off model. Vertical line: the best dx.

  • Exact f″(π/3) = -4.935745. Smallest error 2.65 × 10⁻¹⁰; slope over the truncation-dominated points 4.02 (19 points).
  • Round-off takes over below dx ≈ 0.0040, where the error bottoms out near 1.6 × 10⁻¹⁰.

How does the Taylor table method work?

Every point of the stencil, f(x + s·h), can be written as a Taylor series around x, where s is the point's offset in units of the step h (−2, −1, 0, 1, 2 for the 5-point central stencil):

Put the numbers sk/k! in a table, one row per stencil point and one column per derivative. That is the Taylor table, and the calculator above prints it. A weighted sum of the rows, Σ cj f(x + sjh), is a sum of the columns. To get the m-th derivative, the weights must make every column add up to zero except column m, which must add up to one. For N points that is N equations in N unknowns:

It has exactly one solution whenever the points are distinct. The first column after m that does not cancel is what is left over, the truncation error, and its power of h is the order of accuracy p:

The calculator solves the system with exact fractions, so −1/12 comes out as −1/12, not −0.0833. It then looks along the table for the first column that does not cancel. Skill-Lync checked its weights against sympy's finite_diff_weights, which uses a different method (Fornberg's 1988 recursion), on 99 stencils with derivative orders 1 to 6: every weight matches exactly. The order and error constant were checked against a series expansion of each formula.

What are the answers to the Taylor table challenge?

The challenge, from Skill-Lync's CFD courses (week 3.5): derive fourth-order approximations of the second derivative with a central scheme, a right-skewed scheme and a left-skewed scheme. Then evaluate them for f(x) = ex cos x and compare each with the exact derivative over a range of dx. 871 public reports have the Taylor table in their title. Of the 705 with their full text online, 630 use ex cos x, 568 evaluate it at x = π/3 and 617 plot the error on a log-log chart. The exact value there is f″(π/3) = −2 eπ/3 sin(π/3) = -4.935745. The three schemes:

The left-skewed scheme uses the same weights in mirror order (−10, 61, −156, 214, −154, 45 over 12h² on i−5 … i), with the same error constant: for an even derivative, mirroring the stencil keeps every sign. For an odd derivative (f′, f‴) the mirror image flips every sign. Errors for dx = linspace(π/4, π/4000, 20), the most common setting in the reports (79 of them):

dxCentral (5 pt)Right-skewed (6 pt)Left-skewed (6 pt)Right, 5 pt (order 3)
π/4 = 0.78548.82 × 10⁻²6.20 × 10⁰4.12 × 10⁻¹2.23 × 10¹
0.45509.60 × 10⁻³1.67 × 10⁰5.46 × 10⁻²2.27 × 10⁰
0.16601.67 × 10⁻⁴1.83 × 10⁻²6.10 × 10⁻³4.07 × 10⁻²
0.04216.88 × 10⁻⁷5.38 × 10⁻⁵4.08 × 10⁻⁵3.46 × 10⁻⁴
π/4000 = 0.0007856.70 × 10⁻¹⁰1.09 × 10⁻⁸1.70 × 10⁻⁸1.34 × 10⁻⁹

Computed in double precision with Python's math library. The π/4000 row is round-off noise, not truncation (next section): the calculator in your browser, or MATLAB, can print a value up to tens of times different there, for example 2.6 × 10⁻¹⁰ instead of 1.1 × 10⁻⁸ for the right-skewed scheme in this page's own JavaScript. The chart below is that JavaScript run.

The course run: errors of three fourth-order schemes and of rounded weights, dx = linspace(pi/4, pi/4000, 20)10⁻¹⁰10⁻⁸10⁻⁶10⁻⁴10⁻²10⁰10⁻⁴10⁻³10⁻²10⁻¹10⁰central, 5 pointsright-skewed, 6 pointsleft-skewed, 6 pointscentral, rounded to 0.0833dx (step size), log scale|error|

Three things in that chart catch people out. Linear spacing puts 19 of the 20 points between dx = 0.04 and 0.79, and only the last one far to the left, so the line from the 19th to the 20th point is one long straight segment. Over that segment the central scheme's slope is 1.7 in the table's numbers, not 4, because the last point is already in the round-off zone (next section). The left-skewed error dips sharply near dx ≈ 0.58: the error changes sign there and passes through zero, which is not a bug. And the skewed curves are not straight lines at large dx: the slope of the right-skewed scheme climbs from 1.8 to 4.5 across the plot. The h⁴ behaviour only holds once dx is small. To measure the order, use log-spaced dx and read the slope where the curve is straight.

Why is a 5-point skewed scheme only third order?

Because one-sided stencils do not get the free extra order that symmetry gives central ones. With 5 points you can cancel 5 columns of the Taylor table: f, f′ and f″ (set to 1), then f‴ and f⁗. The f⁽⁵⁾ column is left over, and it carries h³ after dividing by h²:

The central 5-point scheme gets away with 5 points because its f⁽⁵⁾ column cancels by symmetry: the weights are mirror images, and odd powers of s cancel in pairs. A one-sided stencil has no such symmetry, so fourth order needs a sixth point. 45 of the 705 full reports use the 5-point one-sided weights (35/12, −26/3, 19/2, −14/3, 11/12) for their "fourth-order" skewed scheme, and none of them says third order. Over the course's dx range it is hard to tell from the plot: between dx = 0.166 and 0.083 its slope is 3.6. That is why the calculator reports the order from the Taylor table, not from a fitted slope. Compare its 5-point and 6-point presets: the order drops from 4 to 3.

The challenge's thumb rule gets this one right: P = N − Q for a skewed scheme gives 5 − 2 = 3. It is less reliable elsewhere. P = N − Q + 1 for central schemes is wrong for odd derivatives: the 3-point central first derivative, [f(i+1) − f(i−1)] / (2h), is second order, not third. Skill-Lync checked all 99 stencils: the true order is N − Q for non-symmetric stencils and N − Q rounded up to an even number for symmetric ones. The calculator shows the thumb rule's value next to the true order, so you can see where they differ.

Why does the error stop falling at small dx?

Two errors compete. Truncation shrinks like hp. Round-off grows like 1/hm: the formula subtracts nearly equal numbers and divides by a tiny h², so double-precision rounding (ε ≈ 2.2 × 10⁻¹⁶) gets magnified. A simple model of both, and the step where their sum is smallest:

Truncation and round-off: the error falls as dx shrinks, then rises again10⁻¹¹10⁻⁹10⁻⁷10⁻⁵10⁻³10⁻¹10¹10⁻⁴10⁻³10⁻²10⁻¹10⁰central, 5 pointsright-skewed, 6 pointsdx (step size), log scale|error|

For the course's central scheme the best step is dx ≈ 0.0040 (about π/794), and the error there is about 1.6 × 10⁻¹⁰. The six-point skewed scheme has bigger weights (Σ|c| = 53.3 against 5.3) and a bigger error constant, so its best step is 0.0029 and its best error about 3.1 × 10⁻⁹. The course's smallest step, π/4000 = 0.000785, is past both minimums. There the central error is 6.7 × 10⁻¹⁰, while truncation alone would be 8.3 × 10⁻¹⁴: more than 99.9% of the last point is round-off. Its value depends on the machine and the order of operations, so your MATLAB number there can differ from ours by a large factor; only its size, around the round-off line, is predictable. Only 79 of the 705 full reports mention round-off at all.

What happens if you type the coefficients as 0.0833?

The error stops falling. 86 of the 705 full reports type rounded weights into their formula, e.g. −0.0833·f(x−2dx) + 1.3333·f(x−dx) − 2.5·f(x) + …. With weights rounded to 4 decimals the f″ column of the Taylor table adds up to 1.0001 instead of 1. That is a fixed 0.01% error in f″, and no smaller dx removes it. In the course run the error levels off at 4.94 × 10⁻⁴ once dx is below about 0.1, a flat line on the log-log plot (pink in the course chart above), while the exact weights keep going down to 6.9 × 10⁻⁷ at dx = 0.042. With 3 decimals (1.333, −0.083), which several reports use, the column adds up to 1.001, and the floor is ten times higher: 4.94 × 10⁻³.

The fix is simple. Keep MATLAB's full-precision result of A\b, or write the weights as fractions: (−1/12)·f(x−2dx) + (4/3)·f(x−dx) − (5/2)·f(x) + …. Paste your rounded weights into the Check tab and it shows which columns no longer cancel, and the error your code actually gets.

How do you compute the weights in MATLAB or Python?

Build the Taylor table as a matrix, one row per power and one column per point, and solve it. For the right-skewed scheme, with log-spaced dx so the slope can be read off:

% Taylor table weights for derivative m on stencil s (in units of dx)
s = [0 1 2 3 4 5];  m = 2;                 % right-skewed, 6 points
N = numel(s);
A = zeros(N);  for k = 0:N-1,  A(k+1,:) = s.^k / factorial(k);  end
b = zeros(N,1);  b(m+1) = 1;
c = A \ b;                                  % keep the full-precision weights

f = @(x) exp(x).*cos(x);   x = pi/3;   exact = -2*exp(x)*sin(x);
dx = logspace(-3, -0.1, 20);               % log-spaced: even points on a log-log plot
err = zeros(size(dx));
for i = 1:numel(dx)
    err(i) = abs(sum(c' .* f(x + s*dx(i))) / dx(i)^m - exact);
end
loglog(dx, err, 'o-'); xlabel('dx'); ylabel('|error|'); grid on
import math
import numpy as np

def fd_weights(m, s):
    """Taylor table: row k holds s**k / k!; the weights must give 1 in row m and 0 elsewhere."""
    s = np.asarray(s, dtype=float)
    A = np.array([s**k / math.factorial(k) for k in range(len(s))])
    b = np.zeros(len(s)); b[m] = 1
    return np.linalg.solve(A, b)

f = lambda x: np.exp(x) * np.cos(x)
exact = -2 * np.exp(np.pi / 3) * np.sin(np.pi / 3)       # f'' of e^x cos x at pi/3
s = [0, 1, 2, 3, 4, 5]                                    # right-skewed, 6 points
c = fd_weights(2, s)
for dx in [0.1, 0.05, 0.025]:
    approx = sum(cj * f(np.pi / 3 + sj * dx) for cj, sj in zip(c, s)) / dx**2
    print(f"dx = {dx}: error = {abs(approx - exact):.3e}")

Skill-Lync ran the Python version as printed. It prints 0.1 → 2.034e-03, 0.05 → 1.099e-04, 0.025 → 6.361e-06: halving dx cuts the error by 17 times at the small end (2⁴ = 16 is the asymptotic ratio). The MATLAB version is the same algorithm and was checked by reading only (no MATLAB on this machine). For many points or high derivatives the Taylor-table matrix becomes badly conditioned in floating point. Fornberg's recursion (sympy's finite_diff_weights) avoids that, and this calculator avoids it by using exact fractions.

What this calculator does not cover

  • Non-uniform grids, apart from fixed ratios. Offsets are in units of one h, so a stretched mesh needs the actual point positions. Fractional offsets (1/2, 1/3) cover staggered and simply graded stencils.
  • Compact (implicit, Padé) schemes, where derivatives at neighbouring points appear on the left-hand side too. They need a tridiagonal solve and are a different table.
  • Several dimensions. Cross derivatives such as ∂²f/∂x∂y use 2D stencils. On a uniform grid, the 1D weights here apply along each axis separately.
  • Stability. The order of accuracy says nothing about whether a time-marching scheme built from these weights is stable. That depends on the equation and the time step: see the 2D heat conduction solver for the explicit limit.
Learn CFD from the numerics up

The Taylor table is week 3 of Skill-Lync's CFD courses. After it you write 1D and 2D solvers in MATLAB, then move to OpenFOAM and industry meshes. The same truncation and round-off questions come back in every CFD code you will run.

Frequently asked questions

What are the coefficients of the fourth-order central difference for the second derivative?

−1/12, 4/3, −5/2, 4/3, −1/12 on the points i−2 … i+2, divided by h². With a common denominator: [−f(i−2) + 16 f(i−1) − 30 f(i) + 16 f(i+1) − f(i+2)] / (12 h²). The leading error is −(1/90) h⁴ f⁽⁶⁾, so it is fourth-order accurate.

How many points does a fourth-order one-sided (skewed) second derivative need?

Six: i to i+5 (right-skewed) or i−5 to i (left-skewed). The weights are 15/4, −77/6, 107/6, −13, 61/12, −5/6 over h², leading error −(137/180) h⁴ f⁽⁶⁾. Five one-sided points (i to i+4) only give third order: their error is (5/6) h³ f⁽⁵⁾. 45 of 705 public reports of the course challenge use the five-point version and none of them says it is third order.

What is the Taylor table method?

A way to find finite difference weights by hand. Expand every stencil point f(x + s·h) as a Taylor series, write the coefficients s^k / k! in a table (one row per point, one column per derivative), then choose weights so the columns add up to 0, except the column of the derivative you want, which must add up to 1. That is N equations for N weights. The first column that does not add up to 0 gives the leading error term and the order of accuracy.

Why does my error stop falling (or rise) at very small dx?

Round-off. The formula subtracts nearly equal numbers and divides by dx², so double-precision rounding (about 2.2 × 10⁻¹⁶) is magnified by 1/dx². For the course's central scheme the truncation error and the round-off error are equal near dx ≈ 0.0040, where the total error bottoms out at about 1.6 × 10⁻¹⁰. At the course's smallest step, dx = π/4000, the error is 6.7 × 10⁻¹⁰, all of it round-off: truncation alone would be 8.3 × 10⁻¹⁴.

Is the thumb rule for the order of accuracy always right?

No. The rule taught with the challenge (order = N − Q + 1 for central schemes, N − Q for skewed ones; N points, derivative Q) is right for one-sided stencils and for even derivatives on central stencils. For odd derivatives on a central stencil the order is N − Q: the 3-point central first derivative [−1, 0, 1]/(2h) is second order, not third. Rule that always holds: a symmetric stencil gives N − Q rounded up to an even number.

Why use a skewed (one-sided) scheme at all?

Because a central scheme needs points on both sides. At the first and last nodes of a mesh, next to a wall or an inlet, there are no points outside the domain, so the derivative there has to use points from one side only. One-sided schemes need more points for the same order and have larger error constants (137/180 against 1/90 here, 68 times more), which is why the interior uses central differences.

Related on Skill-Lync

Sources

  • B. Fornberg, "Generation of finite difference formulas on arbitrarily spaced grids", Mathematics of Computation 51 (1988) 699-706: the recursion used for the independent check, and the published central and one-sided weight tables.
  • SymPy, sympy.calculus.finite_diff.finite_diff_weights (an implementation of Fornberg's algorithm) and series: used for the weight, order and error-constant checks.
  • R. J. LeVeque, Finite Difference Methods for Ordinary and Partial Differential Equations, SIAM (2007), chapter 1: the method of undetermined coefficients (the Taylor table) and truncation error.
  • Skill-Lync, all 871 public student reports of the Taylor-table challenge (705 with full text), read 20 Nov 2026: functions, x, dx ranges, the stencils and weights used, and rounded coefficients.
  • Skill-Lync, scripts/verify/fdcoef.py: Taylor-table weights in exact fractions against sympy on 99 stencils, published rows, the thumb rule against the true order, course errors in double precision, the round-off model, rounded weights, and the Python code on this page. This page re-derives all of it at build time and checks it against that script.
Free counselling

From a Taylor table to a CFD career.

Knowing why a scheme is fourth order, and why its error rises again at small dx, is what CFD solver and simulation teams test for. A Skill-Lync counsellor can map the course that fits the role you want.

Talk to a course counsellor

Free. We call you back, usually the same day.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.