Nonlinear Least Squares Curve Fitting with SciPy
Nonlinear least squares curve fitting is a process where we fit a nonlinear model to a set of data points by minimizing the sum of the squares of the residuals (the differences between observed and model-predicted values).
$SciPy$ provides the curve_fit function in scipy.optimize to perform this fitting.
Example Problem: Fitting an Exponential Decay Model
Problem Statement:
We have some experimental data that is expected to follow an exponential decay function of the form:
$$
y = a \cdot e^{-b \cdot x} + c
$$
Where:
- $( a )$, $( b )$, and $( c )$ are the parameters to be estimated.
- $( x )$ is the independent variable (e.g., time).
- $( y )$ is the observed dependent variable (e.g., concentration, temperature, etc.).
Given a set of noisy data points generated by the above equation, we will use nonlinear least squares fitting to estimate the values of $( a )$, $( b )$, and $( c )$.
Steps to Solve:
- Generate Noisy Data: Create synthetic data based on the exponential decay function and add some noise to simulate real-world experimental data.
- Define the Model Function: Create a $Python$ function for the exponential decay model.
- Use
curve_fitto Estimate Parameters: Fit the model to the data using $SciPy$’scurve_fit. - Plot the Results: Compare the original noisy data with the fitted curve.
Implementation in Python:
1 | import numpy as np |
Explanation of the Code:
Synthetic Data Generation:
- We generate the independent variable $( x )$ as $50$ equally spaced points between $0$ and $4$.
- We generate noisy data for $( y )$ using the exponential decay function with true parameters $( a = 2.5 )$, $( b = 1.3 )$, and $( c = 0.5 )$.
We add random $Gaussian$ $noise$ with a standard deviation of $0.2$ to simulate real-world measurement noise.
Model Function:
- We define the model function
model_func(x, a, b, c), which represents the exponential decay function we are trying to fit to the data.
- We define the model function
Curve Fitting:
- The
curve_fitfunction takes the model function, the independent variable $( x )$, and the noisy observed data $( y )$, and attempts to find the parameters $( a )$, $( b )$, and $( c )$ that best fit the data. - We provide an initial guess for the parameters.
The algorithm iterates to minimize the sum of squared residuals and returns the best-fit parameters.
- The
Fitted Curve:
- We use the estimated parameters to compute the fitted curve and compare it against the noisy data.
Plotting:
- We plot the noisy data points and the fitted curve on the same graph to visualize how well the model fits the data.
Output:

Plot:
- The plot will show the noisy data points (in blue) and the fitted exponential decay curve (in red).
The fitted curve should align well with the noisy data, demonstrating that the curve-fitting process has accurately captured the underlying trend despite the noise.
Interpretation:
- The fitted parameters $( a )$, $( b )$, and $( c )$ are very close to the true values used to generate the data:
- True values: $( a = 2.5 )$, $( b = 1.3 )$, $( c = 0.5 )$
- Fitted values: $( a = 2.807 )$, $( b = 1.246 )$, $( c = 0.445 )$
- The slight differences between the true and fitted values are due to the noise added to the data, which is common in real-world experiments.
Key Concepts:
- Nonlinear Least Squares: A method for fitting a nonlinear model to data by minimizing the sum of the squared differences between the observed and predicted values.
curve_fitFunction: This function in $SciPy$ is designed to perform nonlinear least squares fitting.
It is a versatile tool that can handle a wide range of models.- Covariance Matrix:
curve_fitalso returns a covariance matrix, which can be used to estimate the uncertainty in the fitted parameters.
This is particularly useful in statistical modeling when you need to quantify the confidence in the parameter estimates.
This example demonstrates how to use nonlinear least squares curve fitting to fit an exponential decay model to noisy data.



