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).

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 ….

[ad type=”banner”]

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. Then f(x) has at least one zero between a and b. If for a function (f(a) < 0 and f(b) > 0) or (f(a) > 0 and f(b) < 0), then it is guaranteed that it has at least one root between them.

Assumptions:

  1. f(x) is a continuous function in interval [a, b]
  2. f(a) * f(b) < 0

Steps:

  1. Find middle point c= (a + b)/2 .
  2. If f(c) == 0, then c is the root of the solution.
  3. Else f(c) != 0
    1. If value f(a)*f(c) < 0 then root lies between a and c. So we recur for a and c
    2. Else If f(b)*f(c) < 0 then root lies between b and c. So we recur b and c.
    3. 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).Bisection

 

Below is C++ implementation of above steps.

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20for%20implementation%20of%20Bisection%20Method%20for%0A%2F%2F%20solving%20equations%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%23define%20EPSILON%200.01%0A%20%0A%2F%2F%20An%20example%20function%20whose%20solution%20is%20determined%20using%0A%2F%2F%20Bisection%20Method.%20The%20function%20is%20x%5E3%20-%20x%5E2%20%20%2B%202%0Adouble%20func(double%20x)%0A%7B%0A%20%20%20%20return%20x*x*x%20-%20x*x%20%2B%202%3B%0A%7D%0A%20%0A%2F%2F%20Prints%20root%20of%20func(x)%20with%20error%20of%20EPSILON%0Avoid%20bisection(double%20a%2C%20double%20b)%0A%7B%0A%20%20%20%20if%20(func(a)%20*%20func(b)%20%3E%3D%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22You%20have%20not%20assumed%20right%20a%20and%20b%5Cn%22%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20double%20c%20%3D%20a%3B%0A%20%20%20%20while%20((b-a)%20%3E%3D%20EPSILON)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20middle%20point%0A%20%20%20%20%20%20%20%20c%20%3D%20(a%2Bb)%2F2%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20middle%20point%20is%20root%0A%20%20%20%20%20%20%20%20if%20(func(c)%20%3D%3D%200.0)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Decide%20the%20side%20to%20repeat%20the%20steps%0A%20%20%20%20%20%20%20%20else%20if%20(func(c)*func(a)%20%3C%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20b%20%3D%20c%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20a%20%3D%20c%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%20%3C%3C%20%22The%20value%20of%20root%20is%20%3A%20%22%20%3C%3C%20c%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Initial%20values%20assumed%0A%20%20%20%20double%20a%20%3D-200%2C%20b%20%3D%20300%3B%0A%20%20%20%20bisection(a%2C%20b)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++ Program” highlight=”” provider=”manual”/]

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.

[ad type=”banner”]

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,