Challenge - 5 Problems
Curve Fitting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of curve fitting with linear model
What is the output of the following code snippet that fits a linear model to data using
curve_fit?SciPy
import numpy as np from scipy.optimize import curve_fit def linear_model(x, a, b): return a * x + b xdata = np.array([0, 1, 2, 3, 4]) ydata = np.array([1, 3, 5, 7, 9]) params, covariance = curve_fit(linear_model, xdata, ydata) print(params)
Attempts:
2 left
💡 Hint
Think about the equation of a line y = a*x + b and the data points given.
✗ Incorrect
The data points follow the line y = 2*x + 1 exactly, so the fitted parameters are a=2 and b=1.
❓ data_output
intermediate1:30remaining
Number of parameters returned by curve_fit
Given the following model and data, how many parameters does
curve_fit return?SciPy
import numpy as np from scipy.optimize import curve_fit def quadratic_model(x, a, b, c): return a * x**2 + b * x + c xdata = np.linspace(0, 5, 6) ydata = np.array([1, 3, 7, 13, 21, 31]) params, covariance = curve_fit(quadratic_model, xdata, ydata) print(len(params))
Attempts:
2 left
💡 Hint
Count the number of parameters in the quadratic model function.
✗ Incorrect
The quadratic model has three parameters: a, b, and c, so curve_fit returns three values.
🔧 Debug
advanced2:00remaining
Identify the error in curve_fit usage
What error does the following code raise when run?
SciPy
import numpy as np from scipy.optimize import curve_fit def model(x, a, b): return a * np.sin(b * x) xdata = np.array([0, 1, 2, 3]) ydata = np.array([0, 0.84, 0.91, 0.14]) params, covariance = curve_fit(model, xdata, ydata, p0=[1])
Attempts:
2 left
💡 Hint
Check the length of the initial guess array compared to the number of parameters in the model.
✗ Incorrect
The model has two parameters (a and b), but p0 is given as a list with only one element, causing a ValueError.
❓ visualization
advanced2:30remaining
Plotting fitted curve with noisy data
Which option correctly plots the noisy data points and the fitted curve from
curve_fit?SciPy
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit def model(x, a, b): return a * np.exp(b * x) xdata = np.linspace(0, 4, 50) y = model(xdata, 2.5, -1.3) np.random.seed(0) noise = 0.2 * np.random.normal(size=xdata.size) ydata = y + noise params, _ = curve_fit(model, xdata, ydata) plt.scatter(xdata, ydata, label='Noisy data') plt.plot(xdata, model(xdata, *params), 'r-', label='Fitted curve') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Look for a scatter plot for data and a red line for the fitted curve.
✗ Incorrect
The code plots noisy data as scattered points and overlays the fitted curve as a red line.
🧠 Conceptual
expert2:00remaining
Understanding covariance matrix from curve_fit
What does the covariance matrix returned by
curve_fit represent?Attempts:
2 left
💡 Hint
Think about what uncertainty means in parameter estimation.
✗ Incorrect
The covariance matrix estimates how much the fitted parameters vary and how they relate to each other.