0
0
SciPydata~10 mins

Linear vs cubic interpolation in SciPy - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Linear vs cubic interpolation
Start with known data points
Choose interpolation method
Linear interpolation
Calculate interpolated values
Compare smoothness and accuracy
End
We start with known points, pick linear or cubic interpolation, compute values, then compare results.
Execution Sample
SciPy
import numpy as np
from scipy.interpolate import interp1d

x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 0, 1])

f_linear = interp1d(x, y, kind='linear')
f_cubic = interp1d(x, y, kind='cubic')

x_new = np.linspace(0, 3, 7)
y_linear = f_linear(x_new)
y_cubic = f_cubic(x_new)
This code creates linear and cubic interpolation functions and computes interpolated values at new points.
Execution Table
StepActionInput x_newLinear OutputCubic Output
1Evaluate at 0.00.00.00.0
2Evaluate at 0.50.50.50.6875
3Evaluate at 1.01.01.01.0
4Evaluate at 1.51.50.50.3125
5Evaluate at 2.02.00.00.0
6Evaluate at 2.52.50.50.6875
7Evaluate at 3.03.01.01.0
8End of evaluation---
💡 All x_new points evaluated; interpolation complete.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
x_new[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]0.00.51.01.52.02.53.0All points processed
y_linear[][0.0][0.0, 0.5][0.0, 0.5, 1.0][0.0, 0.5, 1.0, 0.5][0.0, 0.5, 1.0, 0.5, 0.0][0.0, 0.5, 1.0, 0.5, 0.0, 0.5][0.0, 0.5, 1.0, 0.5, 0.0, 0.5, 1.0]Final linear interpolated values
y_cubic[][0.0][0.0, 0.6875][0.0, 0.6875, 1.0][0.0, 0.6875, 1.0, 0.3125][0.0, 0.6875, 1.0, 0.3125, 0.0][0.0, 0.6875, 1.0, 0.3125, 0.0, 0.6875][0.0, 0.6875, 1.0, 0.3125, 0.0, 0.6875, 1.0]Final cubic interpolated values
Key Moments - 2 Insights
Why are the cubic interpolation values different from linear at the midpoints?
Because cubic interpolation fits a smooth curve considering neighbors, it produces values that can curve up or down between points, unlike linear which connects points with straight lines (see execution_table rows 2,4,6).
Why does linear interpolation output exactly the input y values at known points?
Linear interpolation always matches the original data points exactly, so at x=0,1,2,3 the outputs equal y values (see execution_table rows 1,3,5,7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cubic interpolation output at x=1.5?
A0.3125
B1.0
C0.5
D0.6875
💡 Hint
Check execution_table row 4 under 'Cubic Output'
At which x_new value does linear interpolation output 0.0?
A1.5
B2.0
C0.0
D2.5
💡 Hint
Look at execution_table rows for 'Linear Output' equal to 0.0
If we add more points between known x, how would cubic interpolation outputs change?
AThey would become straight lines
BThey would stay the same
CThey would become smoother and more detailed
DThey would become less accurate
💡 Hint
Cubic interpolation fits smooth curves, so more points give finer smoothness (concept_flow)
Concept Snapshot
Linear vs Cubic Interpolation:
- Linear connects points with straight lines.
- Cubic fits smooth curves through points.
- Use scipy interp1d with kind='linear' or 'cubic'.
- Cubic is smoother but needs more computation.
- Both match original points exactly.
Full Transcript
We start with known data points and choose either linear or cubic interpolation. Linear interpolation connects points with straight lines, producing piecewise linear outputs. Cubic interpolation fits a smooth curve considering neighboring points, producing smoother outputs. We evaluate both methods at new points between known data. Linear outputs are simple averages between points, while cubic outputs curve smoothly. The execution table shows step-by-step outputs for each method at each new point. Variables x_new, y_linear, and y_cubic track inputs and outputs. Key moments clarify why cubic differs from linear at midpoints and why linear matches original points exactly. The visual quiz tests understanding of outputs at specific points and effects of adding more points. The snapshot summarizes key differences and usage.