2


I'm trying to have a curve fit that takes into account multiple series of y based on same values of x and same (exponential) law. The y values among the series vary a little since they're experimental but are still close (at same x).

I tried to build two arrays: one with the x and one with the two different series of y

def f(x,a,b,c):
    return a*numpy.exp(-b*x)+c
xdata=numpy.array([data['x'],data['x']])
ydata = numpy.array([data['y1'], data['y2']])
popt, pcov=curve_fit(f,xdata,ydata)

But this error appears:

TypeError: Improper input: N=3 must not exceed M=2

Does anyone know how solve this error or a proper way to do this kind of curve fitting?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Mabri
  • 119
  • 11
  • Take a look at http://stackoverflow.com/questions/20769340/fitting-multivariate-curve-fit-in-python – user8153 May 09 '17 at 21:03

1 Answers1

5

You should concatenate the data properly instead of creating a multi-dimensional array. There is nothing in curve_fit that states that the data has to be sorted by x:

xdata = np.concatenate((data['x'], data['x']))
ydata = np.concatenate((data['y1'], data['y2']))
popt, pcov = curve_fit(f, xdata, ydata)

This assumes that the referenced elements of data are all 1D.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264