numpy数组中的值表现奇怪

时间:2017-11-27 21:10:12

标签: python numpy

固定:谢谢你,y(t)不是y(x) 我的代码应该模拟一个完美物体的轨迹,而不是只有重力的拖曳。 pastebin: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值和时间是正确的,他们只是突然完成了。

1 个答案:

答案 0 :(得分:1)

这是时间的函数,而不是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 --#

enter image description here