2017-10-20 134 views
-1
import matplotlib.pyplot as plt 
import numpy as np 
n = 5000  # number of data points to plot 
start = 0.0 # start time, s 
end = 4500.0 # ending time, s 
g = -8.87 # acceleration, m*s**-2 

t = np.linspace(start,end,n+1,dtype=np.float(64) # time, s 
y = np.zeros(n+1)     # height, m 
v = np.zeros(n+1)     # velocity, m*s**-1 
y[ 0 ] = 250000      # initial condition, m 
v[ 0 ]=0 
bounce=0.0 
for i in range(1,n+1): 
    v[ i ] = v[ i-1 ] + g*(t[ i ]-t[ i-1 ]) 
    y[ i ] = y[ i-1 ] + v[ i-1 ] * (t[ i ]-t[ i-1 ]) 

    if y[ i ] <= 0: 
     bounce+=1 
     v[ i ] = -0.9*v[ i-1 ] 
     y[ i ]=0 
plt.plot(t,y) 
plt.show() 

爲什麼我對y = np.zeros(n + 1)有無效語法? 我要計算球以1小時15分多少時間從反彈250公里高度,每一次失去速度的10%,它反彈彈跳球無效語法爲什麼是這樣的?

+1

以上 – 0TTT0

+0

在Python,只要你上一條線,顯然是正確的神祕語法錯誤的行右括號,回頭一兩行,因爲你幾乎可以肯定失蹤一個右括號,括號或其他分隔符。 –

回答

2

你錯過了在這條線的括號:

t = np.linspace(start,end,n+1,dtype=np.float(64) 

應該是:

t = np.linspace(start,end,n+1,dtype=np.float(64))