---
title: "Lagrange interpolation pseudo-code"
subject: "Numerical Method"
chapter: 4
key: "semester_4/numerical_method:lagrange-pseudocode"
source: https://www.bctengineer.com/q/semester_4/numerical_method/lagrange-pseudocode
site: BCT Engineering
---

# Lagrange interpolation pseudo-code

Numerical Method · Chapter 4: Interpolation

- Asked in: 2071 Magh, 8 marks; 2068 Magh, 8 marks (asked 2 times)
- Video: [Python code of Lagrange's Interpolation | Interpolating polynomial | #numericalanalysis #msmaths](https://www.youtube.com/watch?v=FyYv-5F0wY8) — MS MATHS
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/numerical_method/lagrange-pseudocode
- Whole chapter: https://www.bctengineer.com/md/semester_4/numerical_method/chapter-4

## Question

Develop a pseudocode to interpolate the given set of data using Lagrange interpolation.

## Short answer

Lagrange interpolation constructs a polynomial of degree ≤ n-1 through n points. For a given xp, compute Σ y_i · Π_{j≠i} (xp - x_j)/(x_i - x_j). Pseudocode implements two nested loops: outer over i, inner over j to compute product and accumulate sum.

## Answer

Lagrange interpolation fits the unique polynomial of degree ≤ n−1 through n points (xᵢ, yᵢ):

```text
P(x) = Σ yᵢ · Lᵢ(x),     Lᵢ(x) = Π (x − xⱼ)/(xᵢ − xⱼ)     (j ≠ i)
```

#### Pseudo-code

```text
START
  INPUT n                         // number of data points
  FOR i = 0 TO n−1
      INPUT x[i], y[i]
  END FOR
  INPUT xp                        // point where value is needed

  sum = 0
  FOR i = 0 TO n−1
      L = 1                       // Lagrange basis term Li(xp)
      FOR j = 0 TO n−1
          IF j ≠ i THEN
              L = L * (xp − x[j]) / (x[i] − x[j])
          END IF
      END FOR
      sum = sum + y[i] * L
  END FOR

  PRINT "Interpolated value at", xp, "=", sum
STOP
```

#### Notes

- The x values must be distinct (otherwise division by zero), but they need not be equally spaced or sorted.
- Two nested loops, so the time taken is O(n²); the value changes completely if a new point is added, so every term must be recomputed.
- Quick check: if xp equals some x[k], the term for i = k has L = 1 and all others have L = 0, so the output is y[k].

#### Equivalent C code

```c
#include <stdio.h>
int main() {
    int n, i, j;
    float x[20], y[20], xp, sum = 0, L;
    printf("Enter n: ");           scanf("%d", &n);
    printf("Enter x and y:\n");
    for (i = 0; i < n; i++) scanf("%f %f", &x[i], &y[i]);
    printf("Enter xp: ");          scanf("%f", &xp);
    for (i = 0; i < n; i++) {
        L = 1;
        for (j = 0; j < n; j++)
            if (j != i) L *= (xp - x[j]) / (x[i] - x[j]);
        sum += y[i] * L;
    }
    printf("f(%.4f) = %.4f\n", xp, sum);
    return 0;
}
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. In the pseudocode, what does the inner loop compute for each i?
   A. L = product of (xp - x[j])/(x[i] - x[j]) over all j ≠ i
   B. L = sum of (xp - x[j]) over all j
   C. L = y[i] times x[i]
   D. L = (xp - x[i]) / n
   Answer: A. This is the Lagrange basis Li(xp); the final value is the sum of y[i]·Li. (Key point: Formula)

2. Where must L be reset to 1?
   A. Once before the program
   B. At the start of each pass of the outer i loop
   C. Only after the loop ends
   D. Inside the j loop for each j
   Answer: B. Each basis term is its own product, so L restarts at 1 for every i. (Key point: Initialisation)

3. Why is the term skipped when j = i?
   A. It would make L negative
   B. It would repeat the sum
   C. It would divide by zero (x[i] - x[i] = 0)
   D. It is only a speed optimisation
   Answer: C. The factor for j = i has denominator zero and is not part of the basis. (Key point: Why j ≠ i)

4. If xp equals some x[k], what does the program output?
   A. y[k]
   B. 0
   C. The average of all y
   D. An error always
   Answer: A. Then Lk = 1 and all other Li = 0, so the sum is y[k]. (Key point: Properties)

## Related in Numerical Method

- [Gauss-Jordan pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Agauss-jordan-pseudocode) — Numerical Method, chapter 3, asked 4×
- [Bisection method pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Abisection-pseudocode) — Numerical Method, chapter 2, asked 3×
- [False position pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Afalse-position-pseudocode) — Numerical Method, chapter 2, asked 2×
- [Power method pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Apower-method-algorithm) — Numerical Method, chapter 3, asked 2×
- [Simpson 3/8 pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Asimpson-38-pseudocode) — Numerical Method, chapter 5
- [Lagrange: y at x = 3.0](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Alagrange-y-at-3) — Numerical Method, chapter 4

## Related in other subjects

- [Algorithm/pseudo-code for software PID](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Apid-software-algorithm) — Embedded System, chapter 7, asked 3×
- [Algorithm versus pseudo-code](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgorithm-vs-pseudocode) — Computer Programming, chapter 2
- [Map sequence diagram to pseudo code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aorder-model-mapping-pseudocode) — Object Oriented Analysis and Design, chapter 4
- [Booking format for two-set horizontal angle](https://www.bctengineer.com/question.md?key=semester_3%2Fsurveying_i%3Abooking-format-two-set-horizontal-angle) — Surveying I, chapter 7
