Jan-16-2018, 12:27 PM
I am new to python, so please bear with me. I am solving a model using odeint function in Python in which I am getting an error: The size of the array returned by func (1) does not match the size of y0 (2)... Maybe I am doing a mistake in returing args in odeint functions. I don't know what is the problem or maybe I am getting the error in wrong direction. Correct me if I am wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
from scipy import * from scipy.integrate import odeint from operator import itemgetter import matplotlib matplotlib.use( 'Agg' ) from matplotlib.ticker import FormatStrFormatter from pylab import * import sys ExpData = [ 1.0 , 1.1660520579009868 , 1.3688685188071037 , 1.6165891026469563 , 1.9191557810726714 ] t_range = arange( 0.0 , 20.0 , 0.1 ) initial_condi = [ 1 , 0.5 ] VarList = [ "a" , "b" ] ParaList = [ "k1" , "k2" ] k1 = 1 k2 = 2 if len (initial_condi)! = len (VarList): sys.exit( 'error' ) def odeFunc(Y,t,modelID,t1,t2): return GenModel(Y,modelID,t1,t2) def GenModel(Y,modelID,t1,t2): RetY = [ None ] if modelID = = 1 : RetY = Y[ 0 ] + Y[ 1 ] elif modelID = = 2 : RetY = t1 * Y[ 0 ] + Y[ 1 ] elif modelID = = 3 : RetY = Y[ 0 ] + t1 * Y[ 1 ] if Y[ 0 ] = = 0 or Y[ 1 ] = = 0 : if modelID = = 27 : RetY = 0 elif modelID = = 28 : RetY = 0 if Y[ 0 ] ! = 0 and Y[ 1 ] ! = 0 : if modelID = = 27 : RetY = Y[ 0 ] * Y[ 1 ] elif modelID = = 28 : RetY = t1 * Y[ 0 ] * Y[ 1 ] elif modelID = = 29 : RetY = t2 * Y[ 0 ] * Y[ 1 ] return RetY def EvalModelFitness(Stofloat,ExpData): Sum = 0.0 for i in range ( len (Stofloat)): Sum + = (Stofloat[i] - ExpData[i]) * * 2 print Sum / len (Stofloat) if initial_condi[ 0 ] = = 0 or initial_condi[ 1 ] = = 0 : NumModels = 28 else : NumModels = 39 for j in range ( 1 ,NumModels + 1 ): S = odeint(odeFunc, initial_condi,t_range,args = (j,k1,k2)) Stofloat = S[:, 0 ].astype( type ( 'float' ,( float ,),{})) EvalModelFitness(Stofloat,ExpData) |