Week 2 - Interpolation#
Interpolation#
Construct a continuous function f(x) through discrete data points (x_j,y_j).
| Interpolation | Curve fitting |
|---|---|
| Passes exactly through every point | Approximates the overall trend |
| f(x_j)=y_j | May not pass through the points |
Interpolation vs Extrapolation#
| Interpolation | Extrapolation |
|---|---|
| Evaluate inside the data range | Evaluate outside the data range |
| Usually safer | Requires knowledge of the underlying system |
Types of interpolation#
| Continuous | Piecewise |
|---|---|
| One function for the whole dataset | Different functions for parts of the domain |
Linear Interpolation#
Fit a straight line between two points:
Equivalent two-point form:
Specific heat of air
Given:
| T\ (^\circ\mathrm C) | C_p\ (\mathrm{J/(kg\cdot K)}) |
|---|---|
| 0 | 1003 |
| 50 | 1006 |
At T=25^\circ\mathrm C:
Piecewise Linear Interpolation#
Apply linear interpolation between each adjacent pair of points.
from scipy.interpolate import interp1d
Problems:
- Poor approximation of smoothly varying functions
- Derivatives are discontinuous at each data point
Higher-order Polynomial Interpolation#
For n data points, use a polynomial of degree n-1:
Each point produces one equation:
Matrix form#
Solve for the coefficients c_i.
Quadratic interpolation
Given:
| T\ (^\circ\mathrm C) | C_p\ (\mathrm{J/(kg\cdot K)}) |
|---|---|
| 0 | 1003 |
| 50 | 1006 |
| 100 | 1010 |
Assume:
Solving gives:
Therefore:
Applications#
- Tabulated experimental data
- CAD and computer graphics
- Numerical differentiation
- Numerical integration
- Signal processing and filtering
Basis Functions#
A function may be represented as a sum of basis functions:
Lagrange polynomials are one family of basis functions.
Lagrange Basis Functions#
The jth Lagrange basis function satisfies:
It has roots at every x_i except x_j.
The interpolating polynomial is:
- Avoids directly solving for polynomial coefficients
- Produces the same polynomial as the matrix approach
Interpolate C_p at 75^\circ\mathrm C
Given:
| T\ (^\circ\mathrm C) | C_p\ (\mathrm{J/(kg\cdot K)}) |
|---|---|
| 0 | 1003 |
| 50 | 1006 |
| 100 | 1010 |
| 150 | 1016 |
At T=75^\circ\mathrm C:
High-order Polynomial Problems#
Using many points in one polynomial can cause:
| Problem | Effect |
|---|---|
| Overfitting | Follows noise in the data |
| Round-off error | Small coefficient errors become large |
| Ill-conditioned matrix | Coefficients become difficult to solve accurately |
Possible solutions:
- Use better basis functions
- Choose better interpolation points
- Interpolate subsets of the data, such as with splines
Choice of Points#
The number and spacing of interpolation points affect accuracy.
For numerical or analytical functions, the point locations can be selected.
Runge Function#
Runge's Phenomenon#
Oscillation near the ends of a polynomial interpolant.
It becomes worse with:
- Higher-degree polynomials
- Equally spaced points
Chebyshev Nodes#
Chebyshev nodes are:
- Roots of first-kind Chebyshev polynomials
- Projections of equally spaced points on a circle
- Chosen to reduce interpolation error
For n nodes between x_{\min} and x_{\max}:
Four Chebyshev nodes on [-5,5]
Given:
The nodes are approximately:
Good Programming Practice
Three main principles:
- Write readable code
- Modularise using functions and modules
- Start small and test as you go
Readable code#
- Use descriptive names
- Keep each line to one understandable operation
- Avoid overly complicated nested expressions
- Use comments to explain difficult reasoning
def lagrange_basis_function(x, index, points):
...
Modularise#
- Put repeated code into functions
- Put related functions into separate files
- Import reusable functions where needed
from polyfit import fit_norder_polynomial
Key ideas#
- Interpolation passes exactly through the data points
- Linear interpolation is simple but has discontinuous derivatives
- n points determine a polynomial of degree at most n-1
- Lagrange functions provide a direct interpolation formula
- High-degree interpolation can suffer from Runge's phenomenon
- Chebyshev nodes reduce endpoint oscillation and interpolation error
Comments
Powered by GitHub issues