To find square root of n by bisection method | Python

Introduction

     The bisection method is a simple numerical technique used to find the roots of a function within a specified interval. One common application of the bisection method is finding square roots. In this blog post, we'll explore how to use the bisection method to approximate both the positive and negative square roots of a given number. We'll provide a Python implementation and discuss the steps involved in the process.

Understanding the Bisection Method:

    The bisection method works by repeatedly dividing the interval in half and selecting the subinterval that contains the root of the function. It relies on the intermediate value theorem, which states that if a continuous function changes sign over an interval, it must have at least one root within that interval.


Code

    
     
    def f(x, n):
        return x * x - n
    
    def bisection_method(n):
        a = 0.0
        b = n
        x = (a + b) / 2
    
        print("The successive approximation of positive square root of", n)
        while abs(f(x, n)) > 0.0000001:
            x = (a + b) / 2
            print(x)
            if f(x, n) < 0:
                a = x
            elif f(x, n) > 0:
                b = x
    
        a = 0.0
        b = -1 * n
        x = (a + b) / 2
    
        print("The successive approximation of negative square root of", n)
        while abs(f(x, n)) > 0.0000001:
            x = (a + b) / 2
            print(x)
            if f(x, n) < 0:
                a = x
            elif f(x, n) > 0:
                b = x
    
    # Input: Number whose square root is to be found
    n = float(input("Enter the value of n: "))
    
    # Perform bisection method to find square roots
    bisection_method(n)
    
       
     


Output


    Enter the value of n: 25
    The successive approximation of positive square root of 25
    12.5
    6.25
    3.125
    4.6875
    5.46875
    5.078125
    4.8828125
    4.98046875
    5.029296875
    5.0048828125
    The successive approximation of negative square root of 25
    -12.5
    -6.25
    -9.375
    -7.8125
    -8.59375
    -9.984375
    -9.1796875
    -9.58203125
    -9.783203125
    -9.8837890625
       




Explanation

1. We define a function f(x, n) that represents the function x^2 - n, where is the given number.
2.The bisection_method function implements the bisection method to find both positive and negative     square roots of n
3. We initialize the interval boundaries a and b and perform successive approximations until the     function value becomes sufficiently close to zero.
4. The bisection process continues until the width of the interval becomes smaller than the specified tolerance.
5. We prompt the user to input the value of n whose square root is to be found.
6. Finally, we call the bisection_method function to find the square roots of n.

Conclusion

     The bisection method is a reliable numerical technique for finding roots of functions, including square roots. In this blog post, we've discussed how to apply the bisection method to approximate both positive and negative square roots of a given number. We've provided a Python implementation and explained the steps involved in the process. This method is widely used in mathematics, engineering, and scientific computing for solving various types of equations and finding roots of functions.

Post a Comment

0 Comments