fitting multivariate curve_fit in python
N and M are defined in the help for the function. N is the number of data points and M is the number of parameters. Your error therefore basically means you need at least as many data points as you have parameters, which makes perfect sense.
This code works for me:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def fitFunc(x, a, b, c, d):
return a + b*x[0] + c*x[1] + d*x[0]*x[1]
x_3d = np.array([[1,2,3,4,6],[4,5,6,7,8]])
p0 = [5.11, 3.9, 5.3, 2]
fitParams, fitCovariances = curve_fit(fitFunc, x_3d, x_3d[1,:], p0)
print ' fit coefficients:\n', fitParams
I have included more data. I have also changed fitFunc
to be written in a form that scans as only being a function of a single x - the fitter will handle calling this for all the data points. The code as you posted also referenced x_3d[2,:]
, which was causing an error.