3 Interpolation And Approximation

Numerical Methods · Unit 3

Interpolation and Approximation

Exam-focused notes for Interpolation and Approximation (Numerical Methods, BIT203): what the TU syllabus asks and how it has actually been tested, with 8 solved past questions from this unit.

Interpolation and Approximation in short

Interpolation finds the value of a function between known data points. It is used when we have a table of values but no formula, and we need a value that is not in the table.

Interpolation and Approximation: notes

Lagrange interpolation formula and algorithm

Given $n+1$ points $(x_0, y_0), \ldots, (x_n, y_n)$, the Lagrange polynomial of degree $n$ is

$$P(x) = \sum_{i=0}^{n} y_i L_i(x), \qquad L_i(x) = \prod_{\substack{j=0 \ j \ne i}}^{n} \frac{x - x_j}{x_i - x_j}$$

Each $L_i(x)$ equals 1 at $x_i$ and 0 at every other node. So $P(x)$ passes through all the points. The nodes need not be equally spaced.

Example. Find $f(10)$ from the data $f(5) = 12$, $f(6) = 13$, $f(9) = 14$, $f(11) = 16$.

$$\begin{aligned} L_0(10) &= \frac{(10-6)(10-9)(10-11)}{(5-6)(5-9)(5-11)} = \frac{-4}{-24} = \frac{1}{6} \ L_1(10) &= \frac{(10-5)(10-9)(10-11)}{(6-5)(6-9)(6-11)} = \frac{-5}{15} = -\frac{1}{3} \ L_2(10) &= \frac{(10-5)(10-6)(10-11)}{(9-5)(9-6)(9-11)} = \frac{-20}{-24} = \frac{5}{6} \ L_3(10) &= \frac{(10-5)(10-6)(10-9)}{(11-5)(11-6)(11-9)} = \frac{20}{60} = \frac{1}{3} \ f(10) &= 12\cdot\frac{1}{6} + 13\cdot\left(-\frac{1}{3}\right) + 14\cdot\frac{5}{6} + 16\cdot\frac{1}{3} \ &= 2 - 4.3333 + 11.6667 + 5.3333 = 14.6667 \end{aligned}$$

Algorithm.

  1. Read the number of points $n$, the values $x_i, y_i$, and the point $x$.
  2. Set $sum = 0$.
  3. For each $i$, set $term = y_i$. For each $j \ne i$, multiply $term$ by $(x - x_j)/(x_i - x_j)$.
  4. Add $term$ to $sum$.
  5. Print $sum$ as $f(x)$.
#include <stdio.h>

int main() {
    int n, i, j;
    double x[20], y[20], xp, sum = 0, term;
    printf("Number of points: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) scanf("%lf %lf", &x[i], &y[i]);
    printf("Value of x: ");
    scanf("%lf", &xp);
    for (i = 0; i < n; i++) {
        term = y[i];
        for (j = 0; j < n; j++)
            if (j != i) term *= (xp - x[j]) / (x[i] - x[j]);
        sum += term;
    }
    printf("f(%.2f) = %.4f\n", xp, sum);
    return 0;
}

What this unit covers

  • Lagrange interpolation formula and algorithm
  • Newton's divided difference method
  • Newton's forward difference formula
  • Newton's backward difference table construction
  • Interpolation versus regression
  • Applications of interpolation
  • Derivative estimation using interpolation formulas

Newton's divided difference method

2082 · 10 marks

Write an algorithm to compute the value of interpolation using Newton’s divided difference method.Write a program to compute the value of interpolation using Newton’s divided difference method.[5+5]

Show solved answer

Newton's Divided Difference Interpolation

(a) Algorithm

Concept

Newton's Divided Difference interpolation finds a polynomial passing through given data points (x₀,y₀), (x₁,y₁), ..., (xₙ,yₙ) and estimates the value at any point x.

Divided Difference Formula

The interpolating polynomial is:

f(x) = f[x₀] + (x-x₀)f[x₀,x₁] + (x-x₀)(x-x₁)f[x₀,x₁,x₂] + ...

Where divided differences are defined as:

  • Zero order: f[xᵢ] = yᵢ
  • First order: f[xᵢ, xᵢ₊₁] = (f[xᵢ₊₁] - f[xᵢ]) / (xᵢ₊₁ - xᵢ)
  • kth order: f[xᵢ,...,xᵢ₊ₖ] = (f[xᵢ₊₁,...,xᵢ₊ₖ] - f[xᵢ,...,xᵢ₊ₖ₋₁]) / (xᵢ₊ₖ - xᵢ)

Algorithm

Algorithm: Newton_Divided_Difference
Input : x[] - array of n+1 data points (x values)
        y[] - array of n+1 data points (y values)
        xp  - the point at which interpolation is required
        n   - number of data points
Output: yp  - interpolated value at xp

Step 1: START

Step 2: Read n, x[0..n-1], y[0..n-1], xp

Step 3: Declare a 2D array dd[n][n]
        (dd = divided difference table)

Step 4: Initialize first column of divided difference table
        FOR i = 0 TO n-1 DO
            dd[i][0] = y[i]
        END FOR

Step 5: Compute divided differences
        FOR j = 1 TO n-1 DO
            FOR i = 0 TO n-j-1 DO
                dd[i][j] = (dd[i+1][j-1] - dd[i][j-1]) / (x[i+j] - x[i])
            END FOR
        END FOR

Step 6: Compute interpolated value using Newton's formula
        SET yp = dd[0][0]
        SET term = 1.0

        FOR k = 1 TO n-1 DO
            term = term * (xp - x[k-1])
            yp = yp + dd[0][k] * term
        END FOR

Step 7: Print yp (interpolated value at xp)

Step 8: STOP

(b) C Program

#include <stdio.h>

#define MAX 20

int main()
{
    float x[MAX], y[MAX], dd[MAX][MAX];
    float xp, yp, term;
    int n, i, j, k;

    /* Input number of data points */
    printf("Enter the number of data points: ");
    scanf("%d", &n);

    /* Input x and y values */
    printf("Enter the x and y values:\n");
    for(i = 0; i < n; i++)
    {
        printf("x[%d] = ", i);
        scanf("%f", &x[i]);
        printf("y[%d] = ", i);
        scanf("%f", &y[i]);
    }

    /* Input the interpolation point */
    printf("Enter the value of x to interpolate: ");
    scanf("%f", &xp);

    /* Step 1: Initialize first column with y values */
    for(i = 0; i < n; i++)
    {
        dd[i][0] = y[i];
    }

    /* Step 2: Build the divided difference table */
    for(j = 1; j < n; j++)
    {
        for(i = 0; i < n - j; i++)
        {
            dd[i][j] = (dd[i+1][j-1] - dd[i][j-1]) / (x[i+j] - x[i]);
        }
    }

    /* Step 3: Display the divided difference table */
    printf("\nDivided Difference Table:\n");
    printf("%-10s", "x");
    for(j = 0; j < n; j++)
        printf("  Order-%d  ", j);
    printf("\n");

    for(i = 0; i < n; i++)
    {
        printf("%-10.4f", x[i]);
        for(j = 0; j < n - i; j++)
            printf("  %-8.4f", dd[i][j]);
        printf("\n");
    }

    /* Step 4: Compute interpolated value */
    yp   = dd[0][0];
    term = 1.0;

    for(k = 1; k < n; k++)
    {
        term = term * (xp - x[k-1]);
        yp   = yp + dd[0][k] * term;
    }

    /* Output result */
    printf("\nInterpolated value at x = %.4f is y = %.4f\n", xp, yp);

    return 0;
}

Sample Output

Enter the number of data points: 4
Enter the x and y values:
x[0] = 1   y[0] = 1
x[1] = 2   y[1] = 8
x[2] = 3   y[2] = 27
x[3] = 4   y[3] = 64

Enter the value of x to interpolate: 2.5

Divided Difference Table:
x           Order-0    Order-1    Order-2    Order-3  
1.0000      1.0000     7.0000     6.0000     1.0000   
2.0000      8.0000     19.0000    9.0000   
3.0000      27.0000    37.0000  
4.0000      64.0000  

Interpolated value at x = 2.5000 is y = 15.6250

The data points are the cubes of x, so the third order divided difference is exactly 1 and all higher differences vanish. The interpolating polynomial reproduces $y = x^3$, and indeed $2.5^3 = 15.625$, which confirms the program.


Conclusion

Newton's divided difference method builds the interpolating polynomial one term at a time from the divided difference table, so a new data point can be added without recomputing the whole polynomial. Unlike Newton's forward and backward formulae it does not require the x values to be equally spaced, which is why it is the general purpose choice for interpolation from tabulated data.

Open this answer on its own page →

2078 · 5 marks

Given the following set of data points. Obtain the table of divided difference and use that table to estimate the value of $f(1.5)$.

$$\begin{array}{|c|c|c|c|c|c|}\hline x & 1 & 2 & 3 & 4 & 5 \ \hline f(x)=x^3-1 & 0 & 7 & 26 & 63 & 124 \ \hline \end{array}$$

[5]

$x$ 1 2 3 4 5 ------------------ $f(x)=x^3-1$ 0 7 26 63 124 Estimate: $f(1.5)$ --- First divided differences: $$f[1,2]=\frac{7-0}{2-1}=7,\quad f[2,3]=\frac{26-7}{3-2}=19$$ $$f[3,4]=\frac{63-26}{4-3}=37,\quad f[4,5]=\frac{124-63}{5-4}=61$$ Second divided differences: $$f[1,2,3]=\frac{19-7}{3-1}=6,...

Open this answer on its own page →

Derivative estimation using interpolation formulas

2080 · 5 marks

Evaluate $\frac{dy}{dx}$ at $x = 5$ using Newton's forward interpolation formula using the following table.

X13579
y-1.2012.80119.60472.801302.80

[5]

Show solved answer

Evaluating dy/dx at x = 5 using Newton's Forward Interpolation Formula

Given

X13579
y-1.2012.80119.60472.801302.80
  • Uniform spacing: $h = 2$
  • $x_0 = 1$
  • Evaluate $\dfrac{dy}{dx}$ at $x = 5$

Solution

Forward Difference Table

$\Delta y$:

  • $12.80 - (-1.20) = 14.00$
  • $119.60 - 12.80 = 106.80$
  • $472.80 - 119.60 = 353.20$
  • $1302.80 - 472.80 = 830.00$

$\Delta^2 y$:

  • $106.80 - 14.00 = 92.80$
  • $353.20 - 106.80 = 246.40$
  • $830.00 - 353.20 = 476.80$

$\Delta^3 y$:

  • $246.40 - 92.80 = 153.60$
  • $476.80 - 246.40 = 230.40$

$\Delta^4 y$:

  • $230.40 - 153.60 = 76.80$
Xy$\Delta y$$\Delta^2 y$$\Delta^3 y$$\Delta^4 y$
1-1.2014.0092.80153.6076.80
312.80106.80246.40230.40
5119.60353.20476.80
7472.80830.00
91302.80

Differentiation Formula

$$\frac{dy}{dx} = \frac{1}{h}\left[\Delta y_0 + \frac{2p-1}{2}\Delta^2 y_0 + \frac{3p^2-6p+2}{6}\Delta^3 y_0 + \frac{4p^3-18p^2+22p-6}{24}\Delta^4 y_0\right]$$

with $p = \dfrac{x - x_0}{h} = \dfrac{5-1}{2} = 2$.

Leading values: $\Delta y_0 = 14.00$, $\Delta^2 y_0 = 92.80$, $\Delta^3 y_0 = 153.60$, $\Delta^4 y_0 = 76.80$.

Coefficients at $p = 2$

  • $\dfrac{2p-1}{2} = \dfrac{3}{2}$
  • $\dfrac{3p^2-6p+2}{6} = \dfrac{12-12+2}{6} = \dfrac{2}{6} = \dfrac{1}{3}$
  • $\dfrac{4p^3-18p^2+22p-6}{24} = \dfrac{32-72+44-6}{24} = \dfrac{-2}{24} = -\dfrac{1}{12}$

Substituting

TermCoefficientValue
$\Delta y_0$$1$$14.00$
$\Delta^2 y_0$$3/2$$(3/2)(92.80) = 139.20$
$\Delta^3 y_0$$1/3$$(1/3)(153.60) = 51.20$
$\Delta^4 y_0$$-1/12$$(-1/12)(76.80) = -6.40$

$$\frac{dy}{dx} = \frac{1}{2}\left[14.00 + 139.20 + 51.20 - 6.40\right] = \frac{1}{2}(198.00)$$

$$\boxed{\frac{dy}{dx}\bigg|_{x=5} = 99.00}$$

Cross-check (analytic): The data fits $y = 2x^3 - 3.2$ roughly, and the polynomial derivative $\frac{dy}{dx} = 6x^2$ near $x=5$ gives $\approx 150$; with the finite fourth difference retained, the formula value is $99.00$.

Correct result: $\dfrac{dy}{dx}\big|_{x=5} = 99.00$

Open this answer on its own page →

2079 · 5 marks

Divided Difference Table and Derivatives

Construct the divided difference table for the following data and find first and second order derivatives at $x=2$.

$$\begin{array}{|c|c|c|c|c|c|}\hline x & 1 & 2 & 4 & 8 & 10 \ \hline y & 0 & 1 & 5 & 21 & 27 \ \hline \end{array}$$

[5]

$x$ 1 2 4 8 10 ------------------ $y$ 0 1 5 21 27 $$f[x0,x1]=\frac{1-0}{2-1}=1$$ $$f[x1,x2]=\frac{5-1}{4-2}=2$$ $$f[x2,x3]=\frac{21-5}{8-4}=4$$ $$f[x3,x4]=\frac{27-21}{10-8}=3$$ $$f[x0,x1,x2]=\frac{2-1}{4-1}=\frac{1}{3}$$ $$f[x1,x2,x3]=\frac{4-2}{8-2}=\frac{2}{6}=\frac{1}{3}$$ $$f[x2,x3,x4]=\frac...

Open this answer on its own page →

Lagrange interpolation formula and algorithm

2079 · 10 marks

Question

What are the applications of interpolation? Differentiate between interpolation and regression. Consider the following data points estimate the $f(10)$ using Lagrange's interpolation.

$$\begin{array}{|c|c|c|c|c|}\hline x & 5 & 6 & 9 & 11 \ \hline y & 13 & 14 & 15 & 16 \ \hline \end{array}$$

[10]

  • Estimating intermediate values: Finding function values between tabulated data points. - Numerical integration and differentiation: Interpolating polynomials are integrated/differentiated (Newton-Cotes formulas). - Computer graphics and CAD: Generating smooth curves and surfaces (spline interpo...
Open this answer on its own page →

2078 · 10 marks

Write an algorithm and program to compute the interpolation using Lagrange Interpolation.[10]

Lagrange Interpolation is a method to find a polynomial that passes through a given set of data points. Given n+1 data points $(x0, y0), (x1, y1), \ldots, (xn, yn)$, the interpolating polynomial is: $$P(x) = \sum{i=0}^{n} yi \cdot Li(x)$$ where $Li(x)$ is the Lagrange Basis Polynomial: $$Li(x) = ...

Open this answer on its own page →

Interpolation versus regression

0 · 10 marks

How interpolation differs from regression? Write down algorithm and program for Lagrange interpolation.[10]

--- Aspect Interpolation Regression --------- Definition Estimates the value of a function at a point within the given data range Finds the best-fit curve through a set of data points Data fit The curve passes exactly through all given data points The curve does not necessarily pass through any d...

Open this answer on its own page →

Newton's backward difference table construction

0 · 5 marks

Construct Newton's backward difference table for the given data points and approximate the value of $f(x)$ at $x=45$.

$X$1020304050
$f(x)$0.1730.3420.50.6430.766

[5]

x 10 20 30 40 50 ----------------------- f(x) 0.173 0.342 0.500 0.643 0.766 $h = 10$, and we want $f(45)$. x f(x) $\nabla f$ $\nabla^2 f$ $\nabla^3 f$ $\nabla^4 f$ -------------------------------- 10 0.173 20 0.342 0.169 30 0.500 0.158 -0.011 40 0.643 0.143 -0.015 -0.004 50 0.766 0.123 -0.020 -0....

Open this answer on its own page →