Splines were formulated to relieve oscillations attributed to Runge’s phenomenon by piecing together a series of lower-order polynomials and requiring smoothness. Consider a polynomial over the interval between and , and assert:
be continuous
be continuous
With these 4 constraints, it is clear we are looking for cubic functions, and therefore these splines are piecewise cubic curves.
We will be describing the splines in terms of the knots, which parameterize the curves. For these splines, these knots are the second derivatives at a point .
To find the coefficients of the cubic splines, consider that the second derivative is linear and represent it with a 2-point Lagrange interpolation:
Using the constraints above we end up with:
which is a tridiagonal matrix!
import numpy as np
import plotly.graph_objects as go
from scipy.interpolate import CubicSpline
try:
import executable_engineering as exe
except ImportError:
%pip install -q executable_engineering
import executable_engineering as exeVisualizing Cubic Splines¶
We can compute and visualize a cubic spline interpolation of the function using scipy.interpolate.CubicSpline.
def f(x):
return np.exp(-(x/2)**2)
x_toy = np.linspace(-6, 6, 100)
y_toy = f(x_toy)
x_d = np.arange(-5, 6, 1)
y_d = f(x_d)
cs = CubicSpline(x_d, y_d)
x_interp = np.linspace(-6, 6, 200)
y_interp = cs(x_interp)
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_toy, y=y_toy, mode='lines', name='exp(-(x/2)^2)'))
fig.add_trace(go.Scatter(x=x_d, y=y_d, mode='markers', name='Sampled points', marker=dict(color='red', size=8)))
fig.add_trace(go.Scatter(x=x_interp, y=y_interp, mode='lines', name='Cubic Spline'))
fig.update_layout(title='Function, Sampled Points, and Cubic Spline Interpolation', xaxis_title='x', yaxis_title='y')
fig.show()Analysis of cubic splines¶
We note:
Cubic splines are stiffer in that they don’t have high-frequency oscillations (thus avoiding Runge’s phenomenon).
The concept of smoothness is easy in 1D, but what does it mean for 2D+? How would you ensure continuity along an edge?
Specifying smoothness as part of the goals going in suggest this is more of a global scheme. This requires simultaneous linears systems to be solved.