Accuracy of Newton-Cotes

Copyright (C) 2020 Andreas Kloeckner

MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

A function to make Vandermonde matrices:

(Note that the ordering of this matrix matches the convention in our class but disagrees with np.vander.)

Fix a set of nodes:

Find the weights for the Newton-Cotes rule for the given nodes on $[0,1]$:

Here is a function and its definite integral from $0$ to $x$:

$$\text{int_f}(x)=\int_0^x f(\xi)d\xi$$

Plotted:

This here plots the function, the interpolant, and the area under the interpolant:

Compute the following:

Compare the error for $h=1,0.5,0.25$. What order of accuracy do you observe?

Estimate the order of accuracy:

We assume that the error depends on the mesh spacings $h$ as $E(h)\approx C h^p$ for some unknown power $p$. Taking the $\log$ of this approximate equality reveals a linear function in $p$: $$ E(h) \approx C h^p \quad \iff \quad \log E(h) \approx \log(C) + p\log(h). $$ You can now either do a least-squares fit for $\log C$ and $p$ from a few data points $(h,E(h))$ (more accurate, more robust), or you can use just two grid sizes $h_1$ and $h_2$, and estimate the slope: (less accurate, less robust) $$ p \approx \frac{ \log(\frac{E(h_2)}{E(h_1)}) } {\log(\frac{h_2}{h_1})}. $$ This is called the empirical order of convergence or EOC.