I need to fit a function which is the solution of a differential function. This is the code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy.optimize import curve_fit
def Vrate(V,a,b,c):
return a/(1 + np.exp(-(V - c)/b))
def K_gate(o, t, a1, a2, b, c):
return Vrate(40,a1,b,c)*(1 - o) - Vrate(40,a2,b,c)*o
# Define the function to fit
def fit_function(t, a1, a2):
sol = odeint(K_gate, 0, t, args=(a1, a2, 5, 40))
return sol.flatten()
popt, pcov = curve_fit(fit_function, times, target_data, bounds=([0,0], np.inf))
I find an error though in curve_fit method which is the one of the title.
If I don’t do the .flatten() I have the same error but with f0.shape: (10000, 1) which is correct as it is the shape of the solution of odeint. The flatten(), though, seems to double the size instaed of turning it into shape (10000,). I tried also returning sol[:,0] but I have the same error. What can I do?
Please help me 🙂