已修复:谢谢各位,y(t) 不是 y(x) 我有代码应该模拟完美物体的轨迹,没有阻力,只有重力。 粘贴箱:https://pastebin.com/XknNBiJ9
我认为问题出在我的名为 evalPoly 的函数中,因为这是填充 y 值 numpy 数组的函数。最大值符合我的预期,但是它在数组中发生得太快,其余的都是非常大的负数
def evalPoly(coefs, xs): #Y values messed up
"""
@function: evalPoly
@params:
coefs; The coefficients, an iteratable object (List, tuple, array)
xs; The x values, a numpy array
@returns: result; a numpy array
@purpose: Evaluate at xs a polynomial with coefficients in coefs.
"""
result = 0.0
for coef in coefs[-1::-1]:
result = result * xs + coef
return result
这是调用函数,以防我的逻辑不合理:
def trajectoryNoDrag(angleDeg,veloc,x0,y0): #Y values messed up
"""
@function: trajectoryNoDrag
@params:
angleDeg (float); the angle of launch
veloc (float); the initial velocity, x and y components
x0 (float); the initial X pos
y0 (float); the initial Y pos
@returns:
times (np.array); the time intervals
xs (np.array); the x values
ys (np.array); the y values
@purpose: Simulate the trajectory of an arrow
"""
coefsX, coefsY = arrowPolys(angleDeg, veloc, x0, y0) #Store the coefs
duration = quadSolve(coefsY)[0] #flight duration
tinc = duration * NUM_PER_SEC #the incerments
times = np.linspace(0,duration,tinc) #The times
xs = np.linspace(x0, coefsX[1] * duration, tinc) #The x values
ys = evalPoly(coefsY, xs)
return times, xs, ys
变量“coefs”的构造方式是[c,b,a] 形式的二次公式的三个系数。
请帮我弄清楚为什么 y 值如此奇怪,它们工作得很好,但我不知道我做了什么来破坏这个函数。 x 值和时间是正确的,y 只是突然出错了。
最佳答案
它是时间的函数,而不是 x 的函数。 [y(t) 而不是 y(x)]。换句话说,调用 evalPoly(coefsY, times)
而不是 evalPoly(coefsY, xs)
。
def trajectoryNoDrag(angleDeg,veloc,x0,y0): #Y values messed up
"""
@function: trajectoryNoDrag
@params:
angleDeg (float); the angle of launch
veloc (float); the initial velocity, x and y components
x0 (float); the initial X pos
y0 (float); the initial Y pos
@returns:
times (np.array); the time intervals
xs (np.array); the x values
ys (np.array); the y values
@purpose: Simulate the trajectory of an arrow
"""
coefsX, coefsY = arrowPolys(angleDeg, veloc, x0, y0) #Store the coefs
duration = quadSolve(coefsY)[0] #flight duration
tinc = duration * NUM_PER_SEC #the incerments
times = np.linspace(0,duration,tinc) #The times
xs = np.linspace(x0, coefsX[1] * duration, tinc) #The x values
# your polynomial evaluation below is a function of time.
ys = evalPoly(coefsY, times)
return times, xs, ys
#-- End of File --#
关于python - numpy 数组中的值表现奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47519841/