Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Array by func (1) does not match size in Python

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)
like image 761
Usman YousafZai Avatar asked Jun 21 '26 19:06

Usman YousafZai


1 Answers

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).

like image 111
David Avatar answered Jun 24 '26 09:06

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!