首页 > 解决方案 > 定义一个函数以定位给定多项式的零点

问题描述

完成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

到目前为止,我实现的代码运行良好。问题是我被困在下一步要解决的问题上。我可以尝试找出一种可以计算第三、第四等多项式根的方法,但这对我没有帮助,因为我需要能够为给定的任何 n 阶多项式生成代码。

标签: pythonmathpolynomial-mathpolynomials

解决方案


似乎您不知道评估多项式根背后的基本数学。给你一个概述:

  • 第一顺序:简单,您只需给出解决方案
  • 二阶:简单,有一个公式
  • 三阶:仍然可行,但越来越棘手(需要多项式除法!)
  • 四阶及以上:不再微不足道

但你很幸运,有一个numpy功能roots()完全符合你的需求。


推荐阅读