Nonlinear curve fitting for physics data with proper error propagation, chi-squared analysis, residual diagnostics, confidence intervals, and model comparison (AIC/BIC). Use for any parameter extraction from experimental or simulation data.
Nonlinear curve fitting for physics data with proper error propagation, chi-squared analysis, residual diagnostics, confidence intervals, and model comparison (AIC/BIC). Use for any parameter extraction from experimental or simulation data.
Extract physical parameters from data using nonlinear least squares, chi-squared minimization, and Bayesian inference. Proper error propagation, residual analysis, confidence intervals, and model comparison included.
When to Use
Fitting a model function to experimental data
Extracting physical constants from measurements
Comparing competing models (which theory fits better?)
Propagating measurement uncertainties to derived quantities
Any parameter estimation with proper error bars
Do NOT Use When
You need MCMC / full posterior distributions (use bayesian-inference with emcee/PyMC)
You're discovering the model itself (use symbolic-regression)
Data is a time series from an ODE (use ode-solver + this skill)
Core Workflows
1. Basic Nonlinear Fit (scipy)
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
# Model functiondefexponential_decay(t, A, tau, C):
"""y = A * exp(-t/tau) + C"""return A * np.exp(-t / tau) + C
# Data with uncertainties
t_data = np.array([0.5, 1, 2, 3, 5, 7, 10, 15, 20])
y_data = np.array([9.8, 8.1, 5.5, 3.9, 2.1, 1.3, 0.7, 0.35, 0.22])
y_err = np.array([0.3, , , , , , , , ])
popt, pcov = curve_fit(exponential_decay, t_data, y_data,
sigma=y_err, absolute_sigma=,
p0=[, , ])
perr = np.sqrt(np.diag(pcov))
A, tau, C = popt
dA, dtau, dC = perr
()
()
()
residuals = (y_data - exponential_decay(t_data, *popt)) / y_err
chi2 = np.(residuals**)
ndof = (t_data) - (popt)
chi2_red = chi2 / ndof
()