Bisection method in C++

An important part of numerical analysis is the procedure of locating continuous function roots within a predetermined range. In such situations, the bisection approach provides an easy way to identify the roots, sometimes referred to as the interval halving method, binary search method, or dichotomy method. It isn't the fastest approach, but its dependability and simplicity make it a useful tool for numerical calculations.

When working with continuous functions in the interval [a, b], where the function values at the endpoints, f(a) and f(b), have opposing signs, the bisection approach is especially helpful. The existence of at least one root within this range is guaranteed by the intermediate value theorem. The bisection method makes use of continually bisecting the interval and evaluating the function at the midway to converge towards the desired root.

Implementations of the Bisection method in C++:

The C++ implementation of the bisection method is broken down into the following steps:

1. Create the target function for which you are looking for the root first. We'll use the equation f(x) = x3 - 2x2 + 3 as an example.

2. Choose a starting range [a, b] where the signs of the function values at the endpoints are opposite. Its need is essential for the procedure to work.

3. The main concept of the bisection method is to iteratively reduce the interval. Establish a loop that keeps going as long as the interval width (b - a) is wide enough (in this case, 01). Inside the loop:

  • Use the formula c = (a + b) / 2 to determine the current interval's midpoint.
  • Calculate the value of the function at c.
  • Depending on the function value at the midpoint, three choices emerge:
  • If func(c) is equal to 0, then c is the root already, and the process can end.
  • If func(c) and func(a) have opposing signs, update b to c to effectively shrink the interval.
  • Update a to c if func(c) and func(b) have opposing signs.
  • Up until the interval width is narrow enough, the loop keeps adjusting the interval. Now, the value of c comes close to representing the root.

4. In the end, the value of c of the loop represents the function's approximate root.

Example:

The full C++ implementation of the bisection method is provided here:

Output:

The value of root is = -0.998535

Explanation:

  • The initial interval is specified by the program to be a = -10 and b = 20.
  • The midpoint c of the present interval (a, b) is determined by the bisection method, which iteratively refines the interval.
  • (A + B) / 2 is used to compute the value of c.
  • At the halfway point, the program examines the value of the function func(c):
  • Since c is the root, the loop ends if func(c) equals 0.
  • If func(c) * func(a)= 0, then changing b to c will shorten the interval because func(c) and func(a) have opposite signs.
  • If none of the conditions are true, then the interval is shortened by updating a to c because func(c) and func(b) have opposing signs.
  • Until the interval width (b - a) drops below 01, the loop is repeated.
  • In this example, the approximated root c's value is about -0.998535. After that, it is printed by the program.

Conclusion:

In conclusion, the bisection method is a solid method for roughly approximating the roots of continuous functions within specified intervals. Although it's not the fastest method out now, its simplicity and resilience make it a useful tool for numerical analysis. The method effectively reduces the search space for roots by iteratively halving intervals and evaluating function values. The example output from the offered C++ implementation shows how effective it is in locating roots. Although more complex algorithms are available, the bisection method's simplicity and versatility maintain its position in the arsenal of numerical problem-solving techniques.






Latest Courses