Skip to main content

Section 30.2 The Bisection Method

We’ve already met Bisection Search as a way to search a sorted list. The same halving idea can be used to solve equations of the form \(f(x) = 0\) for a continuous function \(f\text{:}\) this is the bisection method.
Suppose we know that a solution lies somewhere between \(x=a\) and \(x=b\) (that is, \(f(a)\) and \(f(b)\) have opposite signs). We compute the midpoint \(x_1 = (a+b)/2\text{,}\) and check the sign of \(f(x_1)\text{:}\) if it has the same sign as \(f(a)\text{,}\) the root must be between \(x_1\) and \(b\text{,}\) so we replace \(a\) with \(x_1\text{.}\) Otherwise, the root is between \(a\) and \(x_1\text{,}\) so we replace \(b\) with \(x_1\text{.}\) Either way, we now have a shorter interval containing the root, and we repeat the process until the interval is “short enough”.

Activity 30.1.

We want to write a program that finds the solution of the equation \(x^3 - 4x - 9 = 0\) using the bisection method.
  1. Write a function float func(float x) that computes \(f(x) = x^3 - 4x - 9\text{.}\)
  2. Populate two arrays with the \(x\) and \(y\) coordinates over the interval \([a,b] = [2.0, 4.0]\text{,}\) and plot them using gnuplot, as in the previous section.
  3. Compute a first estimate \(x_1 = (a+b)/2\text{.}\) Determine whether the true solution lies between \(a\) and \(x_1\text{,}\) or between \(x_1\) and \(b\text{,}\) by comparing the signs of \(f(a)\text{,}\) \(f(x_1)\text{,}\) and \(f(b)\text{,}\) then replace \(a\) or \(b\) with \(x_1\) to obtain a shorter interval that still contains the solution, and repeat.
  4. Come up with a reasonable way to stop once your approximation is “good enough”, and display the solution both in the terminal and on your graph.

Compile and run the code below in your own C environment.

#include <stdio.h>
#include <stdlib.h>

int main(void){
	/* define two arrays for coordinates x (between 2 and 4) and y*/

	/* plot the function to see what it looks like */

    return 0;
}

//Add your function here