2012-12-01 280 views
0

我想遍歷一系列c值?我怎麼做?我試圖有一個for循環或在外面while循環:Python雙循環? while和for循環?

注:我想4℃之間,以15 120值

for c in range(4,15): 
    i=0 
    while i< nt-1: #if i<nt-1 is true, the body below will be excuted 
     #Update the acceleration using the following equation 
     x[i+1] = (-y[i]-z[i])*deltat+x[i] 
     #Update the velocity using the follwing equation 
     y[i+1] =(x[i]+a*y[i])*deltat+y[i] 
     #Update the displacement, using follwing equation 
     z[i+1] = (b+z[i]*(x[i]-c))*deltat+z[i] 
     #go to next time step 
     i=i+1 

OR

c=arange(4,15,1) 
for p in c: 
    while i< nt-1: #if i<nt-1 is true, the body below will be excuted 
     #Update the acceleration using the following equation 
     x[i+1] = (-y[i]-z[i])*deltat+x[i] 
     #Update the velocity using the follwing equation 
     y[i+1] =(x[i]+a*y[i])*deltat+y[i] 
     #Update the displacement, using follwing equation 
     z[i+1] = (b+z[i]*(x[i]-p))*deltat+z[i] 
     #go to next time step 
     i=i+1 
+2

哪些變量'X,Y,NT,'和'deltaat' – enginefree

+0

內環只是因爲它''超過後i'不會被重置爲零運行一次NT-1'第一次。但是無論如何,''''''''''''''''''''''和'z'都會被覆蓋,所以我不確定你會得到你想要的。來自不同'c'值的結果應該如何結合? – Blckknght

回答

2

既然你已經使用NumPy,以下可能是最自然的:

for c in np.linspace(4, 15, 120): 
    ... 

linspace()調用會產生120個值:

In [33]: np.linspace(4, 15, 120) 
Out[33]: 
array([ 4.  , 4.09243697, 4.18487395, 4.27731092, 
     4.3697479 , 4.46218487, 4.55462185, 4.64705882, 
     4.7394958 , 4.83193277, 4.92436975, 5.01680672, 
     ... 
     14.35294118, 14.44537815, 14.53781513, 14.6302521 , 
     14.72268908, 14.81512605, 14.90756303, 15.  ]) 
+0

謝謝!但它是while循環沒有運行..我無法從這個雙循環中得到任何東西 – user1869032