二分法求方程的根(Python)

目录

1、概述

2、代码

3、结果


1、概述

二分法求方程的根(Python)_第1张图片

        求方程 f(x)=0的近似根的最简单方法为二分法,其基本思想是:首先根据方程有根定理(函数零点定理)确定方程的有根区间,然后不断的将有根区间一分为二,直到有根区间的长度在允许误差范围内,然后取有根区间的中点作为方程近似根,二分法的优点是算法简单,缺点是收敛速度慢,不能求重根。然后就用迭代法伺候。

2、代码

import numpy as np
def func(x):
    result = x - np.tan(x)
    return result
def bisection(f,a,b,tol,maxit):
    if a > b:
        a, b = b, a 
    if np.abs(f(a))<=1.0e-16:
        return a
    if np.abs(f(b))<=1.0e-16:
        return b
    if f(a)*f(b) > 0:
        print("Zeros point may not exist")
        exit(1)
    a1, b1 = a, b
    for i in range(maxit):
        bc = (a1+b1)/2
        print("it = {},[a,b] = [{},{}],f((a+b)/2) = {}".format(i,a1,b1,f(bc)))
        if np.abs(f(bc))<=1.0e-16:
            return bc
        elif (f(a1)*f(bc)<0):
            b1 = bc
        else:
            a1 = bc
    return bc 

def main():
    maxit = 100
    a, b = 0.01, 60
    tol = 1.0e-8
    x=bisection(func,a,b,tol,maxit)
    print("x= {}".format(x))



    

if __name__ == '__main__':
    main()




3、结果

it = 0,[a,b] = [0.01,60],f((a+b)/2) = 36.20670948365492
it = 1,[a,b] = [0.01,30.005],f((a+b)/2) = 15.850580622920619
it = 2,[a,b] = [0.01,15.0075],f((a+b)/2) = 4.728146144446539
it = 3,[a,b] = [0.01,7.50875],f((a+b)/2) = 3.0488086231383953
it = 4,[a,b] = [0.01,3.759375],f((a+b)/2) = 4.965180853113451
it = 5,[a,b] = [0.01,1.8846874999999998],f((a+b)/2) = -0.44321739582592645
it = 6,[a,b] = [0.9473437499999999,1.8846874999999998],f((a+b)/2) = -4.993061970154832
it = 7,[a,b] = [1.416015625,1.8846874999999998],f((a+b)/2) = 14.19370489381378
it = 8,[a,b] = [1.416015625,1.6503515625],f((a+b)/2) = -25.04101886482726
it = 9,[a,b] = [1.53318359375,1.6503515625],f((a+b)/2) = 49.26910366880788
it = 10,[a,b] = [1.53318359375,1.591767578125],f((a+b)/2) = -118.61635689888206
it = 11,[a,b] = [1.5624755859375,1.591767578125],f((a+b)/2) = 159.67140020221845
it = 12,[a,b] = [1.5624755859375,1.5771215820312499],f((a+b)/2) = -1000.692164743844
it = 13,[a,b] = [1.5697985839843749,1.5771215820312499],f((a+b)/2) = 376.9823019098605
it = 14,[a,b] = [1.5697985839843749,1.5734600830078125],f((a+b)/2) = 1202.041886371078
it = 15,[a,b] = [1.5697985839843749,1.5716293334960936],f((a+b)/2) = -12139.058342277169
it = 16,[a,b] = [1.5707139587402343,1.5716293334960936],f((a+b)/2) = 2665.9689019298944
it = 17,[a,b] = [1.5707139587402343,1.571171646118164],f((a+b)/2) = 6828.644930977909
it = 18,[a,b] = [1.5707139587402343,1.5709428024291991],f((a+b)/2) = 31199.129846181575
it = 19,[a,b] = [1.5707139587402343,1.5708283805847167],f((a+b)/2) = -39748.58767499719
it = 20,[a,b] = [1.5707711696624755,1.5708283805847167],f((a+b)/2) = 289997.1272461388
it = 21,[a,b] = [1.5707711696624755,1.570799775123596],f((a+b)/2) = -92126.95115168976
it = 22,[a,b] = [1.5707854723930357,1.570799775123596],f((a+b)/2) = -270047.07122987835
it = 23,[a,b] = [1.5707926237583159,1.570799775123596],f((a+b)/2) = -7852130.792086863
it = 24,[a,b] = [1.5707961994409558,1.570799775123596],f((a+b)/2) = 602234.3925935033
it = 25,[a,b] = [1.5707961994409558,1.570797987282276],f((a+b)/2) = 1304519.461708586
it = 26,[a,b] = [1.5707961994409558,1.570797093361616],f((a+b)/2) = 3128850.159251721
it = 27,[a,b] = [1.5707961994409558,1.570796646401286],f((a+b)/2) = 10402990.014126034
it = 28,[a,b] = [1.5707961994409558,1.5707964229211209],f((a+b)/2) = -64045667.53043196
it = 29,[a,b] = [1.5707963111810384,1.5707964229211209],f((a+b)/2) = 24840906.13909882
it = 30,[a,b] = [1.5707963111810384,1.5707963670510796],f((a+b)/2) = 81161176.10896431
it = 31,[a,b] = [1.5707963111810384,1.5707963391160589],f((a+b)/2) = -607404991.2282902
it = 32,[a,b] = [1.5707963251485486,1.5707963391160589],f((a+b)/2) = 187356891.7622508
it = 33,[a,b] = [1.5707963251485486,1.5707963321323037],f((a+b)/2) = 541849931.0295086
it = 34,[a,b] = [1.5707963251485486,1.570796328640426],f((a+b)/2) = 10041096317.569733
it = 35,[a,b] = [1.5707963251485486,1.5707963268944873],f((a+b)/2) = -1293027499.757818
it = 36,[a,b] = [1.5707963260215179,1.5707963268944873],f((a+b)/2) = -2968291555.4557796
it = 37,[a,b] = [1.5707963264580025,1.5707963268944873],f((a+b)/2) = -8428021001.515761
it = 38,[a,b] = [1.5707963266762448,1.5707963268944873],f((a+b)/2) = -104924530373.4907
it = 39,[a,b] = [1.570796326785366,1.5707963268944873],f((a+b)/2) = 22207347960.62738
it = 40,[a,b] = [1.570796326785366,1.5707963268399268],f((a+b)/2) = 56338852158.332436
it = 41,[a,b] = [1.570796326785366,1.5707963268126464],f((a+b)/2) = 243336218459.12137
it = 42,[a,b] = [1.570796326785366,1.5707963267990062],f((a+b)/2) = -368927491579.71277
it = 43,[a,b] = [1.570796326792186,1.5707963267990062],f((a+b)/2) = 1429839342339.8782
it = 44,[a,b] = [1.570796326792186,1.570796326795596],f((a+b)/2) = -994550654629.8223
it = 45,[a,b] = [1.5707963267938911,1.570796326795596],f((a+b)/2) = -6533813968064.459
it = 46,[a,b] = [1.5707963267947436,1.570796326795596],f((a+b)/2) = 3662284195633.0815
it = 47,[a,b] = [1.5707963267947436,1.5707963267951697],f((a+b)/2) = 16697052243463.29
it = 48,[a,b] = [1.5707963267947436,1.5707963267949565],f((a+b)/2) = -21417587553013.977
it = 49,[a,b] = [1.57079632679485,1.5707963267949565],f((a+b)/2) = 151512722541384.88
it = 50,[a,b] = [1.57079632679485,1.5707963267949032],f((a+b)/2) = -49887138373250.875
it = 51,[a,b] = [1.5707963267948766,1.5707963267949032],f((a+b)/2) = -148752623989352.16
it = 52,[a,b] = [1.57079632679489,1.5707963267949032],f((a+b)/2) = -1.6331239353195368e+16
it = 53,[a,b] = [1.5707963267948966,1.5707963267949032],f((a+b)/2) = 305863083272329.44
it = 54,[a,b] = [1.5707963267948966,1.5707963267948999],f((a+b)/2) = 583048052969232.8
it = 55,[a,b] = [1.5707963267948966,1.5707963267948983],f((a+b)/2) = 1209268686914963.5
it = 56,[a,b] = [1.5707963267948966,1.5707963267948974],f((a+b)/2) = 2611942160735617.0
it = 57,[a,b] = [1.5707963267948966,1.570796326794897],f((a+b)/2) = 6218431163823740.0
it = 58,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 59,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 60,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 61,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 62,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 63,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 64,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 65,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 66,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 67,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 68,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 69,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 70,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 71,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 72,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 73,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 74,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 75,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 76,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 77,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 78,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 79,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 80,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 81,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 82,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 83,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 84,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 85,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 86,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 87,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 88,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 89,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 90,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 91,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 92,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 93,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 94,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 95,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 96,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 97,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 98,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
it = 99,[a,b] = [1.5707963267948966,1.5707963267948968],f((a+b)/2) = -1.6331239353195368e+16
x= 1.5707963267948966

Process finished with exit code 0

你可能感兴趣的:(#,数值分析,全文检索,android,微信小程序,python)