1

I am trying to fit the coefficients of a multivariate function with curve_fit. All variables are arrays of the following shape : (1000,) Manually I can fit the curves as follows. First I define my function where the variables = [dphi1,dphi2,phi1,phi2,M] and the coefficients = [c1,c2,c3,c4,c5,c6,c7,c8,c9]:

    import numpy as np 
    import matplotlib.pyplot as plt
    import tensorflow as tf
    import math
    
    # Function definition
    def ddphi1(dphi1,dphi2,phi1,phi2,M,c1,c2,c3,c4,c5,c6,c7,c8,c9):
        return (-(c1*np.sin(phi1-phi2)*np.cos(phi1-phi2)*dphi1**2)-(c2*np.sin(phi1-phi2)*dphi2**2)+(c3*np.cos(phi1-phi2)*np.sin(phi2))+(c4*np.cos(phi1-phi2)*(dphi2-dphi1))-(c5*np.sin(phi1))+c6*M-c7*dphi1)/(c8-(c9*np.cos(phi1-phi2)*np.cos(phi1-phi2))) 

my first prediction of the coefficients:

p = [0.5625, 0.375, 27.590625000000003, 0.09375, 55.18125, 62.5, 0.425, 1, 0.5625]

I calculate the values of the function iteratively. I take the length of any of the variables already have the same size:

n = len(time1)
y = np.empty(n)

for i in range(n):
    y[i] = ddphi1(dphi11[i],dphi22[i],phi11[i],phi22[i],M[i],p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8])

plt.plot(time1, ddphi11)
plt.plot(time1, y, 'r')

Predicted Vs real data

Now the idea is to calculate the coefficients automatically with curve_fit as follows: ** ddphi1 ist my Callback function and ddphi11 my data of shape (1000,) as well as the other variables

from scipy.optimize import curve_fit

g = [0.56, 0.37, 27.63, 0.094, 55.18, 62.5, 0.625, 1, 0.56]

c,cov =curve_fit(ddphi1,(dphi1,dphi2,phi1,phi2,M),ddphi11,g)
print(c)

and I receive this error

--------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-158-e8e42e7b1216> in <module>()
      1 from scipy.optimize import curve_fit
      2 
----> 3 c,cov =curve_fit(ddphi1,(dphi1,dphi2,phi1,phi2,M),ddphi11,g=[0.56, 0.37, 27.63, 0.094, 55.18, 62.5, 0.625, 1, 0.56])
      4 print(c)

1 frames
/usr/local/lib/python3.7/dist-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
    719         # non-array_like `xdata`.
    720         if check_finite:
--> 721             xdata = np.asarray_chkfinite(xdata, float)
    722         else:
    723             xdata = np.asarray(xdata, float)

/usr/local/lib/python3.7/dist-packages/numpy/lib/function_base.py in asarray_chkfinite(a, dtype, order)
    484 
    485     """
--> 486     a = asarray(a, dtype=dtype, order=order)
    487     if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all():
    488         raise ValueError(

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (5,) + inhomogeneous part.

I have seen that most of the data that goes into the curve_fir is in the form list. Maybe there is a solution when dealing with arrays? espero I hope you can help me as I am new to Python.

1 Answers1

1

I was finally able to solve it. only the arrays should have been concatenated in a global variable

X=np.column_stack([dphi11,dphi22,phi11,phi22,M])

Then describe the model in based the global variable

def model(X,c1,c2,c3,c4,c5,c6,c7,c8,c9):
  dphi1 = X[:,0]
  dphi2 = X[:,1]
  phi1 = X[:,2]
  phi2 = X[:,3]
  M = X[:,4]

  f = (-(c1*np.sin(phi1-phi2)*np.cos(phi1-phi2)*dphi1**2)-(c2*np.sin(phi1-phi2)*dphi2**2)+(c3*np.cos(phi1-phi2)*np.sin(phi2))+(c4*np.cos(phi1-phi2)*(dphi2-dphi1))-(c5*np.sin(phi1))+c6*M-c7*dphi1)/(c8-(c9*np.cos(phi1-phi2)*np.cos(phi1-phi2)))
  return f

and the magic begins

guesses = [0.56, 0.37, 27.63, 0.094, 55.18, 62.5, 0.625, 1, 0.56]

from scipy.optimize import curve_fit

popt, pcov = curve_fit(model, X, ddphi11, guesses)
print(popt)

enter image description here