Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: avinash ramu Newsgroups: comp.lang.python Subject: Re: RuntimeError: The size of the array returned by func does not match the size of y0 Date: Thu, 5 Nov 2015 23:30:08 -0600 Lines: 170 Sender: trackavinash@gmail.com Message-ID: References: <3ef62cda-560c-4eb9-87f0-f91a3f4d653b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Nt4XXhy4KFrNoiwCUuQyagbTUPc9tcfh4Si9mMfjnkQg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'heavily': 0.04; 'modified': 0.05; 'scipy': 0.05; 'cc:addr :python-list': 0.09; "'n'": 0.09; 'func': 0.09; 'python': 0.10; 'python.': 0.11; 'subject:not': 0.11; 'def': 0.13; 'alpha': 0.15; 'subject: \n ': 0.15; 'thu,': 0.15; "(i'm": 0.16; '0.001': 0.16; '0.01': 0.16; 'abhishek': 0.16; 'equations': 0.16; 'gamma': 0.16; 'matlab': 0.16; 'numpy': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'runtimeerror': 0.16; 'skip:q 30': 0.16; 'subject:array': 0.16; 'written.': 0.16; 'wrote:': 0.16; 'sender:addr:gmail.com': 0.18; 'element': 0.18; 'switched': 0.18; 'email addr:gmail.com>': 0.18; 'versions': 0.20; 'windows': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'arrays': 0.22; 'cheers,': 0.22; 'cc:no real name:2**0': 0.22; 'programming': 0.22; 'code,': 0.23; 'seems': 0.23; 'advance.': 0.23; 'elements': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'error': 0.27; 'parameters': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'this.': 0.28; 'skip:( 20': 0.28; 'initial': 0.28; 'array': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'returned': 0.32; 'statement': 0.32; 'problem': 0.33; 'url:python': 0.33; 'message.': 0.33; 'version:': 0.33; 'url:listinfo': 0.34; 'running': 0.34; 'add': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'integrate': 0.35; 'nov': 0.35; 'returning': 0.35; 'skip:p 30': 0.35; 'instead': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'thanks': 0.37; 'list.': 0.37; 'version': 0.38; 'received:209': 0.38; '(1)': 0.38; 'skip:p 20': 0.38; 'skip:o 20': 0.38; 'hi,': 0.38; 'skip:- 20': 0.39; 'sure': 0.39; 'does': 0.39; 'subject:the': 0.39; 'url:mail': 0.40; 'some': 0.40; 'your': 0.60; 'subject:The': 0.61; 'skip:n 10': 0.62; 'skip:y 20': 0.63; 'series': 0.65; 'from:addr:yahoo.co.in': 0.66; 'here': 0.66; 'expert': 0.70; 'avi': 0.84; 's10': 0.84; 'step.': 0.91; 'thing,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=Nl3Bi3RWZJw+CxfCu9WDFoDScTaFCQSnE19KFGn4CwY=; b=lHuwNJAqzOLYpEZjOSdgBJ7bn1LdY5gyA5S7S1aGn5cubFTMojRPpSlk9QD4k9TCNP Krv470zGWEq8xgdC/pSdBbmCpf9IwzEHKnF8X/cLEB3penpo/FEgSwRnqMNLwCmp7uMD E8HPMo1/yo7XzR0jpjVU9humdQtezQ+X5bOxCxW1SOpI7ODMSRyNPjfJjpusQDc6yxI2 bah91QDSSC6VsbIOLcYmZSg/loPgevtNMhZi2HE39vBJIkWEW7CsbDLHhx7/IiKiu+Nf PTrJRNC1OFzpI5MO1WPCdEJQP/GjaO7nsC5K/bW8QoB6wNDDc8xGZVMaYEeBD878vzzs 7IXA== X-Received: by 10.107.169.18 with SMTP id s18mr13977700ioe.190.1446787828296; Thu, 05 Nov 2015 21:30:28 -0800 (PST) In-Reply-To: <3ef62cda-560c-4eb9-87f0-f91a3f4d653b@googlegroups.com> X-Google-Sender-Auth: KsVXgdRd1w36K-6jAUWJMWphh7Y X-Mailman-Approved-At: Fri, 06 Nov 2015 05:10:02 -0500 X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98337 Hi, The number of elements returned by the function f() needs to match the number of elements in the initial condition y0. The problem seems to be in this part of the code, ``` for j in range(0,3*N/2+3): return ydot[j] ``` It is returning the first element instead of the list. I modified your code to use a temporary list(ydot_new), I then add elements to this new list using the `for` statement and return the list. This seems to work fine! See below, 79 ydot_new = [] 80 for j in range(0,3*N/2+3): 81 ydot_new.extend(ydot[j]) 82 return ydot_new (I'm not an expert on ODE, so I'm not sure how to verify the correctness!) Cheers, Avi On Thu, Nov 5, 2015 at 9:54 PM, Abhishek wrote: > I have recently switched from programming heavily in MATLAB to programming > in Python. Hence I am having some issues running the Python code that I > have written. I am using IPython with Anaconda2 on Windows 7 and using > numPy and SciPy to integrate a system of ordinary differential equations. I > have generalized the system of ODEs for any number 'N' of them. > > I have two versions of code that do the same thing, and give the same > error message. One version uses 'exec' and 'eval' heavily, and the other > uses arrays heavily. Here is my code for the array version: > > ---------------------------------------------------------------------- > import numpy as np > import matplotlib.pyplot as plt > from scipy.integrate import odeint > > #Constants and parameters > N = 2 > K00 = np.logspace(0,3,101,10) > len1 = len(K00) > epsilon = 0.01 > y0 = [0]*(3*N/2+3) > u1 = 0 > u2 = 0 > u3 = 0 > Kplot = np.zeros((len1,1)) > Pplot = np.zeros((len1,1)) > S = [np.zeros((len1,1)) for kkkk in range(N/2+1)] > KS = [np.zeros((len1,1)) for kkkk in range(N/2+1)] > PS = [np.zeros((len1,1)) for kkkk in range(N/2+1)] > Splot = [np.zeros((len1,1)) for kkkk in range(N/2+1)] > KSplot = [np.zeros((len1,1)) for kkkk in range(N/2+1)] > PSplot = [np.zeros((len1,1)) for kkkk in range(N/2+1)] > > for series in range(0,len1): > K0 = K00[series] > Q = 10 > r1 = 0.0001 > r2 = 0.001 > a = 0.001 > d = 0.001 > k = 0.999 > S10 = 1e5 > P0 = 1 > tfvec = np.tile(1e10,(1,5)) > tf = tfvec[0,0] > time = np.linspace(0,tf,len1) > > #Defining dy/dt's > def f(y,t): > for alpha in range(0,(N/2+1)): > S[alpha] = y[alpha] > for beta in range((N/2)+1,N+1): > KS[beta-N/2-1] = y[beta] > for gamma in range(N+1,3*N/2+1): > PS[gamma-N] = y[gamma] > K = y[3*N/2+1] > P = y[3*N/2+2] > > # The model equations > ydot = np.zeros((3*N/2+3,1)) > B = range((N/2)+1,N+1) > G = range(N+1,3*N/2+1) > runsumPS = 0 > runsum1 = 0 > runsumKS = 0 > runsum2 = 0 > > for m in range(0,N/2): > runsumPS = runsumPS + PS[m+1] > runsum1 = runsum1 + S[m+1] > runsumKS = runsumKS + KS[m] > runsum2 = runsum2 + S[m] > ydot[B[m]] = a*K*S[m]-(d+k+r1)*KS[m] > > for i in range(0,N/2-1): > ydot[G[i]] = a*P*S[i+1]-(d+k+r1)*PS[i+1] > > for p in range(1,N/2): > ydot[p] = -S[p]*(r1+a*K+a*P)+k*KS[p-1]+ \ > d*(PS[p]+KS[p]) > > ydot[0] = Q-(r1+a*K)*S[0]+d*KS[0]+k*runsumPS > ydot[N/2] = k*KS[N/2-1]-(r2+a*P)*S[N/2]+ \ > d*PS[N/2] > ydot[G[N/2-1]] = a*P*S[N/2]-(d+k+r2)*PS[N/2] > ydot[3*N/2+1] = (d+k+r1)*runsumKS-a*K*runsum2 > ydot[3*N/2+2] = (d+k+r1)*(runsumPS-PS[N/2])- \ > a*P*runsum1+(d+k+r2)*PS[N/2] > > for j in range(0,3*N/2+3): > return ydot[j] > > # Initial conditions > y0[0] = S10 > for i in range(1,3*N/2+1): > y0[i] = 0 > y0[3*N/2+1] = K0 > y0[3*N/2+2] = P0 > > # Solve the DEs > soln = odeint(f,y0,time,mxstep = 5000) > for alpha in range(0,(N/2+1)): > S[alpha] = soln[:,alpha] > for beta in range((N/2)+1,N+1): > KS[beta-N/2-1] = soln[:,beta] > for gamma in range(N+1,3*N/2+1): > PS[gamma-N] = soln[:,gamma] > > for alpha in range(0,(N/2+1)): > Splot[alpha][series] = soln[len1-1,alpha] > for beta in range((N/2)+1,N+1): > KSplot[beta-N/2-1][series] = soln[len1-1,beta] > for gamma in range(N+1,3*N/2+1): > PSplot[gamma-N][series] = soln[len1-1,gamma] > > for alpha in range(0,(N/2+1)): > u1 = u1 + Splot[alpha] > for beta in range((N/2)+1,N+1): > u2 = u2 + KSplot[beta-N/2-1] > for gamma in range(N+1,3*N/2+1): > u3 = u3 + PSplot[gamma-N] > > K = soln[:,3*N/2+1] > P = soln[:,3*N/2+2] > Kplot[series] = soln[len1-1,3*N/2+1] > Pplot[series] = soln[len1-1,3*N/2+2] > utot = u1+u2+u3 > > #Plot > plt.plot(np.log10(K00),utot[:,0]) > plt.show() > ---------------------------------------------------------------------- > ERROR MESSAGE: > RuntimeError: The size of the array returned by func (1) does not match > the size of y0 (6). > ---------------------------------------------------------------------- > > I am facing a RuntimeError in the ODE integrator step. Please help me > resolve this. Thanks in advance. > -- > https://mail.python.org/mailman/listinfo/python-list >