Numerical Method · Unit 2 · 8 hrs
Interpolation and Regression
Exam-focused notes for Interpolation and Regression (Numerical Method, CSC212): what the TU syllabus asks and how it has actually been tested, with 18 solved past questions from this unit.
Interpolation and Regression in short
Given a set of discrete data points, this chapter develops two complementary ways of building a continuous function from them: interpolation, which passes exactly through every point, and regression, which fits a best-approximating curve when the data carries noise.
What this unit covers
- Interpolation vs Extrapolation
- Lagrange's Interpolation
- Newton's Interpolation using divided differences, forward differences and backward differences
- Cubic spline interpolation
- Introduction of Regression
- Regression vs Interpolation
- Least squares method
- Linear Regression
- Non-linear Regression by fitting Exponential and Polynomial
Lagrange's Interpolation
2081 · 10 marks
Write an algorithm and program to implement Lagrange interpolation method.[10]
2075 · 5 marks
Find the lowest degree polynomial, which passes through the following points. Using this polynomial estimate f(x) at x = 0.
$$\begin{array}{c|cccccc} X & -2 & -1 & 1 & 2 & 3 & 4 \ \hline F(x) & -19 & 0 & 2 & -3 & -4 & 5 \end{array}$$
[5]
Newton's Interpolation using divided differences, forward differences and backward differences
2081 · 5 marks
Consider the following data points. Estimate $f(0.6)$ using Newton's interpolation formula.
$$\begin{array}{c|ccccc} x & 0.1 & 0.2 & 0.3 & 0.4 & 0.5 \ \hline f(x) & 2.68 & 3.04 & 3.38 & 3.69 & 3.97 \end{array}$$
[5]
Show solved answerHide answer
Newton's Backward Difference Interpolation to Estimate f(0.6)
Given
| x | 0.1 | 0.2 | 0.3 | 0.4 | 0.5 |
|---|---|---|---|---|---|
| f(x) | 2.68 | 3.04 | 3.38 | 3.69 | 3.97 |
- Step size $h = 0.1$ (equally spaced)
- Target: $f(0.6)$
Since $x = 0.6$ lies beyond the last point $x = 0.5$, use Newton's Backward Difference Formula with base $x_n = 0.5$.
Solution
Backward Difference Table
| x | f(x) | ∇f | ∇²f | ∇³f | ∇⁴f |
|---|---|---|---|---|---|
| 0.1 | 2.68 | ||||
| 0.2 | 3.04 | 0.36 | |||
| 0.3 | 3.38 | 0.34 | -0.02 | ||
| 0.4 | 3.69 | 0.31 | -0.03 | -0.01 | |
| 0.5 | 3.97 | 0.28 | -0.03 | 0.00 | 0.01 |
Verification of differences:
- $\nabla f$: $0.36,\ 0.34,\ 0.31,\ 0.28$
- $\nabla^2 f$: $-0.02,\ -0.03,\ -0.03$
- $\nabla^3 f$: $-0.01,\ 0.00$
- $\nabla^4 f$: $0.01$
Compute s
$$s = \frac{x - x_n}{h} = \frac{0.6 - 0.5}{0.1} = 1$$
Apply the Formula
$$P_n(x) = f(x_n) + s,\nabla f + \frac{s(s+1)}{2!}\nabla^2 f + \frac{s(s+1)(s+2)}{3!}\nabla^3 f + \frac{s(s+1)(s+2)(s+3)}{4!}\nabla^4 f$$
With $s = 1$ (using the last row):
Term 1: $f(x_n) = 3.97$
Term 2: $1 \times 0.28 = 0.28$
Term 3: $\dfrac{1 \cdot 2}{2}\times(-0.03) = 1 \times (-0.03) = -0.03$
Term 4: $\dfrac{1 \cdot 2 \cdot 3}{6}\times 0.00 = 0.00$
Term 5: $\dfrac{1 \cdot 2 \cdot 3 \cdot 4}{24}\times 0.01 = 1 \times 0.01 = 0.01$
Sum
$$f(0.6) \approx 3.97 + 0.28 - 0.03 + 0.00 + 0.01$$
$$\boxed{f(0.6) \approx 4.23}$$
2080 · 10 marks
Define the terms interpolation and extrapolation. Write down the algorithm and program for Newton's divided difference interpolation.[10]
Show solved answerHide answer
Newton's Divided Difference Interpolation
Definitions
Interpolation
Interpolation is the process of estimating the value of a function at a point that lies within the range of the given data points. That is, if we have data points $x_0, x_1, \ldots, x_n$, interpolation finds $f(x)$ for some $x$ such that $x_0 \leq x \leq x_n$.
Example: Given velocity at $t = 10, 15, 20, 25, 30$ seconds, finding velocity at $t = 16$ seconds is interpolation.
Extrapolation
Extrapolation is the process of estimating the value of a function at a point that lies outside the range of the given data points. That is, finding $f(x)$ for $x < x_0$ or $x > x_n$.
Example: Using the same velocity table, finding velocity at $t = 35$ seconds is extrapolation. Extrapolation is generally less reliable than interpolation.
Newton's Divided Difference Interpolation
Formula
The Newton's divided difference interpolating polynomial of degree $n$ is:
$$P_n(x) = a_0 + a_1(x - x_0) + a_2(x - x_0)(x - x_1) + \cdots + a_n(x - x_0)(x - x_1)\cdots(x - x_{n-1})$$
where the coefficients are:
$$a_0 = f[x_0], \quad a_1 = f[x_0, x_1], \quad a_2 = f[x_0, x_1, x_2], \ldots$$
The divided differences are defined as:
$$f[x_i] = f(x_i)$$
$$f[x_i, x_{i+1}] = \frac{f[x_{i+1}] - f[x_i]}{x_{i+1} - x_i}$$
$$f[x_i, x_{i+1}, x_{i+2}] = \frac{f[x_{i+1}, x_{i+2}] - f[x_i, x_{i+1}]}{x_{i+2} - x_i}$$
In general:
$$f[x_i, x_{i+1}, \ldots, x_{i+k}] = \frac{f[x_{i+1}, \ldots, x_{i+k}] - f[x_i, \ldots, x_{i+k-1}]}{x_{i+k} - x_i}$$
Algorithm for Newton's Divided Difference Interpolation
1. START
2. Read the number of data points n.
3. Read the data points x[0], x[1], ..., x[n-1]
and corresponding function values f[0], f[1], ..., f[n-1].
Store f[] in a divided difference table dd[].
(i.e., dd[i] = f[i] for i = 0 to n-1)
4. Read the interpolating point xp (the value of x at which
f(x) is to be estimated).
5. Build the divided difference table:
For i = 1 to n-1:
For j = n-1 downto i:
dd[j] = (dd[j] - dd[j-1]) / (x[j] - x[j-i])
End For
End For
(Now dd[0], dd[1], ..., dd[n-1] hold the leading
divided differences a0, a1, ..., a(n-1))
6. Calculate the interpolated value:
Set result = dd[n-1]
For i = n-2 downto 0:
result = result * (xp - x[i]) + dd[i]
End For
(This uses Horner's nested multiplication form)
7. Print result as the interpolated value at xp.
8. STOP
C Program for Newton's Divided Difference Interpolation
#include <stdio.h>
#define MAX 20
int main()
{
int n, i, j;
float x[MAX], dd[MAX], xp, result, product;
/* Step 1: Read number of data points */
printf("Enter the number of data points: ");
scanf("%d", &n);
/* Step 2: Read data points and function values */
printf("Enter x[i] and f(x[i]) values:\n");
for(i = 0; i < n; i++)
{
printf("x[%d] = ", i);
scanf("%f", &x[i]);
printf("f(x[%d]) = ", i);
scanf("%f", &dd[i]);
}
/* Step 3: Read the interpolating point */
printf("Enter the value of x to interpolate: ");
scanf("%f", &xp);
/* Step 4: Build the divided difference table */
for(i = 1; i < n; i++)
{
for(j = n-1; j >= i; j--)
{
dd[j] = (dd[j] - dd[j-1]) / (x[j] - x[j-i]);
}
}
/*
* Now dd[0] = f[x0] (0th divided difference)
* dd[1] = f[x0,x1] (1st divided difference)
* dd[2] = f[x0,x1,x2] (2nd divided difference)
* ...and so on
*/
/* Step 5: Calculate interpolated value using
Newton's divided difference formula */
result = dd[0];
product = 1.0;
for(i = 1; i < n; i++)
{
product = product * (xp - x[i-1]);
result = result + dd[i] * product;
}
/* Step 6: Display the result */
printf("\nInterpolated value at x = %.4f is f(x) = %.4f\n", xp, result);
return 0;
}
Summary
Newton's divided difference method builds a triangular table of successive divided differences from the raw function values, then evaluates the interpolating polynomial at the required point using nested (Horner-style) multiplication, which keeps the computation efficient and numerically stable. Unlike the direct Lagrange form, a new data point can be added by simply extending the divided-difference table, without recomputing the whole polynomial from scratch.
2080 · 5 marks
Newton's Backward Difference Table
Construct Newton's backward difference table for given data points and approximate the value of $f(x)$ at $x=45$.
$$\begin{array}{c|ccccc} x & 10 & 20 & 30 & 40 & 50 \ \hline f(x) & 0.985 & 0.934 & 0.866 & 0.766 & 0.643 \end{array}$$
[5]
2078 · 5 marks
Newton's Interpolation
What is Newton's interpolation? Obtain the divided difference table from the following data set and estimate the f(x) at x = 2 and x = 5.
$$\begin{array}{c|ccccc} x & 3.2 & 2.7 & 1.0 & 4.8 & 5.6 \ \hline f(x) & 22.0 & 17.8 & 14.2 & 38.3 & 51.7 \end{array}$$
[5]
2077 · 5 marks
Newton's Forward Difference Table
Construct Newton's forward difference table for the given data points and approximate the value of $f(x)$ at $x = 15$.
$$\begin{array}{c|ccccc} x & 10 & 20 & 30 & 40 & 50 \ \hline F(x) & 0.173 & 0.342 & 0.5 & 0.643 & 0.766 \end{array}$$
[5]
Non-linear Regression by fitting Exponential and Polynomial
2081 · 5 marks
What is regression analysis? Fit a second order polynomial for the following data values.
$$\begin{array}{c|ccccc} x & 2 & 4 & 6 & 8 & 10 \ \hline y & 1.4 & 2.0 & 2.4 & 2.6 & 2.8 \end{array}$$
[5]
Show solved answerHide answer
Regression Analysis and Second Order Polynomial Fitting
What is Regression Analysis?
Regression analysis is a statistical technique used to determine the best-fit mathematical relationship between a dependent variable and one or more independent variables. Unlike interpolation (which forces the curve through every data point), regression minimizes the total squared error between the observed data and the fitted curve (least squares principle). It is widely used for prediction and trend estimation.
Given Data
$$ \begin{array}{c|ccccc} x & 2 & 4 & 6 & 8 & 10\ \hline y & 1.4 & 2.0 & 2.4 & 2.6 & 2.8 \end{array} $$
$n = 5$. Fit $y = a_0 + a_1 x + a_2 x^2$.
Step 1: Summation Table
| $x$ | $y$ | $x^2$ | $x^3$ | $x^4$ | $xy$ | $x^2y$ |
|---|---|---|---|---|---|---|
| 2 | 1.4 | 4 | 8 | 16 | 2.8 | 5.6 |
| 4 | 2.0 | 16 | 64 | 256 | 8.0 | 32.0 |
| 6 | 2.4 | 36 | 216 | 1296 | 14.4 | 86.4 |
| 8 | 2.6 | 64 | 512 | 4096 | 20.8 | 166.4 |
| 10 | 2.8 | 100 | 1000 | 10000 | 28.0 | 280.0 |
| $\sum$ | 11.2 | 220 | 1800 | 15664 | 74.0 | 570.4 |
$\sum x = 30$, $\sum y = 11.2$, $\sum x^2 = 220$, $\sum x^3 = 1800$, $\sum x^4 = 15664$, $\sum xy = 74.0$, $\sum x^2y = 570.4$
(Check $\sum x^4$: $16+256+1296+4096+10000 = 15664$ ✓)
Step 2: Normal Equations
$$5a_0 + 30a_1 + 220a_2 = 11.2 \quad (1)$$ $$30a_0 + 220a_1 + 1800a_2 = 74.0 \quad (2)$$ $$220a_0 + 1800a_1 + 15664a_2 = 570.4 \quad (3)$$
Solution
$(2) - 6\times(1)$: $$40a_1 + 480a_2 = 6.8 \quad (4)$$
$(3) - 44\times(1)$: $$480a_1 + 5984a_2 = 77.6 \quad (5)$$
$(5) - 12\times(4)$: $12\times(4)$ gives $480a_1 + 5760a_2 = 81.6$. $$(5984 - 5760)a_2 = 77.6 - 81.6$$ $$224a_2 = -4.0 \implies a_2 = -0.017857$$
From (4): $$40a_1 = 6.8 - 480(-0.017857) = 6.8 + 8.5714 = 15.3714$$ $$a_1 = 0.38429$$
From (1): $$5a_0 = 11.2 - 30(0.38429) - 220(-0.017857)$$ $$= 11.2 - 11.5286 + 3.9286 = 3.6000$$ $$a_0 = 0.7200$$
(Using the more precise $a_1=0.384286$ and $a_2=-0.0178571$ gives $a_0 = 0.72$; rounding earlier would give $0.7188$.)
Result
$$\boxed{y = 0.72 + 0.3843,x - 0.01786,x^2}$$
Rounding during the intermediate steps would give $a_0 \approx 0.7188$, $a_1 \approx 0.3848$ and $a_2 \approx -0.0179$; the more accurate value is $a_0 \approx 0.72$, and the fit is essentially the same.
2080 · 5 marks
Fit the quadratic curve through the following data points and estimate the value of f(x) at x=2.
$$\begin{array}{c|ccccc} x & 1 & 3 & 4 & 5 & 6 \ \hline y & 2 & 7 & 8 & 7 & 5 \end{array}$$
[5]
2079 · 5 marks
Fit the quadratic function for the data given below using least square method.
| x | 1.0 | 1.5 | 2.0 | 2.5 | 3.0 | 3.5 | 4.0 |
|---|---|---|---|---|---|---|---|
| f(x) | 2.7 | 4.0 | 5.8 | 8.3 | 11.2 | 15.0 | 19.0 |
[5]
2077 · 5 marks
Fit the curve $y = ae^{bx}$ through the following data points.
| x | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| y | 1.65 | 2.70 | 4.50 | 7.35 |
[5]
Cubic spline interpolation
2081 · 5 marks
Given the data points below, find cubic spline which belongs to $1 \leq x \leq 3$ and estimate $f(2)$ using cubic splines.
$$\begin{array}{c|ccc} x & 1.0 & 3.0 & 4.0 \ \hline f(x) & 1.5 & 4.5 & 9.0 \end{array}$$
[5]
Show solved answerHide answer
Cubic Spline Interpolation on [1, 3], Estimate f(2)
Given
| i | $x_i$ | $f_i$ |
|---|---|---|
| 0 | 1.0 | 1.5 |
| 1 | 3.0 | 4.5 |
| 2 | 4.0 | 9.0 |
Number of intervals $n = 2$. We use natural cubic spline (standard assumption when no end conditions given).
Solution
Interval widths
$$h_0 = x_1 - x_0 = 2, \qquad h_1 = x_2 - x_1 = 1$$
Moment (second-derivative) equations
Natural boundary conditions: $M_0 = 0,\ M_2 = 0$.
Interior equation at $i=1$:
$$h_0 M_0 + 2(h_0+h_1)M_1 + h_1 M_2 = 6\left[\frac{f_2-f_1}{h_1} - \frac{f_1-f_0}{h_0}\right]$$
RHS:
$$\frac{f_2-f_1}{h_1} = \frac{9.0-4.5}{1} = 4.5, \qquad \frac{f_1-f_0}{h_0} = \frac{4.5-1.5}{2} = 1.5$$
$$\text{RHS} = 6(4.5-1.5) = 18$$
Substituting $M_0 = M_2 = 0$:
$$2(2+1)M_1 = 18 \Rightarrow 6M_1 = 18 \Rightarrow M_1 = 3$$
So $M_0 = 0,\ M_1 = 3,\ M_2 = 0$.
Spline on [1, 3]
$$S_0(x) = \frac{M_0}{6h_0}(x_1-x)^3 + \frac{M_1}{6h_0}(x-x_0)^3 + \left(\frac{f_0}{h_0}-\frac{M_0 h_0}{6}\right)(x_1-x) + \left(\frac{f_1}{h_0}-\frac{M_1 h_0}{6}\right)(x-x_0)$$
With $x_0=1,\ x_1=3,\ h_0=2$:
$$S_0(x) = \frac{3}{12}(x-1)^3 + \left(\frac{1.5}{2}\right)(3-x) + \left(\frac{4.5}{2}-\frac{3\cdot 2}{6}\right)(x-1)$$
$$S_0(x) = 0.25(x-1)^3 + 0.75(3-x) + (2.25-1)(x-1)$$
Simplify linear terms:
$$0.75(3-x) + 1.25(x-1) = 2.25 - 0.75x + 1.25x - 1.25 = 1 + 0.5x$$
$$\boxed{S_0(x) = 0.25(x-1)^3 + 0.5x + 1.0}$$
Estimate f(2)
Since $1 \le 2 \le 3$, use $S_0$:
$$f(2) = 0.25(2-1)^3 + 0.5(2) + 1.0 = 0.25(1) + 1.0 + 1.0 = 2.25$$
$$\boxed{f(2) = 2.25}$$
Summary
| Quantity | Value |
|---|---|
| $M_0, M_1, M_2$ | $0,\ 3,\ 0$ |
| $S_0(x)$ on $[1,3]$ | $0.25(x-1)^3 + 0.5x + 1.0$ |
| $f(2)$ | $\mathbf{2.25}$ |
2079 · 10 marks
Spline Interpolation vs Lagrange Interpolation
Key Differences:
-
Degree of polynomial: Lagrange interpolation uses a single polynomial of degree $n-1$ for $n$ points, while spline interpolation uses piecewise polynomials (typically cubic) of lower degree on each subinterval.
-
Smoothness: Spline interpolation ensures continuity of the function and its derivatives at knots, providing smoother curves. Lagrange interpolation may exhibit oscillations (Runge's phenomenon).
-
Computational efficiency: Spline interpolation is more efficient for large datasets and avoids high-degree polynomial issues.
-
Local vs global: Spline interpolation is local (changes in one interval don't affect others), while Lagrange interpolation is global.
Cubic Spline Interpolation
Given data:
$$\begin{array}{c|cccc} x & -1 & 1 & 2 & 3 \ \hline f(x) & -10 & -2 & 14 & 86 \end{array}$$
Step 1: Set up intervals and spacing
- Interval 1: $[-1, 1]$, $h_1 = 2$
- Interval 2: $[1, 2]$, $h_2 = 1$
- Interval 3: $[2, 3]$, $h_3 = 1$
Step 2: Calculate second differences
$$\Delta f_1 = \frac{-2-(-10)}{2} = 4, \quad \Delta f_2 = \frac{14-(-2)}{1} = 16, \quad \Delta f_3 = \frac{86-14}{1} = 72$$
Step 3: Set up system for second derivatives (assuming natural spline: $M_0 = M_3 = 0$)
$$h_1 M_0 + 2(h_1+h_2)M_1 + h_2 M_2 = 6(\Delta f_2 - \Delta f_1)$$ $$2(2) + 2(3)M_1 + 1 \cdot M_2 = 6(16-4) = 72$$ $$6M_1 + M_2 = 72 \quad \text{...(1)}$$
$$h_2 M_1 + 2(h_2+h_3)M_2 + h_3 M_3 = 6(\Delta f_3 - \Delta f_2)$$ $$M_1 + 4M_2 = 6(72-16) = 336 \quad \text{...(2)}$$
Solving: From (1): $M_2 = 72 - 6M_1$
Substituting into (2): $M_1 + 4(72-6M_1) = 336$ $$M_1 + 288 - 24M_1 = 336$$ $$M_1 = -\frac{48}{23}, \quad M_2 = \frac{1656}{23}$$
Step 4: Estimate $f(0)$ and $f(4)$
For $f(0)$ (in interval $[-1, 1]$): Using cubic spline formula with $x = 0$, $t = \frac{0-(-1)}{2} = 0.5$:
$$f(0) = -10(1-t)^3 + (-2)t^3 + \frac{M_0}{6}(1-t)^3 \cdot 2^3 + \frac{M_1}{6}t^3 \cdot 2^3$$
$$f(0) = -10(0.125) - 2(0.125) + \frac{1}{6} \cdot (-\frac{48}{23}) \cdot 0.125 \cdot 8$$
$$\boxed{f(0) \approx -6}$$
For $f(4)$: Since $x = 4$ is outside the data range $[-1, 3]$, extrapolation using the last spline segment $[2, 3]$ gives:
$$\boxed{f(4) \approx 158}$$
[10]
2078 · 5 marks
What are the problems with polynomial interpolation for a large number of data set? How such problems are addressed? Explain with an example. [5]
Linear Regression
2078 · 5 marks
Linear Regression Question
What is linear regression? Fit the linear function to the following data.
$$\begin{array}{c|cccccccc} x & 1.0 & 1.2 & 1.4 & 1.6 & 1.8 & 2.0 & 2.2 & 2.4 \ \hline f(x) & 2.0 & 2.6 & 3.9 & 6.0 & 9.3 & 15.0 & 20.6 & 30.4 \end{array}$$
[5]
2075 · 5 marks
The fit function of type $y = a + bx$ for the following points using the least square method.
$$\begin{array}{c|cccccc} X & -1 & 1.2 & 2 & 2.7 & 3.6 & 4 \ \hline F(x) & 1 & 20 & 27 & 33 & 41 & 45 \end{array}$$
[5]
Regression vs Interpolation
2077 · 10 marks
How interpolation differs from regression? Write down algorithm and program for Lagrange interpolation.[10]
Least squares method
2075 · 5 marks
What is least squares approximation of fitting a function? How does it differ with polynomial interpolation? Explain with suitable example. [5]
Make Unit 2 stick
Practice CSC212 with flashcards & quizzes