Bisection Method

  Bisection Method:

The method states that if a function f(x) is continuous between a and b, and f(a) and f(b) are of opposite signs, then there exists at least one root between a and b. 

– Let f(a) be negative and f(b) be positive. Then the root lies between a and b and let its approximate value be given by x0 = (a + b)/2. 

– If f(x0) = 0,we conclude that x0 is a root of the equation f(x) = 0. Otherwise, the root lies either between x0 and b, or between x0 and a depending on whether f(x0) is negative or positive. 

– We designate this new interval as [a1, b1] whose length is |b − a|/2.

– This is bisected at x1 and the new interval will be exactly half the length of the previous one. We repeat this process until the latest interval (which contains the root) is as small as desired ϵ >= |b−a|/ 2^n.


Program for Bisection Method :


#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

import math

# - - - - - - - - - - - - - Equation - - - - - - - - - - - - - - - - 

def func ( x):      # f(x) = 5* exp (-x)+x -5

    return 4* math . exp (x) * math . sin (x) -1 

    


# - - - - - - - - - - - Bisection Method - - - - - - - - - - - - - - 


def bisection (a ,b) :


    if( func (a) * func (b) >= 0) :

        print (" you have not choose right a and b \n")

        return


    c = a ; # consider c is our root of equation

    while ((b -a) >= 0.00001) : # error [e *2^ n = (b-a)]

        c =( a+ b) /2

        # decide the side to repeat the steps

        if ( func ( c) ==0.0) :

            break

        if ( func ( c)* func (a) < 0) :

            b = c # [a, c]

        else : # [ func (c)* func (b) < 0 ]

            a = c # [c, b]


    print (" the value of root x is : ", c )

    

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


a = -1;

b = 1;

bisection (a , b)

Comments

Popular posts from this blog

Solving Kepler’s equation by Newton-Raphson method

Velapanti Python Program

To find mean , standard deviation and frequency distribution of an actual data set (Python Program)