本文介绍了如何在Python中执行Bisection方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个Python程序,该程序将运行二分法来确定其根:

I want to make a Python program that will run a bisection method to determine the root of:

f(x) = -26 + 85x - 91x2 +44x3 -8x4 + x5

对分法是一种用于估计多项式f(x)的根。

The Bisection method is a numerical method for estimating the roots of a polynomial f(x).

有没有可用的伪代码,算法或库可以用来告诉我答案?

Are there any available pseudocode, algorithms or libraries I could use to tell me the answer?

推荐答案

以下代码显示了基本技术:

Here's some code showing the basic technique:

>>> def samesign(a, b):
        return a * b > 0

>>> def bisect(func, low, high):
    'Find root of continuous function where f(low) and f(high) have opposite signs'

    assert not samesign(func(low), func(high))

    for i in range(54):
        midpoint = (low + high) / 2.0
        if samesign(func(low), func(midpoint)):
            low = midpoint
        else:
            high = midpoint

    return midpoint

>>> def f(x):
        return -26 + 85*x - 91*x**2 +44*x**3 -8*x**4 + x**5

>>> x = bisect(f, 0, 1)
>>> print x, f(x)
0.557025516287 3.74700270811e-16

这篇关于如何在Python中执行Bisection方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 04:52