I want to adjust the parameters of a model to a given set of data.
I'm trying to use scipy
's function curve_fit
in Sage, but I keep getting
TypeError: only length-1 arrays can be converted to Python scalars
Here´s my code:
from numpy import cos,exp,pif = lambda x: exp( - 1 / cos(x) )import numpy as npdef ang(time): return (time-12)*pi/12def temp(x,maxtemp): cte=(273+maxtemp)/f(0)**(1/4) if 6<x and x<18: return float(cte*f(ang(x))**(1/4)-273) else: return -273lT=list(np.linspace(15,40,1+24*2))lT=[float(num) for num in lT] #list of y dataltimes=np.linspace(0,24,6*24+1)[1:]ltimes=list(ltimes) #list of x datau0=lT[0]def u(time,maxtemp,k): #the function I want to fit to the data def integ(t): return k*exp(k*t)*temp(t,maxtemp) return exp(-k*time)*( numerical_integral(integ, 0, time)[0] + u0 )import scipy.optimize as optimizationprint optimization.curve_fit(u, ltimes, lT,[1000,0.0003])