How to do the Bisection method in Python

前端 未结 7 2448
小蘑菇
小蘑菇 2021-02-15 15:05

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

The Bisecti

7条回答
  •  面向向阳花
    2021-02-15 15:37

    # Defining Function
    def f(x):
        return x**3-5*x-9
    
    # Implementing Bisection Method
    def bisection(x0,x1,e):
        step = 1
        print('\n\n*** BISECTION METHOD IMPLEMENTATION ***')
        condition = True
        while condition:
            x2 = (x0 + x1)/2
            print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))
    
            if f(x0) * f(x2) < 0:
                x1 = x2
            else:
                x0 = x2
    
            step = step + 1
            condition = abs(f(x2)) > e
    
        print('\nRequired Root is : %0.8f' % x2)
    
    
    # Input Section
    x0 = input('First Guess: ')
    x1 = input('Second Guess: ')
    e = input('Tolerable Error: ')
    
    # Converting input to float
    x0 = float(x0)
    x1 = float(x1)
    e = float(e)
    
    #Note: You can combine above two section like this
    # x0 = float(input('First Guess: '))
    # x1 = float(input('Second Guess: '))
    # e = float(input('Tolerable Error: '))
    
    
    # Checking Correctness of initial guess values and bisecting
    if f(x0) * f(x1) > 0.0:
        print('Given guess values do not bracket the root.')
        print('Try Again with different guess values.')
    else:
        bisection(x0,x1,e)
    
    

    Code and Output Here

    Additionally codesansar.com/numerical-methods/ has large collection of algorithms, pseudocodes, and programs using different programming languages for Numerical Analysis.

提交回复
热议问题