修正:谢谢你,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 --#