Introduction to Interpolation¶
Interpolation is a fundamental numerical method for fitting continuous curves to discrete data points. Historically, it was easier to perform locally or iteratively as new information was obtained.
When choosing an interpolation method, we must consider:
Build Speed: How fast can we construct the model?
Update Speed: How quickly can we add new data?
Execution Speed: How fast can we evaluate interpolated values?
Generalizability: Does it scale to N-dimensions?
Example: Interpolating a Gaussian Curve¶
For illustrative purposes, let’s design a toy problem for exploration. We will sample a known Gaussian function, and then attempt to recover it using various interpolation techniques.
Our goal is to use the sampled data (the red points) and recover the ‘true’ underlying function (the blue curve) as faithfully as possible.
import numpy as np
import plotly.graph_objects as go
# Define the function
def f(x):
return np.exp(-(x/2)**2)
# Create x values for plotting
x_toy = np.linspace(-6, 6, 100)
y_toy = f(x_toy)
# Sample 11 times at 1-interval intervals
x_d = np.arange(-5, 6, 1)
y_d = f(x_d)
# Plot the function and sampled points
fig = go.Figure()
fig.add_trace(go.Scatter(x=x_toy, y=y_toy, 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')))
fig.update_layout(title='Function and Sampled Points', xaxis_title='x', yaxis_title='y')
fig.show()
print("x_d:", x_d)
print("y_d:", y_d)