2 Interpolation And Regression

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.

Interpolation and Regression: notes

Interpolation vs Extrapolation

Interpolation is the process of estimating the value of $y=f(x)$ at some $x$ that lies within the range of a given set of data points; it constructs a function that reproduces the known points exactly and reads intermediate values off that function. Extrapolation is the corresponding estimate for an $x$ that lies outside the range of the given data. A weather station recording the temperature every hour and reading off the value at a half-hour mark is interpolating; using the same readings to predict tomorrow's temperature is extrapolating. Interpolation is generally reliable because the estimate is anchored by data on both sides; extrapolation is riskier because the fitted function is being trusted in a region it was never shown, so its error grows quickly with distance from the known range.

Lagrange's Interpolation

The Lagrange interpolation polynomial of degree $n$ through $n+1$ points $(x_0,f_0), \ldots, (x_n,f_n)$ is written directly as a weighted sum of the function values, with no intermediate coefficients to solve for: $$P_n(x) = \sum_{i=0}^{n} f_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 Lagrange basis polynomial $L_i(x)$ is built so that it equals $1$ at $x_i$ and $0$ at every other node, which is what forces $P_n(x_i) = f_i$ for every node.

Worked example (first order). The upward velocity of a rocket is tabulated below.

$t$ (s)01015202530
$v$ (m/s)0227.04362.78517.35602.97901.67

Estimate $v(16)$ using the two nodes bracketing $t=16$, namely $t_0=15$ and $t_1=20$: $$L_0 = \frac{16-20}{15-20} = 0.8, \qquad L_1 = \frac{16-15}{20-15} = 0.2,$$ $$v(16) \approx 0.8(362.78) + 0.2(517.35) = 393.69 \text{ m/s}.$$

Worked example (second order). For $f(x)=e^x$ tabulated at $x_0=0, x_1=1, x_2=2$ ($f_0=1$, $f_1=2.7183$, $f_2=7.3891$), estimate $e^{1.2}$: $$L_0 = \frac{(1.2-1)(1.2-2)}{(0-1)(0-2)} = -0.08, \quad L_1 = \frac{(1.2-0)(1.2-2)}{(1-0)(1-2)} = 0.96, \quad L_2 = \frac{(1.2-0)(1.2-1)}{(2-0)(2-1)} = 0.12,$$ $$e^{1.2} \approx -0.08(1) + 0.96(2.7183) + 0.12(7.3891) = 3.4162.$$ The true value is $3.3201$, an error of about $2.9%$: a wide node spacing relative to the curvature of $e^x$ limits accuracy even for a second-order fit, which is why the node spacing (and order) must suit how fast the underlying function bends.

#include <stdio.h>

double lagrange(double x[], double y[], int n, double xp) {
    double result = 0.0;
    for (int i = 0; i < n; i++) {
        double term = y[i];
        for (int j = 0; j < n; j++) {
            if (j != i) {
                term *= (xp - x[j]) / (x[i] - x[j]);
            }
        }
        result += term;
    }
    return result;
}

int main(void) {
    double x[] = {15, 20};
    double y[] = {362.78, 517.35};
    printf("v(16) = %.2f\n", lagrange(x, y, 2, 16));
    return 0;
}

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]

Lagrange interpolation is a method of constructing a polynomial that passes through a given set of data points. Given n+1 data points (x₀, y₀), (x₁, y₁), ..., (xₙ, yₙ), the Lagrange interpolating polynomial is: $$Pn(x) = \sum{i=0}^{n} Li(x) \cdot f(xi)$$ where the basis polynomials (Lagrange coef...

Open this answer on its own page →

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]

X -2 -1 1 2 3 4 ----------------------- F(x) -19 0 2 -3 -4 5 Find the lowest degree polynomial through these 6 points, then estimate $f(0)$. Using Newton's Divided Difference Interpolation. First differences: $$f[x0,x1]=\frac{0-(-19)}{-1-(-2)}=\frac{19}{1}=19$$ $$f[x1,x2]=\frac{2-0}{1-(-1)}=\frac...

Open this answer on its own page →

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 answer

Newton's Backward Difference Interpolation to Estimate f(0.6)

Given

x0.10.20.30.40.5
f(x)2.683.043.383.693.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

xf(x)∇f∇²f∇³f∇⁴f
0.12.68
0.23.040.36
0.33.380.34-0.02
0.43.690.31-0.03-0.01
0.53.970.28-0.030.000.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}$$

Open this answer on its own page →

2080 · 10 marks

Define the terms interpolation and extrapolation. Write down the algorithm and program for Newton's divided difference interpolation.[10]

Show solved 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.

Open this answer on its own page →

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]

x 10 20 30 40 50 ------------------------ f(x) 0.985 0.934 0.866 0.766 0.643 - Step size: $h = 10$ - $xn = 50$, $f(xn) = 0.643$ - Interpolation point: $x = 45$ - $s = \dfrac{x - xn}{h} = \dfrac{45 - 50}{10} = -0.5$ $\nabla f = f(xi) - f(x{i-1})$ First differences: - $0.934 - 0.985 = -0.051$ -

Open this answer on its own page →

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]

Newton's interpolation constructs an interpolating polynomial through unequally (or equally) spaced data points using divided differences. Its form is: $$Pn(x) = f[x0] + fx0,x1 + fx0,x1,x2(x-x1) + \cdots$$ $i$ $xi$ $f(xi)$ ---------------------- 0 3.2 22.0 1 2.7 17.8 2 1.0 14.2 3 ...

Open this answer on its own page →

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]

x F(x) --------- 10 0.173 20 0.342 30 0.500 40 0.643 50 0.766 Spacing $h = 10$, $x0 = 10$. Required: $f(15)$. --- First differences ($\Delta$): - $0.342 - 0.173 = 0.169$ - $0.500 - 0.342 = 0.158$ - $0.643 - 0.500 = 0.143$ - $0.766 - 0.643 = 0.123$ Second differences ($\Delta^2$): -

Open this answer on its own page →

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 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$
21.448162.85.6
42.016642568.032.0
62.436216129614.486.4
82.664512409620.8166.4
102.810010001000028.0280.0
$\sum$11.222018001566474.0570.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.

Open this answer on its own page →

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]

$x$ 1 3 4 5 6 -------------------- $y$ 2 7 8 7 5 $n = 5$. Fit $y = a0 + a1 x + a2 x^2$ by least squares and find $f(2)$. --- $x$ $y$ $x^2$ $x^3$ $x^4$ $xy$ $x^2y$ --------------------- 1 2 1 1 1 2 2 3 7 9 27 81 21 63 4 8 16 64 256 32 128 5 7 25 125 625 35 175 6 5 36 216 1296 30 180 Σ 29 87 433 22...

Open this answer on its own page →

2079 · 5 marks

Fit the quadratic function for the data given below using least square method.

x1.01.52.02.53.03.54.0
f(x)2.74.05.88.311.215.019.0

[5]

$n = 7$ $x$ 1.0 1.5 2.0 2.5 3.0 3.5 4.0 ---------------------------------------- $y=f(x)$ 2.7 4.0 5.8 8.3 11.2 15.0 19.0 Model: $y = a0 + a1 x + a2 x^2$ $x$ $y$ $x^2$ $x^3$ $x^4$ $xy$ $x^2y$ --------------------------------------------- 1.0 2.7 1.00 1.000 1.0000 2.700 2.700 1.5 4.0 2.25 3.375 5.0...

Open this answer on its own page →

2077 · 5 marks

Fit the curve $y = ae^{bx}$ through the following data points.

x1234
y1.652.704.507.35

[5]

$x$ 1 2 3 4 ------------------------- $y$ 1.65 2.70 4.50 7.35 $n = 4$ Taking natural logarithm: $$\ln y = \ln a + bx$$ Let $Y = \ln y$, $A = \ln a$, $B = b$: $$Y = A + Bx$$ $x$ $y$ $Y=\ln y$ $x^2$ $xY$ -------------------------- 1 1.65 0.50078 1 0.50078 2 2.70 0.99325 4 1.98650 3 4.50 1.50408 9 4...

Open this answer on its own page →

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 answer

Cubic Spline Interpolation on [1, 3], Estimate f(2)

Given

i$x_i$$f_i$
01.01.5
13.04.5
24.09.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

QuantityValue
$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}$
Open this answer on its own page →

2079 · 10 marks

Spline Interpolation vs Lagrange Interpolation

Key Differences:

  1. 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.

  2. Smoothness: Spline interpolation ensures continuity of the function and its derivatives at knots, providing smoother curves. Lagrange interpolation may exhibit oscillations (Runge's phenomenon).

  3. Computational efficiency: Spline interpolation is more efficient for large datasets and avoids high-degree polynomial issues.

  4. 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]

Feature Lagrange Interpolation Cubic Spline Interpolation --------- Approach Single global polynomial of degree $(n-1)$ through all $n$ points Piecewise cubic polynomials between consecutive points Degree Grows with number of points Always cubic in each subinterval Oscillation Runge's phenomenon ...

Open this answer on its own page →

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]

When the number of data points is large, a single polynomial of degree n-1 must be fitted through all n points. This approach suffers from several serious problems: For a large number of data points, a high-degree interpolating polynomial tends to oscillate wildly between the data points, especia...

Open this answer on its own page →

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]

Linear regression is a curve-fitting technique that determines the best-fit straight line through a set of data points using the least squares principle, which minimizes the sum of squares of the residuals (vertical deviations) between observed values and the line. The model is: $$f(x) = a0 + a1 ...

Open this answer on its own page →

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]

X -1 1.2 2 2.7 3.6 4 --------------------------- F(x) 1 20 27 33 41 45 Number of points: $n = 6$ Normal equations for $y = a + bx$: $$\sum y = na + b\sum x$$ $$\sum xy = a\sum x + b\sum x^2$$ x y x² xy --------------------------- -1 1 1.00 -1.0 1.2 20 1.44 24.0 2 27 4.00 54.0 2.7 33 7.29 89.1 3.6...

Open this answer on its own page →

Regression vs Interpolation

2077 · 10 marks

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

Basis Interpolation Regression --------- Definition Estimating the value of a function at a point within the range of known data points Finding the best-fit curve that approximates the relationship between variables Data Fit The curve passes exactly through all given data points The curve does no...

Open this answer on its own page →

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]

Least squares approximation is a method of fitting a function (usually a polynomial) to a set of data points such that the sum of the squares of the errors (residuals) between the actual data values and the approximated function values is minimized. When we perform least squares approximation (re...

Open this answer on its own page →