【发布时间】:2021-11-02 14:47:31
【问题描述】:
我正在编写一个程序,该程序可以绘制一个投掷物体的抛射运动图形。在嵌套循环中,我插入了一行以将计算值保存在一个 numpy 数组中,但每当我打印该数组时,只会显示初始值。
import numpy as np
v_0 = np.empty(0, dtype = float) #in m/s
theta_0 = np.empty(0, dtype = float)
t = np.arange(0, 50, .1) #time
G = -9.81 #gravity
r = 0
X = 0
Y = 0
x_val = np.array([0.0])
y_val = np.array([0.0])
for i in range(1): #will use this later for generating random values
theta_0 = np.array([15, 20, 22, 25]) * (np.pi/180)
v_0 = np.array([20, 24, 28, 32])
print("These are the angles in degrees:", theta_0 / (np.pi/180))
print("These are the initial velocities in m/s:", v_0)
for i in range(0, len(t)):
for r in range(len(theta_0)):
#if any(Y > 0):
X = v_0[r] * np.cos(theta_0[r]) * t[i]
X = np.round_(X,4)
Y = v_0[r] * np.sin(theta_0[r]) * t[i] + 0.5 * G * (t[i])**2
Y = np.round_(Y,4)
np.append(x_val, X)
np.append(y_val, Y)
print(x_val)
print(y_val)
【问题讨论】: