Using scipy curve_fit to fit a function with multiple independent variables

In this blog post, I will show you how to use scipy curve_fit to fit a function with multiple independent variables. Curve fitting is a technique to find the best parameters for a model function that describes the relationship between a dependent variable and one or more independent variables. Scipy curve_fit is a function in the scipy.optimize module that performs non-linear least squares curve fitting.

The general syntax of curve_fit is:

scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=(-inf, inf), method=None, jac=None, *, full_output=False, **kwargs)

where f is the model function that takes the independent variable(s) as the first argument and the parameters to fit as separate remaining arguments, xdata is the array-like or object containing the independent variable(s), ydata is the array-like containing the dependent data, and p0 is the optional initial guess for the parameters.

See also  Numerical Simulations with Python (ODEs, PDEs)

To fit a function with multiple independent variables, we need to pass xdata as an (k,M)-shaped array in case of functions with k predictors, where M is the number of data points. For example, if we have two independent variables x and y, we can pass xdata as (x,y), where x and y are M-length sequences. Then, our model function f should accept xdata as the first argument and unpack it to x and y for clarity. For example:

import numpy as np
from scipy.optimize import curve_fit

def func(X, a, b, c):
  x,y = X
  return np.log(a) + b*np.log(x) + c*np.log(y)

x = np.linspace(0.1,1.1,101)
y = np.linspace(1.,2., 101)
a, b, c = 10., 4., 6.
z = func((x,y), a, b, c) + np.random.random(101) / 100

popt, pcov = curve_fit(func, (x,y), z)

print(popt)

See also  Solving Differential Equations with SciPy

After fitting the model function with multiple independent variables using scipy curve_fit, you can access the optimal parameters in the ‘popt’ variable, and the covariance matrix of the parameters in the ‘pcov’ variable. These parameters represent the best-fit values that minimize the sum of squared residuals between the model predictions and the observed data. You can use these parameters for various purposes, such as making predictions, further analysis, or visualization of the fitted model. Curve fitting with multiple independent variables is a powerful technique that allows you to capture complex relationships in your data and gain insights into the underlying processes.

See also  How to Calculate the Factorial of an Array in Numpy