Python  

How to Interpolate Using Lagrange’s Interpolation Formula in Python

Table of Contents

  • Introduction

  • What Is Lagrange’s Interpolation?

  • Why It Matters: A Real-World Example from Environmental Science

  • Step-by-Step Mathematical Breakdown

  • Python Implementation with Error Handling

  • Testing with Realistic Scenarios

  • Performance Considerations and Best Practices

  • Conclusion

Introduction

In data science, engineering, and scientific computing, we often face situations where we know the value of a function at certain points—but need to estimate its value between those points. This is called interpolation. Among the many interpolation techniques, Lagrange’s Interpolation Formula stands out for its mathematical elegance and conceptual simplicity.

Unlike methods that require solving systems of equations (like Newton’s divided differences), Lagrange’s approach constructs the interpolating polynomial directly from known data points—making it ideal for small datasets or educational purposes.

This article explores Lagrange interpolation through a compelling real-world use case, provides a clean and robust Python implementation, and shares practical tips for effective usage.

What Is Lagrange’s Interpolation?

Given n+1 distinct data points (x0​,y0​),(x1​,y1​),…,(xn​,yn​) , Lagrange’s formula constructs a unique polynomial P(x) of degree at most n that passes through all these points:

qw

where each Lagrange basis polynomial Li​(x) is defined as:

qa

The beauty? No matrix inversion. No recursion. Just direct computation.

Why It Matters: A Real-World Example from Environmental Science

Imagine you're an environmental scientist monitoring air quality in a remote valley. Your sensor logs PM2.5 (fine particulate matter) levels every 6 hours:

  • 6:00 AM → 25 µg/m³

  • 12:00 PM → 45 µg/m³

  • 6:00 PM → 60 µg/m³

  • 12:00 AM → 30 µg/m³

But a policymaker urgently asks: “What was the PM2.5 level at 3:00 PM?” You don’t have a reading for that exact time—but you can interpolate it.

Using Lagrange’s method, you can estimate the air quality at any intermediate time with high accuracy, enabling timely public health advisories. This is interpolation in action—turning sparse data into actionable insights.

Step-by-Step Mathematical Breakdown

For our air quality example with 4 points, we compute:

  1. For each known point (xi​,yi​) , build Li​(x) —a polynomial that is 1 at xiand 0 at all other xj.

  2. Multiply each yi​ by its corresponding Li​(x) .

  3. Sum all terms to get P(x) , the estimated value at any x .

At x=15 (3:00 PM in 24-hour format), the formula blends the influence of all four measurements based on their proximity.

Python Implementation with Error Handling

"""
Lagrange Interpolation Example
------------------------------

This script demonstrates how to interpolate values using
Lagrange's interpolation formula, with an example for
PM2.5 air quality estimation.

It also includes simple test cases for validation.
"""

from typing import List

def lagrange_interpolate(x_points: List[float], y_points: List[float], x: float) -> float:
    """
    Interpolate value at x using Lagrange's interpolation formula.

    Args:
        x_points (list): List of x-coordinates (must be unique)
        y_points (list): List of corresponding y-values
        x (float): Point at which to interpolate

    Returns:
        float: Interpolated value at x

    Raises:
        ValueError: If input lists are empty, mismatched, or contain duplicates
    """
    if not x_points or not y_points:
        raise ValueError("Input lists cannot be empty.")
    if len(x_points) != len(y_points):
        raise ValueError("x_points and y_points must have the same length.")
    if len(set(x_points)) != len(x_points):
        raise ValueError("x_points must contain unique values.")

    n = len(x_points)
    result = 0.0

    for i in range(n):
        # Compute Lagrange basis polynomial L_i(x)
        term = y_points[i]
        for j in range(n):
            if i != j:
                denominator = x_points[i] - x_points[j]
                if denominator == 0:
                    raise ValueError("Duplicate x-values detected.")
                term *= (x - x_points[j]) / denominator
        result += term

    return result


def main():
    # Example: Air quality monitoring
    # Time in hours (6 AM, 12 PM, 6 PM, 12 AM)
    times = [6, 12, 18, 24]
    pm25 = [25, 45, 60, 30]

    # Interpolate PM2.5 at 3:00 PM (15:00)
    x_target = 15
    estimated = lagrange_interpolate(times, pm25, x_target)
    print(f"Estimated PM2.5 at {x_target}:00: {estimated:.2f} µg/m³")

    # Test edge case: interpolation at known point
    assert abs(lagrange_interpolate(times, pm25, 12) - 45) < 1e-10
    print(" Edge case test passed: interpolation at known point is correct.")


if __name__ == "__main__":
    main()
c

This implementation:

  • Validates inputs rigorously

  • Avoids division by zero

  • Works for any numeric type (int, float)

  • Is easy to read and debug

Performance Considerations and Best Practices

  • Time Complexity: O(n2) —fine for n<50 , but inefficient for large datasets.

  • Numerical Stability: Lagrange interpolation can suffer from Runge’s phenomenon with high-degree polynomials or unevenly spaced points.

  • When to Use:

    • Small, static datasets

    • Educational demonstrations

    • Symbolic math applications

  • When to Avoid:

    • Real-time systems with large data

    • Noisy data (consider splines or regression instead)

  • Pro Tip: For repeated queries, precompute basis polynomials or switch to barycentric Lagrange interpolation for speed.

Conclusion

Lagrange’s Interpolation Formula is more than a textbook curiosity—it’s a practical tool for filling gaps in real-world data, from environmental monitoring to robotics and finance. While not suited for massive datasets, its simplicity and directness make it invaluable for prototyping, education, and small-scale estimation tasks. By understanding its mechanics and limitations—and pairing it with thoughtful validation—you can turn sparse observations into reliable predictions. In a world drowning in data but starving for insight, interpolation remains a quiet superpower.