Using scipy.optimize.curve_fit for Parameter Estimation
Overview
scipy.optimize.curve_fit is a tool for fitting models to experimental data using nonlinear least squares optimization.
Basic Usage
from scipy.optimize import curve_fit
import numpy as np
# Define your model function
def model(x, param1, param2):
return param1 * (1 - np.exp(-x / param2))
# Fit to data
popt, pcov = curve_fit(model, x_data, y_data)
# popt contains the optimal parameters [param1, param2]
# pcov contains the covariance matrix
Fitting a First-Order Step Response
import numpy as np
from scipy.optimize import curve_fit
# Known values from experiment
y_initial = ... # Initial output value
u = ... # Input magnitude during step test
# Define the step response model
def step_response(t, K, tau):
"""First-order step response with fixed initial value and input."""
return y_initial + K * u * (1 - np.exp(-t / tau))
# Your experimental data
t_data = np.array([...]) # Time points
y_data = np.array([...]) # Output readings
# Perform the fit
popt, pcov = curve_fit(
step_response,
t_data,
y_data,
p0=[K_guess, tau_guess], # Initial guesses
bounds=([K_min, tau_min], [K_max, tau_max]) # Parameter bounds
)
K_estimated, tau_estimated = popt
Setting Initial Guesses (p0)
Good initial guesses speed up convergence:
# Estimate K from steady-state data
K_guess = (y_data[-1] - y_initial) / u
# Estimate tau from 63.2% rise time
y_63 = y_initial + 0.632 * (y_data[-1] - y_initial)
idx_63 = np.argmin(np.abs(y_data - y_63))
tau_guess = t_data[idx_63]
p0 = [K_guess, tau_guess]
Setting Parameter Bounds
Bounds prevent physically impossible solutions:
tau_min = 1e-6 # small positive floor: tau = 0 makes exp(-t / tau) divide by zero
bounds = (
[lower_K, tau_min], # Lower bounds
[upper_K, upper_tau] # Upper bounds
)
# curve_fit raises ValueError if p0 lies outside the bounds; clip it in
p0 = np.clip(p0, bounds[0], bounds[1])
Parameter Uncertainty
pcov is the estimated covariance matrix of popt; the square roots of its diagonal are the 1-sigma parameter errors.
popt, pcov = curve_fit(model, x_data, y_data, p0=p0, sigma=y_err, absolute_sigma=True)
perr = np.sqrt(np.diag(pcov)) # 1-sigma errors, same order as popt
corr = pcov / np.outer(perr, perr) # parameter correlation matrix
if not np.all(np.isfinite(pcov)):
raise RuntimeError("covariance not estimated; parameters are not identifiable from these data")
sigma: per-point standard deviations ofy_data(residuals are divided bysigma, so points are weighted by1/sigma**2), or a 2-D covariance matrix ofy_data.absolute_sigma=True:sigmaare true measurement errors andpcovis used as computed.absolute_sigma=False(default): only relativesigmamatter;pcovis rescaled by the reduced chi-squaresum(r**2) / (N - n_params), so errors follow the observed scatter.pcovfilled withinf(withOptimizeWarning: Covariance of the parameters could not be estimated) means a rank-deficient Jacobian orN <= n_params. With bounds, curve_fit usestrf, which returns a pseudoinverse instead ofinf, so also checknp.linalg.cond(pcov)and correlations near +/-1.
Calculating Fit Quality
R-squared (Coefficient of Determination)
# Predicted values from fitted model
y_predicted = step_response(t_data, K_estimated, tau_estimated)
# Calculate R-squared
ss_residuals = np.sum((y_data - y_predicted) ** 2)
ss_total = np.sum((y_data - np.mean(y_data)) ** 2)
r_squared = 1 - (ss_residuals / ss_total)
For nonlinear models R² is descriptive only: it can be negative when the fit is worse than the mean, it does not equal the squared correlation, and it is a poor basis for comparing models. Use residual plots, RMSE, reduced chi-square (with known sigma), or AIC/BIC for that.
Root Mean Square Error (RMSE)
residuals = y_data - y_predicted
rmse = np.sqrt(np.mean(residuals ** 2))
Complete Example
import numpy as np
from scipy.optimize import curve_fit
def fit_first_order(t, y, y0, u, y_err=None):
"""Fit y(t) = y0 + K*u*(1 - exp(-(t - t[0]) / tau)) to step-response arrays.
Returns (popt, perr, r_squared, rmse) with popt = [K, tau].
"""
t = np.asarray(t, dtype=np.float64)
y = np.asarray(y, dtype=np.float64)
t_rel = t - t[0] # step applied at the first sample
def model(t_rel, K, tau):
return y0 + K * u * (1 - np.exp(-t_rel / tau))
lower, upper = [-np.inf, 1e-6], [np.inf, np.inf]
K_guess = (y[-1] - y0) / u
idx_63 = np.argmin(np.abs(y - (y0 + 0.632 * (y[-1] - y0))))
p0 = np.clip([K_guess, t_rel[idx_63]], lower, upper)
popt, pcov = curve_fit(model, t_rel, y, p0=p0, bounds=(lower, upper),
sigma=y_err, absolute_sigma=y_err is not None)
perr = np.sqrt(np.diag(pcov))
residuals = y - model(t_rel, *popt)
r_squared = 1 - np.sum(residuals ** 2) / np.sum((y - y.mean()) ** 2)
rmse = np.sqrt(np.mean(residuals ** 2))
return popt, perr, r_squared, rmse
Common Issues
RuntimeError: Optimal parameters not found
- Try better initial guesses
- Check that data is valid (no NaN, reasonable range)
Poor fit (low R^2):
- Data might not be from step response phase
- System might not be first-order
- Too much noise in measurements
Unrealistic parameters:
- Add bounds to constrain solution
- Check units are consistent