Blog

Lagrange interpolation explained

Write the curve through your points straight from the data, no equations to solve. Three-point example worked in full and checked.


You have three or four data points and you want the curve that passes through all of them, not just a straight line between the nearest two. Lagrange interpolation writes that curve down directly. There is no system of equations to set up and no matrix to invert.

The quick answer

For each data point, build a helper function that equals 1 at that point and 0 at all the others. Multiply each one by its own y value and add them up. The sum passes through every point automatically.

Good for a handful of points. Past about six, use a cubic spline instead.

The trick that makes it work

Suppose you could build, for each of your data points, a function that is worth 1 at that point and exactly 0 at every other data point. Call the one belonging to point i by the name Li.

Then add them up, each weighted by its own y value:

P(x) = y0L0(x) + y1L1(x) + … + ynLn(x)

Check what happens at one of your data points, say x3. The term for point 3 contributes y3 × 1. Every other term contributes its y value times 0. The total is y3, which is exactly what you wanted. It works at every point for the same reason.

Building one of these helper functions takes two moves.

To make it hit zero at all the other points, multiply together a factor of (x − xj) for every other point j. Landing on any of those x values makes one factor zero, which kills the whole product.

That gets you the zeros but leaves the wrong height at your own point. So divide by whatever the product comes out to there, which forces it to 1:

Li(x) = ∏j≠i (x − xj) ÷ (xi − xj)

Top half makes the zeros, bottom half sets the scale. That is the entire method.

Worked example with three points

Three points, spaced unevenly on purpose, taken from the curve y = x3:

ixiyi
011
128
2464

Three points give a quadratic. Find the value at x = 3.

Build the three helper functions

For point 0, at x = 1: put (x − 2) and (x − 4) on top so it vanishes at the other two points, then divide by what that product gives at x = 1, which is (−1)(−3) = 3.

L0(x) = (x − 2)(x − 4) ÷ 3

Same for the other two:

L1(x) = (x − 1)(x − 4) ÷ [(2 − 1)(2 − 4)] = (x − 1)(x − 4) ÷ (−2) L2(x) = (x − 1)(x − 2) ÷ [(4 − 1)(4 − 2)] = (x − 1)(x − 2) ÷ 6

Test one of them before going further. At x = 2, L1 gives (1)(−2) ÷ (−2) = 1, and at x = 1 and x = 4 it gives 0. Working as intended.

Evaluate at x = 3

HelperArithmetic at x = 3ValueTimes its y
L0(3)(1)(−1) ÷ 3−0.333331 × (−0.33333) = −0.33333
L1(3)(2)(−1) ÷ (−2)1.000008 × 1 = 8.00000
L2(3)(2)(1) ÷ 60.3333364 × 0.33333 = 21.33333
P(3) = −0.33333 + 8.00000 + 21.33333 = 29.00000

The real value of 33 is 27, so the quadratic is out by 2. That is expected. A cubic curve cannot be reproduced by a quadratic, and being 7 percent out across a range where the function grows sixty-fourfold is reasonable.

One thing worth noticing in that table: the three helper values add up to −0.33333 + 1 + 0.33333 = 1. They always do, at any x, for any set of points. It is the fastest check available, and a total that is not 1 means you got a denominator wrong.

Turn it into a normal polynomial

Multiplying the brackets out gives something you can plug numbers into directly:

P(x) = (1/3)(x2 − 6x + 8) − 4(x2 − 5x + 4) + (32/3)(x2 − 3x + 2)

Collecting the terms:

  • x2 terms: 1/3 − 4 + 32/3 = 11 − 4 = 7
  • x terms: −2 + 20 − 32 = −14
  • constants: 8/3 − 16 + 64/3 = 24 − 16 = 8
P(x) = 7x2 − 14x + 8

Check it against all three data points, plus the value you already worked out:

x7x2 − 14x + 8Should be
17 − 14 + 8 = 11
228 − 28 + 8 = 88
4112 − 56 + 8 = 6464
363 − 42 + 8 = 2929, matching the table above

It is the same thing you already do with two points

Run the method with only two points. L0 becomes (x − x1) ÷ (x0 − x1) and L1 becomes (x − x0) ÷ (x1 − x0), which gives:

P(x) = y0(x − x1)/(x0 − x1) + y1(x − x0)/(x1 − x0)

That is the symmetric version of the ordinary linear interpolation formula. Lagrange is what drawing a line between two points turns into when you have more than two.

What it is good and bad at

Good: no equations to solve. The alternative is building a matrix of powers of x and solving it, which is more work and numerically fragile. Here you write the answer down.

Good: it shows you the structure. The formula makes clear that your answer is a weighted mix of your y values, where the weights depend only on the x positions. That idea is the basis of numerical integration rules like Simpson's, which come from integrating these same helper functions.

Bad: adding a point means starting over. Every helper function changes when a new point arrives, so you redo all of them. If your data arrives over time, Newton's divided differences gives you the same curve while only adding one term.

Bad: slow if written naively. Evaluating it the obvious way costs work proportional to the square of the number of points, every single time. The barycentric version fixes this by precomputing one weight per point, after which each lookup is cheap and numerically stable.

Bad: it does not fix any polynomial problem. High degree on evenly spaced points still oscillates badly, as described in Runge's phenomenon. Lagrange is a way of writing the polynomial down, not a way of making it behave.

Reach for it with a handful of points, when you want an explicit formula, or when you are deriving a numerical method. For a real dataset with many points, a spline is almost always the better answer, and both are in the interpolation calculator if you want to compare them.

Try it yourself

Run these numbers through the calculator and check the working step by step.

Open the calculator