0

我想循环一系列 c 值?我怎么做?我尝试在外部使用 for 循环或 while 循环:

注意:我希望 c 介于 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

或者

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
4

1 回答 1

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.        ])
于 2012-12-01T17:31:59.270 回答