Lagrange Interpolation Polynomial Calculator

Give this calculator a table of x and y values and it builds the unique polynomial of lowest degree that passes exactly through every one of them, then evaluates that polynomial at whatever x you ask for. It shows each Lagrange basis polynomial Li separately, expands the result into ordinary a x^k form, and prints the Newton divided-difference triangle so you can check the same answer two ways. Use it to read between the rows of a steam table, a rating chart or a set of lab measurements, and to check numerical-methods homework line by line.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
x valuesThe nodes, separated by commas or spaces. They must all be different.1, 2, 3, 4
y valuesThe measured or tabulated value at each node, in the same order as the x values.1, 8, 27, 64
Interpolate at x =The point where you want the polynomial evaluated. Keep it inside the range of your nodes.2.5

It returns

  • Interpolated value P(x) — The interpolating polynomial evaluated at your target x.
  • Interpolating polynomial
  • Degree of the polynomial
  • Data points used
  • Leading coefficient
  • Slope P'(x) at the target

The formula

P(x)=i=0n1yijixxjxixj
f[x0,x1]=f(x1)f(x0)x1x0

In plain text: P(x) = Σ yᵢ·Lᵢ(x), where Lᵢ(x) = Π (x − xⱼ)/(xᵢ − xⱼ) for j ≠ i

  • P(x)The interpolating polynomial evaluated at x
  • nNumber of data points supplied
  • xᵢThe i-th node — must be distinct from every other node
  • yᵢThe value of the tabulated function at xᵢ
  • Lᵢ(x)The i-th Lagrange basis polynomial: 1 at xᵢ, 0 at every other node

The result has degree at most n − 1 and is the unique polynomial of that degree through the given points.

Updated Category Numerical Methods, Root Finding & Interpolation Verified against published test cases Reading time 11 min

What Lagrange interpolation actually does

Interpolation answers one question: you have measured or tabulated a quantity at a handful of points, and you need its value somewhere in between. Lagrange interpolation answers it by constructing a single polynomial that agrees with your data at every node you supplied, and then reading that polynomial at the point you want.

For n distinct nodes there is exactly one polynomial of degree at most n − 1 through them. That uniqueness is the whole reason the method is trustworthy: whether you build it by the Lagrange formula, by Newton's divided differences, or by solving a Vandermonde system, you get the same curve. The forms differ only in how much arithmetic they cost and how easily you can add another point later.

The word degree at most matters. Four points that happen to lie on a straight line give you a degree-1 polynomial, not a cubic with three vanishing coefficients. This calculator reports the true degree after dropping leading coefficients that come out as zero, so you can see when your data is simpler than the number of points suggests.

Interpolation is not curve fitting. A least-squares fit deliberately misses the data points to smooth out noise; an interpolating polynomial hits every one of them, noise included. If your y values carry measurement error, a fit is the honest tool and interpolation will chase the errors. Use this calculator when the tabulated values are exact - physical constants, code-book tables, published factors - or when you deliberately want the exact-through-the-points curve.

The formula, and why it has that shape

The Lagrange form writes the answer as a weighted sum of your y values:

P(x) = y₀L₀(x) + y₁L₁(x) + … + yₙ₋₁Lₙ₋₁(x)

Each weight Li(x) is built to be a switch. It equals 1 at its own node xi and 0 at every other node:

Lᵢ(x) = ∏₀₋ᵢ (x − x⫺) / (xᵢ − x⫺)

Look at the numerator. It contains a factor (x − x⫺) for every node except the i-th, so it vanishes at all of them. The denominator is that same product evaluated at xi, which forces Lᵢ(xᵢ) = 1. Substitute any node into the full sum and every term dies except one, leaving exactly the y value you supplied. That is the proof that the polynomial interpolates, and it takes one line.

Two consequences worth remembering. First, the basis values at any x always sum to 1, because interpolating the constant function 1 must return 1; the table below lets you verify that on your own data. Second, the denominators are the only place a division happens, and they are zero only if two nodes coincide - which is why repeated x values are rejected rather than approximated.

The Newton divided-difference form rewrites the same polynomial as P(x) = f[x₀] + f[x₀,x₁](x − x₀) + f[x₀,x₁,x₂](x − x₀)(x − x₁) + …, where the coefficients are the top diagonal of the difference triangle. Its advantage is incremental: add a new data point and you append one term, while the Lagrange form has to be rebuilt from scratch because every denominator changes.

Worked example: four points of a cubic

Take the four points (1, 1), (2, 8), (3, 27) and (4, 64) - the cubes of the first four integers - and interpolate at x = 2.5.

  1. Build the denominators. For i = 0 (node x = 1): (1−2)(1−3)(1−4) = (−1)(−2)(−3) = −6. For i = 1: (2−1)(2−3)(2−4) = (1)(−1)(−2) = 2. For i = 2: (3−1)(3−2)(3−4) = (2)(1)(−1) = −2. For i = 3: (4−1)(4−2)(4−3) = (3)(2)(1) = 6.
  2. Build the numerators at x = 2.5. L0: (2.5−2)(2.5−3)(2.5−4) = (0.5)(−0.5)(−1.5) = 0.375. L1: (2.5−1)(2.5−3)(2.5−4) = (1.5)(−0.5)(−1.5) = 1.125. L2: (2.5−1)(2.5−2)(2.5−4) = (1.5)(0.5)(−1.5) = −1.125. L3: (2.5−1)(2.5−2)(2.5−3) = (1.5)(0.5)(−0.5) = −0.375.
  3. Divide. L0 = 0.375 ÷ −6 = −0.0625; L1 = 1.125 ÷ 2 = 0.5625; L2 = −1.125 ÷ −2 = 0.5625; L3 = −0.375 ÷ 6 = −0.0625. They sum to −0.0625 + 0.5625 + 0.5625 − 0.0625 = 1, as they must.
  4. Weight the y values. 1(−0.0625) + 8(0.5625) + 27(0.5625) + 64(−0.0625) = −0.0625 + 4.5 + 15.1875 − 4 = 15.625.

The answer is 15.625, and you can check it instantly: four points of y = x3 determine that cubic uniquely, and 2.53 = 15.625. The expanded form the calculator prints is P(x) = x^3 - every lower coefficient cancels to zero, which is exactly the uniqueness theorem doing its job.

The divided-difference triangle for the same data reads 1, 7, 6, 1 down its top diagonal, giving P(x) = 1 + 7(x−1) + 6(x−1)(x−2) + 1(x−1)(x−2)(x−3). At x = 2.5 that is 1 + 10.5 + 6(1.5)(0.5) + (1.5)(0.5)(−0.5) = 1 + 10.5 + 4.5 − 0.375 = 15.625. Same number, different bookkeeping.

How to read the result

Trust the value most when your target sits near the middle of the node range and the nodes are close together. The classical error bound for interpolation at n nodes is

f(x) − P(x) = f⁽ⁿ⁾(ξ) / n! × (x−x₀)(x−x₁)…(x−xₙ₋₁)

for some unknown ξ in the interval. You usually cannot evaluate the derivative term, but the product term is fully under your control and it tells you most of what you need. It is smallest between the two central nodes and grows without limit outside the range - which is why extrapolation one node-spacing past the end is already risky and three spacings past is usually worthless. The calculator flags any target outside the data range for this reason.

The reported degree is a diagnostic. If you feed eleven points and get degree 10, the curve is free to wiggle ten times; if the data is smooth, that freedom shows up as oscillation between the nodes rather than as accuracy. Watch the chart: a polynomial that swings far above and below the data between the last few nodes is telling you the degree is too high for the job.

The slope output is the derivative of the interpolating polynomial at your target. It is a reasonable estimate of the underlying function's derivative near the middle of the range, and a poor one near the ends, where interpolation error grows fastest and differentiation amplifies it. If you specifically want a derivative from tabulated data, a centred difference on the two nearest nodes is usually steadier than differentiating a high-degree interpolant.

Which interpolation method to reach for

Cost and behaviour of the common methods for n tabulated points.
MethodDegree of the resultWork to evaluate onceAdding a new pointBest used when
Linear (two nearest nodes)1O(1)FreeDense tables, or any table where you must not overshoot
Lagrange form≤ n − 1O(n2)Rebuild every basis polynomialOne-off evaluation, hand work, proofs
Newton divided differences≤ n − 1O(n2) to build, O(n) afterAppend one termData arriving incrementally, repeated evaluation
Barycentric Lagrange≤ n − 1O(n) after O(n2) setupUpdate every weightMany evaluations of the same fixed node set
Cubic spline3 on each sub-intervalO(n) setup, O(1) afterRe-solve the tridiagonal systemMany nodes, smooth curve wanted, no oscillation
Least-squares polynomial fitChosen by youO(n) per evaluationRefitNoisy measurements, where hitting every point is wrong

All the interpolating rows produce the same curve for the same nodes; they differ in arithmetic cost and in how gracefully they absorb a new data point.

Mistakes that produce a plausible wrong number

  • Extrapolating past the last node. The polynomial is unconstrained outside the range and the highest-power term dominates immediately. A degree-6 interpolant can be off by orders of magnitude one spacing beyond the end while still looking smooth on screen.
  • Feeding every point you have. More nodes means higher degree, not more accuracy. For a smooth function, three or four nodes bracketing your target usually beat twenty spread across the whole table.
  • Using equally spaced nodes at high degree. Runge's function 1/(1+25x^2) on [−1, 1] is the standard counterexample: the interpolation error at the ends grows as you add equally spaced nodes. Chebyshev node spacing fixes it; equal spacing does not.
  • Interpolating noisy data. The curve is forced through every measurement, so scatter becomes wiggle. Fit instead.
  • Repeating an x value. Two rows with the same x make a denominator zero. If you genuinely have a value and a slope at the same point, you want Hermite interpolation, which is built for repeated nodes.
  • Reading the expanded coefficients too literally. Expanding a high-degree interpolant into powers of x is numerically fragile - large coefficients of opposite sign cancel. Evaluate through the basis or the Newton form when precision matters.

Where this sits among the numerical methods

Interpolation is the foundation under a surprising amount of numerical analysis. Integrate the interpolating polynomial instead of the function and you get the Newton-Cotes rules directly: fit a straight line through two nodes and integrate it and you have the trapezoidal rule; fit a parabola through three and you have Simpson's rule. Differentiate it and you get the finite-difference formulas. Invert it - interpolate x as a function of y and evaluate at y = 0 - and you have inverse interpolation, a respectable root-finder in its own right.

That last connection runs the other way too. The secant method is precisely linear inverse interpolation applied repeatedly, and Muller's method is the quadratic version. If you have a derivative available, Newton-Raphson is the faster choice; if you do not, the interpolation-based methods are what remain.

For a local approximation around a single point rather than through a set of points, the Taylor series is the counterpart: it matches many derivatives at one place, where interpolation matches one value at many places. And when the underlying relationship is known to be linear or polynomial but the data is noisy, a least-squares fit through a normal-equation solve replaces interpolation altogether.

The method is named for Joseph-Louis Lagrange, who published it in 1795, though Edward Waring had described the same construction in 1779 and Euler rediscovered it in 1783. Nothing about the formula has changed since; what changed is that barycentric rearrangements made it numerically stable enough to use at high degree, which the naive form is not.

Key terms

Node
One of the x values where the polynomial is forced to match your data. Nodes must be distinct.
Basis polynomial
The weight Li(x) attached to yi. It equals 1 at node i and 0 at every other node.
Divided difference
f[xᵢ,x⫺] = (f⫺ − fᵢ)/(x⫺ − xᵢ), and recursively for higher orders. The top diagonal of the triangle gives the Newton form coefficients.
Runge phenomenon
The growth of interpolation error near the ends of an interval as more equally spaced nodes are added. Named for Carl Runge, who exhibited it in 1901.
Extrapolation
Evaluating the polynomial outside the range of the nodes. Legal arithmetic, unreliable prediction.

Frequently asked questions

How many points should I use for interpolation?

Usually three to five, chosen so your target sits near the middle of them. Interpolation error depends on the product of the distances from your target to each node, so nearby nodes help and distant ones mostly raise the degree. Doubling the number of nodes across a wide table typically makes the answer worse, not better, because the extra degrees of freedom show up as oscillation between the points rather than as accuracy.

What is the difference between Lagrange and Newton interpolation?

Nothing, in the answer - both produce the identical polynomial for identical nodes. They differ in bookkeeping. The Lagrange form is symmetric and easy to write down, but adding a data point forces you to rebuild every basis polynomial. Newton's divided-difference form is built incrementally, so a new point appends a single term and the earlier coefficients are untouched. This calculator prints both so you can check one against the other.

Why do my x values have to be different?

Because the denominator of a basis polynomial is a product of differences (xᵢ − x⫺), and a repeated node makes one of those zero. Mathematically there is no unique polynomial through two different y values at the same x, and there is nothing to compute when both y values are equal either. If you want to prescribe a value and a slope at the same point, use Hermite interpolation, which handles repeated nodes deliberately.

Can I interpolate outside my data range?

The arithmetic works, but treat the answer with suspicion. The error term contains the product of the distances from your target to every node, and that product grows quickly once you leave the range while nothing constrains the curve. The calculator warns you when the target lies outside the node range. One node spacing beyond the end is sometimes defensible; several spacings almost never is.

Why does the calculator sometimes report a lower degree than I expect?

Because the interpolating polynomial has degree at most n − 1, not exactly that. Five points that lie on a parabola give a degree-2 polynomial with the cubic and quartic coefficients equal to zero. The calculator drops leading coefficients smaller than 1e-12 before reporting the degree, so you see the real degree of your data instead of a padded one.

What do the basis values mean if they are negative or bigger than one?

They are weights, not probabilities, and they are routinely negative or larger than one when the target lies away from the middle of the nodes. What is always true is that they sum to exactly 1. Large positive and negative weights that nearly cancel are a warning sign: they mean the answer is a small difference of large numbers, so rounding error in the y values is being amplified.

Is interpolation the same as a line of best fit?

No, and confusing them is the most common misuse. Interpolation passes through every point exactly; a least-squares fit deliberately misses them to average out noise. Use interpolation for exact tabulated values - published factors, code-book tables, physical constants. Use a fit for measurements with scatter, where forcing the curve through every reading just reproduces the noise as wiggle.

How do I interpolate a table that has two independent variables?

Interpolate in one direction, then the other. For a table indexed by temperature and pressure, run the calculator across the temperature row at each of the two bracketing pressures, then interpolate the two results against pressure. That is bilinear interpolation when you use two points each way, and it generalises to higher orders by using more nodes in each pass.

References

  • Numerical Analysis, 10th ed. (Chapter 3, Interpolation and Polynomial Approximation) — Richard L. Burden and J. Douglas Faires, Cengage Learning
  • Numerical Recipes: The Art of Scientific Computing, 3rd ed. (Chapter 3, Interpolation and Extrapolation) — Press, Teukolsky, Vetterling and Flannery, Cambridge University Press
  • Barycentric Lagrange Interpolation, SIAM Review 46(3), 501-517 (2004) — Jean-Paul Berrut and Lloyd N. Trefethen