3

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 of 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 function but I have seen one related post of odeint LINK on stake overflow and that is working fine with the returning parameters. 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)

y0 = [1,0.5]
VarList = ["a","b"]
ParaList = ["k1","k2"]
k1 = 1
k2 = 2


def func(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]
# code reduced from here

    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]
           # code reduced from here as well
    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 y0[0] == 0 or y0[1] == 0:
    NumModels = 28
else:
    NumModels = 39

for j in range(1,NumModels+1):
    S = odeint(func, y0,t_range,args=(j,k1,k2))
    Stofloat = S[:,0].astype(type('float',(float,),{}))
    EvalModelFitness(Stofloat,ExpData)
2
  • stackoverflow.com/help/mcve Commented Jan 15, 2018 at 15:54
  • @Psytho Done with editing. Reduced the code but I can't reduce the code further because No one will get the error. Commented Jan 16, 2018 at 12:06

1 Answer 1

1

First, you dont seem to be using t anywhere in your 'func' function.

Second, the length of the returned set of values must match the length of Y0. Here you are returning a single value from func (1, just like the error states), and Y0 has a length of two (just like the error states).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.