Python Forum

Full Version: The size of the array returned by func (1) does not match
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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)