定义函数以找到给定多项式的零点

问题描述 投票:0回答:1

完成函数FindRealZeros,该函数将多项式作为列表并以递增的顺序返回多项式根的列表,出现次数与重复次数相同

至此,我已经断言只能将最小阶数为1的多项式传递给函数。 0阶的任何函数都会立即使断言失败。接下来,输出阶次为1的多项式。

我遇到的问题是为多项式的阶数大于2定义函数的其余部分,例如x ^ 3项。


def FindRealZeros(polynomial):



    assert len(polynomial) > 1.  
# Above i have made sure the polynomial is never just a constant or empty list such as [a], or y = a and [].




    assert EnsureStandardForm(polynomial)
# Above ensures that there are never additional zeros that does not satisfy the previous assertion.     



    if len(polynomial) == 2:     
#######################################################################
#   Here i am saying that if given a linear polynomai, ax + b, then return the root #
#               (given by the equation using the members of the list)               #
#######################################################################
        first_member = polynomial[0]
        second_member = polynomial[1]

        root = [(-first_member)/second_member]
        return root

到目前为止,我已经实现的代码可以完美地工作。问题是我被困在下一步解决的问题上。我可以尝试找出一种可以计算3、4等多项式根的方式,但这对我没有帮助,因为我需要能够为给定的任何n阶多项式生成代码。

python math polynomial-math polynomials
1个回答
0
投票
似乎您似乎不知道为多项式求根的基本数学。给您一个概述:

    第一顺序:简单,您只需提供解决方案
  • 第二阶:简单,有一个formula
  • 3阶:仍然可行,但是变得棘手(需要多项式除法!)
  • 四阶及以上:不再是微不足道的
  • 但是您很幸运,有numpy函数roots()完全适合您的需求。
  • © www.soinside.com 2019 - 2024. All rights reserved.