Program for Bisection Method

06 Apr, 2021
Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents algebraic or transcendental equation. Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0). Example:  
Input: A function of x, for example x3 - x2 + 2.     
       And two values: a = -200 and b = 300 such that
       f(a)*f(b) < 0, i.e., f(a) and f(b) have
       opposite signs.
Output: The value of root is : -1.0025
        OR any other value with allowed
        deviation from root.
What are Algebraic and Transcendental functions? Algebraic function are the one which can be represented in the form of polynomials like f(x) = a1x3 + a2x2 + ..... + e where aa1, a2, ... are constants and x is a variable. Transcendental function are non algebraic functions, for example f(x) = sin(x)*x – 3 or f(x) = ex + x2 or f(x) = ln(x) + x .... What is Bisection Method? The method is also called the interval halving method, the binary search method or the dichotomy method. This method is used to find root of an equation in a given interval that is value of ‘x’ for which f(x) = 0 . The method is based on The Intermediate Value Theorem which states that if f(x) is a continuous function and there are two real numbers a and b such that f(a)*f(b) 0 and f(b) < 0), then it is guaranteed that it has at least one root between them.Assumptions:  
f(x) is a continuous function in interval [a, b]f(a) * f(b) < 0
f(x) is a continuous function in interval [a, b]
f(a) * f(b) < 0
Steps:  
Find middle point c= (a + b)/2 .If f(c) == 0, then c is the root of the solution.Else f(c) != 0If value f(a)*f(c) < 0 then root lies between a and c. So we recur for a and cElse If f(b)*f(c) < 0 then root lies between b and c. So we recur b and c.Else given function doesn’t follow one of assumptions.
Find middle point c= (a + b)/2 .
If f(c) == 0, then c is the root of the solution.
Else f(c) != 0If value f(a)*f(c) < 0 then root lies between a and c. So we recur for a and cElse If f(b)*f(c) < 0 then root lies between b and c. So we recur b and c.Else given function doesn’t follow one of assumptions.
If value f(a)*f(c) < 0 then root lies between a and c. So we recur for a and cElse If f(b)*f(c) < 0 then root lies between b and c. So we recur b and c.Else given function doesn’t follow one of assumptions.
If value f(a)*f(c) < 0 then root lies between a and c. So we recur for a and c
Else If f(b)*f(c) < 0 then root lies between b and c. So we recur b and c.
Else given function doesn’t follow one of assumptions.
Since root may be a floating point number, we repeat above steps while difference between a and b is less than a value ? (A very small value).  
Below is implementation of above steps.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program for implementation of Bisection Method for// solving equations#include<bits/stdc++.h>using namespace std;#define EPSILON 0.01 // An example function whose solution is determined using// Bisection Method. The function is x^3 - x^2  + 2double func(double x){    return x*x*x - x*x + 2;} // Prints root of func(x) with error of EPSILONvoid bisection(double a, double b){    if (func(a) * func(b) >= 0)    {        cout << "You have not assumed right a and b\n";        return;    }     double c = a;    while ((b-a) >= EPSILON)    {        // Find middle point        c = (a+b)/2;         // Check if middle point is root        if (func(c) == 0.0)            break;         // Decide the side to repeat the steps        else if (func(c)*func(a) < 0)            b = c;        else            a = c;    }    cout << "The value of root is : " << c;} // Driver program to test above functionint main(){    // Initial values assumed    double a =-200, b = 300;    bisection(a, b);    return 0;}
// Java program for implementation of Bisection Method// for solving equationsclass GFG{    static final float EPSILON = (float)0.01;     // An example function whose solution is determined using    // Bisection Method. The function is x^3 - x^2  + 2    static double func(double x)    {        return x*x*x - x*x + 2;    }     // Prints root of func(x) with error of EPSILON    static void bisection(double a, double b)    {        if (func(a) * func(b) >= 0)        {            System.out.println("You have not assumed"                        + " right a and b");            return;        }         double c = a;        while ((b-a) >= EPSILON)        {            // Find middle point            c = (a+b)/2;             // Check if middle point is root            if (func(c) == 0.0)                break;             // Decide the side to repeat the steps            else if (func(c)*func(a) < 0)                b = c;            else                a = c;        }                //prints value of c upto 4 decimal places        System.out.printf("The value of root is : %.4f"                        ,c);    }     // Driver program to test above function    public static void main(String[] args)    {        // Initial values assumed        double a =-200, b = 300;        bisection(a, b);    }    // This code is contributed by Nirmal Patel}
# Python program for implementation# of Bisection Method for# solving equations   # An example function whose# solution is determined using# Bisection Method.# The function is x^3 - x^2  + 2def func(x):    return x*x*x - x*x + 2  # Prints root of func(x)# with error of EPSILONdef bisection(a,b):     if (func(a) * func(b) >= 0):        print("You have not assumed right a and b\n")        return      c = a    while ((b-a) >= 0.01):         # Find middle point        c = (a+b)/2          # Check if middle point is root        if (func(c) == 0.0):            break          # Decide the side to repeat the steps        if (func(c)*func(a) < 0):            b = c        else:            a = c                 print("The value of root is : ","%.4f"%c)     # Driver code# Initial values assumeda =-200b = 300bisection(a, b) # This code is contributed# by Anant Agarwal.
// C# program for implementation// of Bisection Method for// solving equationsusing System; class GFG{static float EPSILON = (float)0.01; // An example function whose// solution is determined using// Bisection Method. The function// is x^3 - x^2 + 2static double func(double x){    return x * x * x -           x * x + 2;} // Prints root of func(x)// with error of EPSILONstatic void bisection(double a,                      double b){    if (func(a) * func(b) >= 0)    {        Console.WriteLine("You have not assumed" +                                " right a and b");        return;    }     double c = a;    while ((b - a) >= EPSILON)    {        // Find middle point        c = (a + b) / 2;         // Check if middle        // point is root        if (func(c) == 0.0)            break;         // Decide the side        // to repeat the steps        else if (func(c) * func(a) < 0)            b = c;        else            a = c;    }         // prints value of c    // upto 4 decimal places    Console.WriteLine("The value of " +                      "root is : "+ c);} // Driver Codestatic public void Main (){    // Initial values assumed    double a = -200, b = 300;    bisection(a, b);}} // This code is contributed by ajit
<?php// PHP program for implementation// of Bisection Method for solving// equations$EPSILON = 0.01; // An example function whose// solution is determined// using Bisection Method.// The function is x^3 - x^2 + 2function func($x){    return $x * $x * $x -           $x * $x + 2;} // Prints root of func(x)// with error of EPSILONfunction bisection($a, $b){    global $EPSILON;    if (func($a) *        func($b) >= 0)    {        echo "You have not assumed " .                 "right a and b","\n";        return;    }     $c = $a;    while (($b - $a) >= $EPSILON)    {        // Find middle point        $c = ($a + $b) / 2;         // Check if middle        // point is root        if (func($c) == 0.0)            break;         // Decide the side to        // repeat the steps        else if (func($c) * func($a) < 0)            $b = $c;        else            $a = $c;    }    echo "The value of root is : " , $c;} // Driver Code // Initial values assumed$a =-200;$b = 300;bisection($a, $b); // This code is contributed by ajit?>
<script> // JavaScript program for implementation// of Bisection Method for// solving equations let EPSILON = 0.01;     // An example function whose solution is determined using    // Bisection Method. The function is x^3 - x^2  + 2    function func(x)    {        return x*x*x - x*x + 2;    }       // Prints root of func(x) with error of EPSILON    function bisection(a, b)    {        if (func(a) * func(b) >= 0)        {            document.write("You have not assumed"                        + " right a and b");            return;        }           let c = a;        while ((b-a) >= EPSILON)        {            // Find middle point            c = (a+b)/2;               // Check if middle point is root            if (func(c) == 0.0)                break;               // Decide the side to repeat the steps            else if (func(c)*func(a) < 0)                b = c;            else                a = c;        }                //prints value of c upto 4 decimal places        document.write("The value of " +                      "root is : "+ c.toFixed(4));    }  // Driver program         // Initial values assumed        let a =-200, b = 300;        bisection(a, b);                 // This code is contributed by susmitakundugoaldanga.</script>
Output: 
The value of root is : -1.0025
Time complexity :- Time complexity of this method depends on the assumed values and the function. What are pros and cons? Advantage of the bisection method is that it is guaranteed to be converged. Disadvantage of bisection method is that it cannot detect multiple roots.In general, Bisection method is used to get an initial rough approximation of solution. Then faster converging methods are used to find the solution. We will soon be discussing other methods to solve algebraic and transcendental equationsReferences: Introductory Methods of Numerical Analysis by S.S. Sastry https://en.wikipedia.org/wiki/Bisection_methodThis article is contributed by Abhiraj Smit. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Nims972
jit_t
susmitakundugoaldanga
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Minimum number of jumps to reach end
Find minimum number of coins that make a given value
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Program for Decimal to Binary Conversion