What is the output of the following code snippet that fits a custom quadratic model to data?
import numpy as np from scipy.optimize import curve_fit def model(x, a, b, c): return a * x**2 + b * x + c xdata = np.array([0, 1, 2, 3, 4]) ydata = np.array([1, 3, 7, 13, 21]) params, _ = curve_fit(model, xdata, ydata) print(np.round(params, 2))
Recall the quadratic formula: y = a*x² + b*x + c. Check the data points to guess coefficients.
The data fits y = x² + 2x + 1 exactly. The curve_fit finds parameters [1.0, 2.0, 1.0].
Given the custom exponential model y = a * exp(b * x) + c, how many parameters does scipy.optimize.curve_fit estimate?
import numpy as np from scipy.optimize import curve_fit def exp_model(x, a, b, c): return a * np.exp(b * x) + c xdata = np.linspace(0, 4, 5) ydata = exp_model(xdata, 2, 0.5, 1) + 0.1 * np.random.normal(size=xdata.size) params, _ = curve_fit(exp_model, xdata, ydata) print(len(params))
Count the number of parameters in the function definition after x.
The model has three parameters: a, b, and c. curve_fit estimates all three.
What error does the following code raise when trying to fit a linear model?
import numpy as np from scipy.optimize import curve_fit def linear_model(x, m, c): return m * x + c xdata = np.array([0, 1, 2, 3]) ydata = np.array([1, 3, 5, 7]) params, _ = curve_fit(linear_model, xdata, ydata, p0=[1])
Check the initial guess p0 length compared to model parameters.
The model has two parameters but p0 provides only one initial guess, causing a ValueError.
Which plot correctly shows the fitted sinusoidal model y = a * sin(b * x + c) to noisy data?
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit def sin_model(x, a, b, c): return a * np.sin(b * x + c) xdata = np.linspace(0, 2 * np.pi, 50) ydata = 3 * np.sin(2 * xdata + 0.5) + 0.3 * np.random.normal(size=xdata.size) params, _ = curve_fit(sin_model, xdata, ydata, p0=[2, 2, 0]) plt.scatter(xdata, ydata, label='Data') plt.plot(xdata, sin_model(xdata, *params), color='red', label='Fitted model') plt.legend() plt.show()
Think about the shape of a sine wave and how fitting works.
The fitted model is a sine curve that closely matches the noisy data points, shown as scatter.
Which statement best describes the effect of the initial parameter guess p0 in scipy.optimize.curve_fit when fitting complex custom models?
Consider how optimization algorithms work with complex landscapes.
Nonlinear fitting can get stuck in local minima if initial guesses are poor, affecting final fit quality.