Innlevering 4

Innleveringsfrist: fredag 17.04.2026 kl 16:00.

Du leverer besvarelsen din som to filer:

Oppgave 1: Finnes her. Legg til svarene dine og lever Jupyter notebook gjennom Ovsys2. [teller 25%]

Oppgavene 2-7: Finnes i Oppgave filen på wikisiden og besvarelsen leveres som en pdf fil gjennom Ovsys2. [teller 75%]

Innleveringer levert i feil format underkjennes.

Polynomial interpolation, piecewise-polynomial interpolation and adaptive quadrature.

The Python function interpolate( , , ) implements the Lagrange interpolation polynomial for an arbitrary set of distinct nodes $x_0,\dots, x_n$ and for values $y_0,\dots , y_n$. In what follows we test this code on equidistant nodes and on Chebishev nodes. The code works both for functions $f\in C^{\infty}$ defined on the interval $[-1,1]$ and for arbitrary intervals $[a,b]$ by applying the one-to-one transformation $\Psi:[-1,1]\rightarrow [a,b]$, $\Psi(x)=\frac{b-a}{2} x+\frac{b+a}{2}$.

interpolate( , , ) receives in input:

and gives in output: the value of the interpolation polynomial in $x$.

In what follows you can find a plot of the interpolation of the Runge function $f(x)=1/(x^2+1)$ with equidistant nodes and Chebishev nodes on the interval $[-5,5]$ with $n=10$.

For obtaining a nice plot, we evaluate the interpolation polynomial on a finer grid denoted here with "xfine".

Familiarize yourself with this code which you will have to use for answering the questions in this notebook.

a) The interpolation on equidistant nodes and on Chebishev nodes should converge for both the functions:

Can you use the theory learned in class and explain why? These functions are among those implemented in the preamble.

Explain here why we should get convergence with these two functions and on these two intervals. ...

b)To check convergence in a numerical experiment consider an approximation of $$\max_{x\in [a,b]}|f(x)-p_n(x)|,$$ and consider the behaviour as $n$ increases.

Consider a grid which has many more points than the maximal number of interpolation nodes: $x_0=\eta_0 <\eta_1 <\dots <\eta_N=x_n$ with $N$ ``large'', e.g. $N=10n_{\max}$, where $n_{\max}$ is the largest degree of the interpolation polynomial that you consider in your numerical experiments. Compute the following approximations $$\max_{x\in [a,b]}|f(x)-p_n(x)|\approx \max_{\eta_0,\dots, \eta_N} | f(\eta_i)-p_n(\eta_i) |.$$

Make a plot of the estimated error $\max_{x\in[a,b]}|p_n(x)-f(x)|$ as a function of $n$ to check convergence. Use semi-logarithmic plot (with logarithmic scale on the y-axis): "plt.semilogy".

Please complete the code below.

Piecewise-polynomial interpolation

c) Suppose $f$ is continuous and is the function you want to approximate, here we want to obtain a piece-wise polynomial and continuous approximation of $f$ on $[a,b]$.

The interval $[a,b]$ is subdivided in the disjoint union of $K$ subintervals $a= v_0 < v_1 < \dots < v_K=b$. Then we have implemented a method that performs Lagrangian interpolation on $n+1$ nodes on each subinterval $[v_i,v_{i+1}]$ (and we are using interpolate( , , )).
We use equidistant nodes on the subintervals. Fix $n=1,2,\dots, 10$.

The function "piecewiseinterpolation(x, a, b, K, n, f)" implements piecewise-polynomial interpolation on the interval [a,b] with $K$ subintervals and polynomials of degree $n$ on each subinterval. Assume the piecewise-polynomial approximation you obtain is called $\tilde{f}$.

Make a plot of the interpolation error $\max_{x\in[a,b]}|f(x)-\tilde{f}(x)|$ as a function of $K$, give numerical evidence that the method converges as $K\rightarrow \infty.$

d) In this part you find a suggestion for solving the exercise 2) in the multiple choice exerciseset 10. You can simply use the function interpolate( , , ) with appropriate input and requiring appropriate output and you can find which is the correct answer. Complete the code below to get your answer.

e) The following code is a simple implementation of Adaptive Simpson Quadrature. We want to make sure that it is correct.