这个python程序应该模拟一个物体从50米高的建筑物上扔下,具有一定的初始速度和恒定的重力加速度。 我使用数组来存储不同的组件,但是当需要进行计算时,我的结果矩阵并不是应该的那样。事实上,得到的矩阵在很大程度上仍然是空的。是什么导致了这个问题?在
x = z = vz = vy = ax = ay = time = 0.0
y = 50 #Initial Height: 50 meters
vx = 25 #Initial velocity in the x direction: 25 m/s
az = -9.8 #Constant acceleration in the z direction: -9.8 m/s^2
deltaTime = .000001
#Initializes a matrix with 3 columns and 1000 rows for each column: Will hold the corresponding x,y,z coordinate of the particle at time t
positionMatrix = [[None]*1000 for x in range(3)]
posArray = [x, y, z]
velArray = [vx, vy, vz]
accArray = [ax, ay, az]
timeArray = [i*deltaTime for i in range(1000)]
j = 1 #time increment
for j in range (1,500): #j is the time increment
for i in range (1,3): #i is each component (x, y, z)
#x = x + vx*time + .5*ax*(time*time); #y = y + vy*time + .5*ay*(time*time); #z = z + vz*time + .5*az*(time*time)
positionMatrix[i][j] = posArray[i] + velArray[i] * timeArray[j] + 1/2*accArray[i] * timeArray[j] * timeArray[j]
print(positionMatrix)
我不知道这是否是代码中唯一的问题,甚至是最重要的问题,但您的范围从1开始。这意味着您永远不会遍历数组的第一个元素,索引0。在
我不确定你有一个有效的问题?你怎么判断失败?是因为你每次都在打印位置矩阵吗?在
它看起来好像什么都没有,因为你要打印出3k的每个迭代。更改代码行:
到
^{pr2}$我做了一个
结果是
所以你可以看到每一行都被设置了一些内容。至少你正在处理的那些,从0开始你的范围(不要指定1)。在
^{4}$您的范围是错误的-posArray的索引范围是从0到2(因此posArray[0]=x,posArray[1]=y,posArray[2]=z)。而且,你每次都会打印出矩阵,所以你会看到很多没有。在
也可以在数组中放入1000行,但只填充500行。在
应将最后一段代码替换为:
相关问题 更多 >
编程相关推荐