python - numpy 数组中的值表现奇怪

标签 python numpy

已修复:谢谢各位,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 --#

enter image description here

关于python - numpy 数组中的值表现奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47519841/

相关文章:

object - 有效地内省(introspection) Python 对象 IP 地址实例所有权

python - 我如何访问这个复杂的 JSON 中的嵌套数据,其中包含另一个 JSON 文档作为字符串之一?

python - 使用基于numpy数组中位置的numpy算术

python - 为什么python的SharedMemory似乎将数组初始化为零

python - Classifier.fit for oneclassSVM 提示 float 类型。 TypeError float 是必需的

python - Wordpress xmlrpc SSL 证书错误(仅在 1 台机器上)

python - 将矩阵分割成更小的 block 并根据其值执行某些操作

r - 哪些数值优化器可以只使用梯度,而没有明确的目标值?

python - numpy 数组的形状

python - 导入错误 : No module named django_extensions