# Numerical Analysis

### Exercise Session 2: Polynomial interpolation

The general interpolation problem can be summarized as follows. We consider a set of pairs $(x_i, y_i)$, $i=0, \ldots, n$ as a well as a basis for the polynomial space, $\phi_0(x), \phi_1(x), \ldots, \phi_n(x)$. The result of the interpolation can then be written as a linear combination of the basis vectors \begin{align*} f(x) = \sum_{k=0}^n a_k\phi(x) \end{align*}

We want the interpolation to pass through the pairs $(x_i, y_i)$ so that $f(x_i) = y_i$. In other words, we want to determine $a_k$ such that \begin{align*} f(x_i) = \sum_{k=0}^n a_k \phi_k(x_i) = y_i, \quad i=0, 1, \ldots, n \end{align*} which is a system of $n+1$ equations with $n+1$ unknowns. Using matrices, the problem can be written as \begin{align*} \mathbf{A} = \left[\begin{array}{ccc} \phi_0(x_0)& \ldots & \phi_n(x_0)\\ \phi_0(x_1)& \ldots & \phi_n(x_1)\\ \vdots & & \\ \phi_0(x_n)& \ldots & \phi_n(x_n) \end{array}\right] \left[\begin{array}{c} a_0\\ a_1\\ \vdots \\ a_n \end{array}\right] = \left[\begin{array}{c} y_0\\ y_1\\ \vdots\\ y_n \end{array}\right] \end{align*}

There exist several choices for the basis, including

• The monomial basis $\displaystyle \phi_k(x) = x^k$
• The Lagrange basis $\displaystyle \phi_k(x) = \prod_{\substack{j=0\\ j\neq k}}^n \left(\frac{x - x_j}{x_k -x_j}\right)$
• Newton basis $\phi_k(x) = \prod_{j=0}^{k-1} \left(x - x_j\right)$

where $k=0, \ldots, n$

In the case of the monomial basis, the matrix of the basis (which is known as the Vandermonde matrix) is given by

\begin{align*} \left[\begin{array}{ccccc} 1& x_0 & x_0^2 & \ldots & x_0^n\\ 1& x_1 & x_1^2 & \ldots & x_1^n\\ \vdots & & & & \\ 1& x_n & x_n^2 & & x_n^n \end{array}\right] \end{align*}

In the case of the Lagrange basis, the matrix $\mathbf{A}$ turns into \begin{align*} \left[\begin{array}{cccc} \ell_0(x_0)& \ell_1(x_0)& \ldots & \ell_n(x_0)\\ \ell_0(x_1)& \ell_1(x_1)& & \ell_n(x_1)\\ \vdots & & & \\ \ell_0(x_n)& \ell_1(x_n)& & \ell_n(x_n) \end{array}\right] \end{align*}

which, from the definition of the Lagrange polynomials, $\ell_k(x_k)=1$ and $\ell_n(x_i) = 0$ for all $i\neq k$ reduces to \begin{align*} \left[\begin{array}{cccc} 1& 0 &\ldots & 0\\ 0 & 1 & & \\ \vdots & & \ddots & \\ 0 & & & 1 \end{array}\right] \end{align*}

and the corresponding system reduces to $a_k = y_k$ for all $k$.

#### Exercise 1¶

Find the interpolating polynomial using the monomial basis (use the Gaussian elimination implementation '\' of Julia) and for the Lagrange basis (manually) for the data $(-1,-6),(1,0),(2,6)$

In [ ]:



#### Exercise 2.¶

Prove that $\sum_{k=0}^n \ell_k(x) = 1$ for all $x$ where $\ell_k$ are the Lagrange polynomials for $n+1$ points. (Hint verify the identity for $n=1$ for any data points). For the general case, think about what special function's interpolation polynomial in Lagrange form is $\sum_{k=0}^n \ell_k(x)$

In [ ]:



#### Exercise 3¶

Construct the Lagrange interpolation polynomial $p_1$ of degree $1$ for a continuous function $f$ defined on the interval $[-1,1]$, using the interpolation points $x_0 = -1$, $x_1 = 1$. Show that if the second derivative of $f$ exists and is continuous on $[0,1]$, then \begin{align*} \left|f(x) - p_1(x)\right|\leq \frac{M_2}{2}(1-x^2)\leq \frac{M_2}{2}, \quad x\in [-1,1] \end{align*} where $M_2 = \max_{x\in [-1,1]} |f''(x)|$

In [ ]:



#### Exercise 4¶

a) Write down the Lagrange interpolation polynomial of degree $1$ for the function $f\;:\; x\mapsto x^3$, using the points $x_0 = 0$, $x_1 = a$ Verify the bound below and prove that in this case, the $\xi$ is unique and satisfies $\xi = \frac{1}{3}(x+a)$

\begin{align*} f(x) - p_n(x) = \frac{f^{(n+1)}(\xi)}{(n+1)!} \pi_{n+1}(x) \end{align*}
In [ ]:



#### Exercise 5¶

Write down the Lagrange interpolation polynomial of degree $n$ for the given functions and nodes. Using Pyplot, plot the function, nodes and corresponding polynomial

• $f(x) = \sin(x)$, $n=2, x_0 = 0, x_1 = \pi/2$
• $f(x) = \sin(x)$, $n=2$, $x_0=0, x_1 = \pi/6, x_2 = \pi/2$,
• $f(x) = \cos(x)$, $n=2$, $x_0 = 0, x_1 = \pi/3, x_2 = \pi/2$
• $f(x) = \tanh(x)$, $n=2$, $x_0 = 0, x_1 = \pi/3, x_2 = \pi/2$
In [ ]:



#### Exercise 6¶

For each case below, plot the Lagrange polynomial on the interval $[x_0,x_n]$, over the interpolation points

• $n=2$, $x_0 = -1, x_1 = -0.2, x_2 = 0$, $\ell_2(x)$
• $n=4$, $x_0 = 0, x_1 = 1, x_2 = 1.5, x_3 = 2.5, x_4 = 3$, $\ell_3(x)$
• $n = 20$, $x_i=i/n$ for $i=0,\ldots n$, $\ell_{10}(x)$
In [ ]:



#### Exercise 7¶

For each function below, generate equispaced points on the $[0,1]$ interval. Then build the Lagrange interpolation polynomials of degree $2$, $3$ and $4$

• $\sin(10x) + \cos(9x) - x^2 +2$
• $\log(5x +1 + \sin(10*x)) + x^3+2x^2 +1 + \cos(9x)$
In [ ]:



#### Exercise 8¶

We consider the function $f(x) = \frac{1}{1+16x^2}$. Generate 15 equispaced points on the [-1,1] interval and plot the resulting Lagrange interpolation polynomial on top of the function and the sample. What do you notice ? How can we explain this phenomenon?

In [ ]:



#### Exercise 9¶

The Newton basis for polynomials up to degree $n$ is given by \begin{align*} \pi_k(x) = \prod_{j=0}^{k-1} (x-x_j), \quad k=0,1,\ldots, n \end{align*} the polynomial $\pi_0(x) = \prod_{j=0}^{-1} (x-x_j)$ is interpreted as the constant polynomial $1$.

From this, one can decompose a polynomial of degree $n$ as

\begin{align*} p_n(x) &= a_0\pi_0(x) + a_1\pi_1(x) + \ldots +a_n\pi_n(x)\\ & = a_0 + a_1(x-x_0) + a_2(x-x_0)(x-x_1) + \ldots + a_n(x-x_0)\ldots (x-x_{n-1}) \end{align*}

The coefficients of this decomposition can then be obtained as

\begin{align*} p_n(x_i) = a_0 + a_1(x_i - x_0) + \ldots + a_n(x_i - x_0)\ldots (x_i - x_{n-1}) = y_i \end{align*}

Or similarly

\begin{align*} \left[\begin{array}{ccccc} 1& 0 & 0 & \ldots & 0\\ 1& (x_1 - x_0)& 0 & & 0\\ 1& (x_1 - x_0) & (x_2 - x_0) (x_2 - x_1)& & 0\\ \vdots & \vdots & \vdots & & \vdots\\ 1& (x_n-x_0) & (x_n - x_0)(x_n-x_1) & \ldots & \prod_{i=0}^{n-1} (x_n - x_i) \end{array}\right]\left[\begin{array}{c} a_0\\ a_1\\ \vdots \\ a_n \end{array}\right] = \left[\begin{array}{c} y_0\\ y_1\\ \vdots \\ y_n \end{array}\right] \end{align*}

#### Exercise 9a¶

Find the interpolating polynomial using the Newton basis for the data $(-1,-6), (1,0)$ and $(2,6)$. Plot this polynomial on top of the samples.

In [ ]:



#### Exercise 9b¶

Compute by hand, the interpolating polynomial to the data $(-1,0), (0.5,1), (1, 0)$ using the monomial, Lagrange and Newton basis functions. verify that the three polynomials are identical.

In [ ]:



#### Exercise 10¶

Exercise 10a

We want to predict the risk of wild fish use as food for fish farming.

The tables below encode the evolution of Wild fish used as fishmeal and oil and the Aquaculture (fish farm) production. As a first approximation, use the data and learn interpolating polynomials to predict the evolution of those two quantities until 2030

\begin{align*} \begin{array}{|l|lllll|}\hline &1975&1985&1995&2005&2015\\ \hline \text{Wild fish used as animal feed} &13.68&21.91&24.88&24.34&14.60 \\ \text{Aquaculture production} &5.22&11.35&31.23&57.82&106\\ \hline \end{array} \end{align*}
In [ ]:



Exercise 10b

In 2006, a first study by Worm et al. predicted that by the year 2048, the ocean will be devoid of fish (as shown in the Fig below)

In a subsequent study, following some uproar from the community, rather than relying on reported data on fish catch, the same authors built a database on global fish stocks (i.e not the amount of fish being caught, but the abundance of fish in each population). Such a database can be done through a combination of sonar acoustic methods (which lets scientist build a picture of the number and density of fish) and fishing (which lets them identify specific species in the ecosystem).

The difference can be illstrated through the population of Tuna in the Atlantic ocean (see table below). Using this table, learn a polynomial model and predict the evolution of Tunas in the Atlantic ocean by 2048.

The data below corresponds to the biomass of a fish stock divided by the biomass at its maximum sustainable yield (The level at which we can catch the maximum amount of fish without a decline in fish populations. A value of one maximises fish catch without decreasing fish populations)

\begin{align*} \begin{array}{|l|lllll|}\hline &1975&1985&1995&2005&2015\\ \hline \text{Wild Atlantic Tuna} &1.61&1.34&1.04&0.80& 1.14 \\ \hline \end{array} \end{align*}
In [ ]: