代码之家  ›  专栏  ›  技术社区  ›  MrD

Numpy多项式生成

  •  0
  • MrD  · 技术社区  · 6 年前

    我正在使用多项式在域0到0.02上生成一个二次多项式,使其适合点(0,0)和(0.02,16)

    结果多项式对象如下:

    enter image description here

    所以用系数 [4. 8. 4.]

    如果我跑了 plt.plot(*pA.linspace(), label="Ascending", color="orange") ,结果如预期:

    enter image description here

    但是,如果我在另一个工具上绘制这个多项式,例如WolframAlpha,结果会完全不同:

    http://www.wolframalpha.com/input/?i=4x%5E2%2B8x%2B4+;+0+%3C%3D+x+%3C+0.02

    enter image description here

    有人能帮忙吗?相当困惑

    编辑,根据要求,多项式对象可以通过以下方式获得:

    from numpy.polynomial import Polynomial
    import matplotlib.pyplot as plt
    
    pA = Polynomial(coef=[4.,8.,4.], domain=[0,0.02])
    plt.plot(*pA.linspace(), label="Ascending", color="orange")
    plt.show()
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   user2357112    6 年前

    A numpy.polynomial.Polynomial coef 数组 array([4., 8., 4.]) 不一定代表多项式 4 + 8x + 4x^2 Polynomial 对象的 domain window 一是测绘 domain[0] window[0] domain[1] window[1] 窗口 存在,但他们确实存在。

    如果让Wolfram Alpha从-1到1绘制4+8x+4x^2,而不是从0到0.02,则会看到一个形状更像matplotlib输出的图形:

    enter image description here

        2
  •  1
  •   Charles Harris    6 年前

    In [1]: from numpy.polynomial import Polynomial as P
    
    In [2]: p = P([4.,8.,4.], domain=[0,0.02])
    
    In [3]: p.convert()
    Out[3]: Polynomial([     0.,      0.,  40000.], [-1.,  1.], [-1.,  1.])
    
        3
  •  0
  •   denis    5 年前

    p = P( [0, 0, 1], domain=[0, 1], window=[-1, 1] )
    

    p(x) 分两步进行:

    1: domaintowindow: x -> w = 2x - 1  # 0 to -1, 1 to 1
    2: [0 0 1] is squaring, w -> w^2
    
    p( x ): x -> w = 2x - 1
              -> w^2  # [0 0 1]
              = (2x - 1)^2  = 4x^2 - 4x + 1  # p.convert()
    

    p() 在另一个系统中,这两个步骤都需要-- 别忘了 domaintowindow() .


    w^2