Challenge - 5 Problems
Interpolation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of linear interpolation with scipy
What is the output of this code snippet that uses linear interpolation to estimate values?
SciPy
import numpy as np from scipy.interpolate import interp1d x = np.array([0, 1, 2, 3]) y = np.array([0, 1, 4, 9]) linear_interp = interp1d(x, y, kind='linear') result = linear_interp(1.5) print(result)
Attempts:
2 left
💡 Hint
Linear interpolation finds the value on the straight line between two known points.
✗ Incorrect
The value at 1.5 is halfway between y=1 at x=1 and y=4 at x=2, so linear interpolation gives (1 + 4)/2 = 2.5.
❓ Predict Output
intermediate2:00remaining
Output of cubic interpolation with scipy
What value does this cubic interpolation code produce at x=1.5?
SciPy
import numpy as np from scipy.interpolate import interp1d x = np.array([0, 1, 2, 3]) y = np.array([0, 1, 4, 9]) cubic_interp = interp1d(x, y, kind='cubic') result = cubic_interp(1.5) print(round(result, 2))
Attempts:
2 left
💡 Hint
Cubic interpolation fits a smooth curve through the points, not just straight lines.
✗ Incorrect
Cubic interpolation estimates a smoother curve, giving approximately 3.375 at 1.5, rounded to 3.38.
❓ data_output
advanced2:00remaining
Number of points generated by linear vs cubic interpolation
Given 5 data points, how many points will the following code generate when interpolating 10 new points between the minimum and maximum x values?
SciPy
import numpy as np from scipy.interpolate import interp1d x = np.linspace(0, 4, 5) y = x**2 linear_interp = interp1d(x, y, kind='linear') cubic_interp = interp1d(x, y, kind='cubic') new_x = np.linspace(0, 4, 10) linear_y = linear_interp(new_x) cubic_y = cubic_interp(new_x) print(len(linear_y), len(cubic_y))
Attempts:
2 left
💡 Hint
The interpolation returns values for each input in new_x.
✗ Incorrect
Both linear and cubic interpolation return one value for each new x input, so both arrays have length 10.
❓ visualization
advanced3:00remaining
Visual difference between linear and cubic interpolation
Which plot correctly shows the difference between linear and cubic interpolation for the points (0,0), (1,1), (2,4), (3,9)?
SciPy
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import interp1d x = np.array([0, 1, 2, 3]) y = np.array([0, 1, 4, 9]) new_x = np.linspace(0, 3, 100) linear_interp = interp1d(x, y, kind='linear') cubic_interp = interp1d(x, y, kind='cubic') plt.plot(x, y, 'o', label='Data points') plt.plot(new_x, linear_interp(new_x), label='Linear') plt.plot(new_x, cubic_interp(new_x), label='Cubic') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Think about how linear interpolation connects points versus cubic interpolation.
✗ Incorrect
Linear interpolation connects points with straight lines, while cubic interpolation fits a smooth curve through all points.
🧠 Conceptual
expert2:30remaining
Why cubic interpolation can fail with few points
Why might cubic interpolation raise an error or produce unreliable results when given only two data points?
Attempts:
2 left
💡 Hint
Think about the minimum number of points needed to define a cubic curve.
✗ Incorrect
A cubic polynomial has four coefficients, so at least four points are needed to define it uniquely.