Challenge - 5 Problems
Spline Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of cubic spline interpolation at given points
Given the code below using SciPy's CubicSpline, what is the output array printed?
SciPy
import numpy as np from scipy.interpolate import CubicSpline x = np.array([0, 1, 2, 3, 4]) y = np.array([0, 1, 0, 1, 0]) cs = CubicSpline(x, y) print(cs([1.5, 2.5, 3.5]))
Attempts:
2 left
💡 Hint
Think about how cubic splines smoothly connect points and evaluate at intermediate x values.
✗ Incorrect
The CubicSpline fits a smooth curve through the points. Evaluating at 1.5, 2.5, and 3.5 gives values close to 0.6875, 0.6875, and 0.3125 respectively.
❓ data_output
intermediate1:30remaining
Number of points in spline evaluation output
Using the following code, how many points are in the output array from the spline evaluation?
SciPy
import numpy as np from scipy.interpolate import CubicSpline x = np.linspace(0, 10, 6) y = np.sin(x) cs = CubicSpline(x, y) result = cs(np.linspace(0, 10, 50)) print(len(result))
Attempts:
2 left
💡 Hint
The output length matches the number of points you evaluate the spline at.
✗ Incorrect
The spline is evaluated at 50 points, so the output array length is 50.
🔧 Debug
advanced1:30remaining
Identify the error in spline interpolation code
What error does the following code raise when run?
SciPy
import numpy as np from scipy.interpolate import CubicSpline x = np.array([0, 1, 2, 3]) y = np.array([1, 2]) cs = CubicSpline(x, y)
Attempts:
2 left
💡 Hint
Check if the input arrays x and y have matching lengths.
✗ Incorrect
CubicSpline requires x and y to have the same length. Here y has length 2 but x has length 4, causing a ValueError.
🧠 Conceptual
advanced1:30remaining
Effect of boundary conditions in spline interpolation
Which boundary condition in CubicSpline ensures that the second derivative at the endpoints is zero (natural spline)?
Attempts:
2 left
💡 Hint
Natural splines have zero curvature at the ends.
✗ Incorrect
The 'natural' boundary condition sets the second derivatives at the endpoints to zero, producing a natural spline.
🚀 Application
expert2:00remaining
Choosing spline type for periodic data
You have temperature data measured hourly over 24 hours and want to interpolate smoothly assuming the temperature at hour 0 equals hour 24. Which CubicSpline boundary condition should you use?
Attempts:
2 left
💡 Hint
Periodic data means the start and end points match in value and derivatives.
✗ Incorrect
The 'periodic' boundary condition enforces that function values and derivatives match at the endpoints, ideal for cyclic data like daily temperature.