Assignment 5 onlineLecture 14 (Pivoted LU decomposition) onlineLecture 13 (Linear systems of equations) onlineLecture 12 (Gaussian quadrature) onlineA4 onlineTypos in L1-11 fixed, Quiz 3 in the exercise bank, solutions published, study guide availableLecture 10/11: Newton-Cotes quadratureLecture 9: Hermite interpolation availableAssignment 3 availableLecture 8 onlineLecture 7 onlineSchedule now available in "Admin"Problem class exercises now onlineLecture 6 onlineQuiz 1 solutions onlineLectures 2-3 exercises have solutions!Lecture 1 exercises have solutions!
These lecture notes are mainly a record of what we discussed in class. They should not be used as a replacement for attending lectures or reading books. Please let me know if you find any errors or have any questions about the material.
TipHypothesis
You can use the Hypothesis commenting system available on the right side panel (you can login with your U/Google account). This allows you annotate the notes (private) and leave comments or reply to other people’s comments (public).
Note
I encourage you to download the lecture notes and play around with the Julia code.
Hermite interpolation: given distinct nodes \(z_0,\dots,z_k\) with multiplicities \(m_0,\dots,m_k\) (forming a multiset \(X = \{\{x_0,\dots,x_n\}\}\)), find \(p \in \mathcal P_n\) with \(p^{(l)}(z_j) = f^{(l)}(z_j)\) for \(l = 0,\dots,m_j-1\),
There exists a unique solution (Theorem 9.1); Taylor expansion (one node repeated \(n+1\) times) and Lagrange interpolation (all \(m_j = 1\)) are the two extremes,
Divided differences with repeated nodes: \(f[x_0,\dots,x_n] = \frac{f^{(n)}(x_0)}{n!}\) if all nodes are equal, otherwise the usual recursion (Theorem 9.2),
Newton form \(p = \sum_{k=0}^n f[x_0,\dots,x_k] \, \pi_k\) (Theorem 9.3) computed from the divided difference table,
Compute the divided difference table for \(f(x) = x^2\) on \(\{0, 1, 2\}\) and write down the Newton form of the interpolant. What do you notice?
What are \(f[x_0, x_0]\) and \(f[x_0, x_0, x_0]\)?
Why do we prefer the barycentric formula (8.3) to the naive Lagrange formula?
What is the Hermite interpolant of \(f\) on \(\{\{a, a\}\}\)? How many conditions does the Hermite interpolant on \(\{\{0, 0, 1\}\}\) satisfy, and what is its degree?
The next iterate of Newton’s method is the root of which interpolant of \(f\)? What about the secant method?
\(f[0] = f(0) = 0\), \(f[1] = f(1) = 1\), \(f[2] = f(2) = 4\); \(f[0,1] = \frac{f(1)-f(0)}{1-0} = 1\), \(f[1,2] = \frac{f(1)-f(2)}{1-2} = 3\); \(f[0,1,2] = \frac{f[1,2] - f[0,1]}{2-0} = \frac{3 - 1}{2 - 0} = 1\). So \(p(x) = 0 + 1 \cdot x + 1 \cdot x(x-1) = x^2\): the interpolant is \(f\) itself, as it must be since \(f \in \mathcal P_2\).
\(f[x_0, x_0] = f'(x_0)\) and \(f[x_0, x_0, x_0] = \frac{f''(x_0)}{2}\).
Once the weights are known, the barycentric formula costs \(O(n)\) operations per evaluation (the naive formula costs \(O(n^2)\)), it is numerically stable, and the weights may be rescaled to avoid overflow/underflow (Exercise 8.1, Exercise 8.3).
The tangent line \(p(x) = f(a) + f'(a)(x - a)\) (the degree \(1\) Taylor polynomial). On \(\{\{0,0,1\}\}\) there are \(3\) conditions (\(p(0) = f(0)\), \(p'(0) = f'(0)\), \(p(1) = f(1)\)) and \(p \in \mathcal P_2\).
Newton: \(x_{n+1}\) is the root of the Hermite interpolant on \(\{\{x_n, x_n\}\}\) (the tangent line). Secant: \(x_{n+1}\) is the root of the Lagrange interpolant on \(\{x_{n-1}, x_n\}\) (the secant line).
10.2 Quadrature rules
Exercise 10.2 (Why approximate integrals?)
Can you compute \(\int_0^1 e^{-x^2} \,\mathrm{d}x\) or \(\int_0^1 \frac{\sin x}{x} \,\mathrm{d}x\) exactly?
No: neither integrand has an antiderivative that can be written in terms of elementary functions. (Also, in practice we may only know \(f\) at some points.) This is why we need quadrature rules.
with \(\lim_{x\to\infty}\mathrm{erf}(x)=1\) and \(\mathrm{Si}(x) \to \frac{\pi}{2}\).)
Definition 10.1 We wish to consider quadrature rules of the following form:
\[
\begin{align}
\int_{a}^b f &\approx \sum_j w_j f(x_j).
\end{align}
\tag{10.1}\]
We call \(\{w_j\}\) the quadrature weights, \(\{x_j\}\) the quadrature nodes and \((\{w_j\}, \{x_j\})\) or \(\sum_{j} w_j f(x_j)\) a quadrature rule.
Suppose \(p = I_X f\) is the polynomial of degree at most \(n\) interpolating \(f\) on \(X = \{x_0,\dots,x_n \}\): that is \(p(x) = \sum_{j=0}^n \ell_j(x) f(x_j)\). We simply approximate the integral of \(f\) by instead integrating \(p\):
\[
\begin{align}
\int_{a}^b f &\approx \int_a^b p \nonumber\\
%
&= \sum_{j=0}^n \Big[\int_a^b\ell_j(x)\mathrm{d}x \Big] f(x_j)
\end{align}
\tag{10.2}\]
which gives a quadrature rule with weights \(w_j := \int_a^b\ell_j(x)\mathrm{d}x\) and nodes \(x_j\).
Definition 10.2 When the interpolation nodes \(x_0, \dots, x_n\) are equispaced, (10.2) is a Newton–Cotes quadrature rule.
Example 10.1 In the following, we will consider the example of integrating \(f(x) = \tfrac1{2\pi} ( x + x \sin x)\) between \(0\) and \(2\pi\). In this particular case, the integral is \(\pi-1\). We use this example so that we can analyse the error committed in the methods we will introduce.
Exercise 10.3
Show that \(\int_0^{2\pi} \frac{1}{2\pi}(x + x \sin x) \,\mathrm{d}x = \pi - 1\).
Integrating by parts gives
\[\begin{align}
\int_0^{2\pi} x \sin x \,\mathrm{d}x
%
&= \int_0^{2\pi} x (-\cos' x) \,\mathrm{d}x \nonumber\\
%
&= \big[ -x \cos x \big]_0^{2\pi} + \int_0^{2\pi} \cos x \,\mathrm{d}x \nonumber\\
%
&= -2\pi.
\end{align}\]
Therefore the integral of \((x+x\sin x)/2\pi\) on \([0,2\pi]\) is \(\frac{1}{2\pi} \big( 2\pi^2 - 2\pi \big) = \pi - 1\).
Code
a, b =0, 2πf = x -> x * ( 1+sin(x) ) /( 2π )df_max =1+1/(2π)# maximum of |f''|, |f'''|, |f''''| on [a,b] (computed on a fine grid)d2f = x -> ( 2cos(x) - x *sin(x) ) /( 2π )d3f = x -> ( -3sin(x) - x *cos(x) ) /( 2π )d4f = x -> ( -4cos(x) + x *sin(x) ) /( 2π )X =range(a, b, 10^5)d2f_max =maximum( abs.( d2f.(X) ) )d3f_max =maximum( abs.( d3f.(X) ) )d4f_max =maximum( abs.( d4f.(X) ) )plot( f, a, b, title=L"f(x) = \frac{1}{2\pi} (x + x\sin x ) ", lw =3, legend=false)
10.3 Rectangular rule
The simplest rule of this kind is taking \(X = \{a\}\) and considering the polynomial interpolation of degree \(0\): \(p_0(x) = f(a)\),
The rule has weights \(\{ b-a \}\) and nodes \(\{ a \}\). Notice that this rule is exact if \(f\) is a polynomial of degree \(0\).
Exercise 10.4 (Interpolation error with one node)
Write down the interpolation error formula for \(f - p\) when \(p\) interpolates \(f\) on \(n+1\) nodes (Theorem 6.2). What does it say when \(n = 0\) and \(X = \{a\}\)?
We have: for \(x\), there exists \(\xi_x\) between \(\min\{ x, \min X\}\) and \(\max\{x,\max X\}\) such that
where \(\ell_X\) is the node polynomial on \(X\). For \(n = 0\) and \(X = \{a\}\), \(p = f(a)\) and this reads \(f(x) - f(a) = f'(\xi_x)(x - a)\) for some \(\xi_x\) between \(a\) and \(x\) (which is just the mean value theorem)
Composite rule. This error bound is only useful if \([a,b]\) is a small interval (or \(f'\) is small). It therefore makes sense to split the interval \([a,b]\) into \(n\) subintervals and consider the rectangular rule on each one: consider \(x_j = a + j\frac{b-a}{n}\) for \(j = 0,\dots,n\) and write
\[
\begin{align}
\int_a^b f &= \sum_{j=0}^{n-1} \int_{x_j}^{x_{j+1}} f \nonumber\\
%
&\approx \sum_{j=0}^{n-1} (x_{j+1} - x_j) f(x_j).
\end{align}
\]
This is a quadrature rule with weights \(w_j = x_{j+1} - x_j = \frac{b-a}{n}\) and nodes \(x_j\) for \(j = 0,\dots,n-1\).
Exercise 10.6
Approximate \(\int_0^1 x \,\mathrm{d}x\) by the composite rectangular rule with \(n\) subintervals of equal length. What is the error?
First, note that \(\int_0^1 x \mathrm{d}x = \frac12\).
The composite rectangular rule on \([0,1]\) is \(\frac1n \sum_{j=0}^{n-1} \frac{j}{n} = \frac{n-1}{2n} = \frac12 - \frac{1}{2n}\), so the error is exactly \(\frac{1}{2n}\): the rate \(O(n^{-1})\) of Theorem 10.1 (and the constant \(\frac{\|f'\|_{L^\infty}}{2}(b-a)^2 = \frac12\) is attained).
Theorem 10.1 Suppose \(f \in C^1([a,b])\). Then the composite rectangular rule satisfies
\[
\begin{align}
\left| \int_a^b f - \sum_{j=0}^{n-1} \frac{b-a}{n} f(x_j) \right| &\leq \frac{\| f'\|_{L^\infty([a,b])}}{2} \frac{ (b-a)^2 }{ n }.
\end{align}
\]
Proof. Using Exercise 10.5 on each subinterval \([x_j, x_{j+1}]\) and summing over \(j\), we obtain
When \(f(a), f(b) \geq 0\), this is the area of the trapezium with vertices \((a,0), (a,f(a)), (b, f(b)), (b,0)\): width \(b-a\) times the average height \(\frac{f(a) + f(b)}{2}\).
This rule has weights \(\{ \frac{b-a}{2}, \frac{b-a}{2} \}\) and nodes \(\{ a, b \}\). Notice that this rule is exact if \(f\) is a polynomial of degree \(1\) (because \(f\) agrees with \(p_1\) in this case).
Exercise 10.7
Write down the polynomial \(p_1\) of degree at most \(1\) interpolating \(f\) on \(X = \{a, b\}\).
Show that \(\int_a^b p_1(x) \,\mathrm{d}x = \frac{b-a}{2} \big( f(a) + f(b) \big)\).
You can either go through the details in the integration, or draw a picture and calculate the area of the trapezium with points \((a,0), (a,f(a)), (b, f(b)), (b,0)\). If you do the integration explicitly, the Newton form is probably slightly easier…
Lagrange form:
We have \(p_1(x) = \frac{x-b}{a-b}f(a) + \frac{x-a}{b-a} f(b)\).
Composite rule. This error bound is only useful if \([a,b]\) is a small interval (or \(f''\) is small). It therefore makes sense to split the interval \([a,b]\) into \(n\) subintervals and consider the trapezium rule on each one: consider \(x_j = a + j\frac{b-a}{n}\) for \(j = 0,\dots,n\) and write
We can see here that the error decays with rate \(O(n^{-2})\) and the error bound (proved above) is in fact an upper bound!
10.5 Summary
Rectangular rule is given by \((b-a) f(a)\) and leads to a composite rule with error \(C \|f'\|_{L^\infty([a,b])} n^{-1}\) (Theorem 10.1),
Trapezium rule is given by \(\frac{b-a}{2} \left( f(a) + f(b) \right)\) and leads to a composite rule with error \(C \|f''\|_{L^\infty([a,b])} n^{-2}\) (Theorem 10.3).
10.6 Next time….
Simpson’s rule: integrating the quadratic interpolant on \(\{a, \tfrac{a+b}{2}, b\}\),
Simpson’s rule is exact for cubics, and an improved error estimate using Hermite interpolation.
TipTO-DO
Exercises throughout the notes,
(Sign-up to office hours if you like)
10.7 Extra exercises
Exercise 10.9 (Midpoint rule)
The midpoint rule is \(\int_a^b f \approx (b-a) f\big(\tfrac{a+b}{2}\big)\).
Show that the midpoint rule is exact for all polynomials of degree at most \(1\).
Suppose \(f \in C^2([a,b])\). Show that \[
\left| \int_a^b f - (b-a) f\big(\tfrac{a+b}{2}\big) \right| \leq \frac{\|f''\|_{L^\infty([a,b])}}{24} (b-a)^3.
\]
Write down the composite midpoint rule on \(n\) subintervals of equal length and show that its error is bounded by \(\frac{\|f''\|_{L^\infty([a,b])}}{24} \frac{(b-a)^3}{n^2}\). Compare with Theorem 10.3.
For 2, use Taylor’s theorem about \(m = \frac{a+b}{2}\):
For \(f = 1\): \(\int_a^b 1 = b - a = (b-a) \cdot 1\). For \(f = x - m\): \[
\begin{align}
\int_a^b (x - m) \,\mathrm{d}x &= 0
\nonumber\\
%
&= (b-a)(m - m).
\end{align}
\]
By Taylor’s theorem, \[
\begin{align}
f(x) = f(m) + f'(m)(x-m) + \frac{f''(\xi_x)}{2} (x-m)^2
\end{align}
\] for some \(\xi_x\) between \(m\) and \(x\). Integrating and using \(\int_a^b (x - m) \,\mathrm{d}x = 0\), \[
\begin{align}
\left| \int_a^b f - (b-a) f(m) \right|
%
&= \left| \int_a^b \frac{f''(\xi_x)}{2} (x-m)^2 \,\mathrm{d}x \right| \nonumber\\
%
&\leq \frac{\|f''\|_{L^\infty([a,b])}}{2} \int_a^b (x - m)^2 \,\mathrm{d}x
\nonumber\\
%
&= \frac{\|f''\|_{L^\infty([a,b])}}{2} \frac{(b-a)^3}{12}.
\end{align}
\]
With \(x_j = a + j \frac{b-a}{n}\), the composite rule is \(\frac{b-a}{n} \sum_{j=0}^{n-1} f\big( \frac{x_j + x_{j+1}}{2} \big)\). Applying 2 on each subinterval of length \(\frac{b-a}{n}\) and summing the \(n\) errors gives \[
\begin{align}
n \cdot \frac{\|f''\|_{L^\infty([a,b])}}{24} \Big( \frac{b-a}{n} \Big)^3
%
&= \frac{\|f''\|_{L^\infty([a,b])}}{24} \frac{(b-a)^3}{n^2}.
\end{align}
\] This is the same rate \(O(n^{-2})\) as the composite trapezoid rule, but with half the constant (and one fewer function evaluation).
Exercise 10.10 (Properties of interpolatory weights)
Let \(x_0, \dots, x_n \in [a,b]\) be distinct and let \(w_j = \int_a^b \ell_j\) be the weights in (10.2).
Show that \(\{w_j\}\) are the unique weights for which \(\sum_{j=0}^n w_j f(x_j)\) is exact for all \(f \in \mathcal P_n\).
Show that \(\sum_{j=0}^n w_j = b - a\).
Let \(\hat x_j \in [0,1]\) and let \(\hat w_j\) be the corresponding weights on \([0,1]\). Show that the nodes \(x_j = a + (b-a) \hat x_j\) on \([a,b]\) have weights \(w_j = (b-a) \hat w_j\).
Suppose the nodes are symmetric: \(x_{n-j} = a + b - x_j\) for all \(j\). Show that \(w_{n-j} = w_j\).
What happens if you apply the rule to \(f(x) = \ell_j(x)\)?
What happens if you apply the rule to \(f(x) = 1\)?
Use \(w_j = \int_a^b \ell_j\),
Show that \(\ell_{n-j}(x) = \ell_j(a + b - x)\) by checking the values of both sides at the nodes.
If \(f \in \mathcal P_n\) then \(f\) is its own interpolant, \(f = \sum_j \ell_j f(x_j)\), and so \(\int_a^b f = \sum_j w_j f(x_j)\) and the rule is exact. Conversely, if \(\sum_j v_j f(x_j)\) is exact on \(\mathcal P_n\), then applying it to \(\ell_j \in \mathcal P_n\) gives \(v_j = \sum_k v_k \ell_j(x_k) = \int_a^b \ell_j = w_j\), as required.
Apply 1 to \(f = 1\): \(b - a = \int_a^b 1 = \sum_j w_j\).