Skip to main content
 首页 » 编程设计

python之numpy 数组中的值表现异常

2024年10月01日6exmyth

已修复:谢谢小伙伴们,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 值和时间是正确的,它们只是突然搞砸了。

请您参考如下方法:

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