Module for the Bisection Method

Numerical Methods using Mathematica  
Complementary software supplements for   
Numerical Methods textbooks  
John H. Mathews and Kurtis Fink, 2004

Background. The bisection method is one of the bracketing methods for finding roots of equations.
Implementation.  Given a function f(x) and an interval which might contain a root, perform a predetermined number of iterations using the bisection method. 
Limitations.  Investigate the result of applying the bisection method over an interval where there is a discontinuity.  Apply the bisection method for a function using an interval where there are distinct roots.  Apply the bisection method over a "large" interval.

Theorem (Bisection Theorem).  Assume that  f∈C[a,b] and that there exists a number r∈[a,b] such that f(r)=0.  
If  f(a) and f(b) have opposite signs, and {(Subscript[c, n])} represents the sequence of midpoints generated by the bisection process, then

		|r-(Subscript[c, n])| ≤((b - a)/(2^(n+1)))   for   n=0,1,...,

and the sequence {(Subscript[c, n])} converges to the zero  x=r.  

That is,  	(lim_(k -> ∞)) (Subscript[c, n]) = r.

Proof  Bisection Method  Bisection Method

Computer Programs  Bisection Method  Bisection Method

Mathematica Subroutine (Bisection Method).

Bisection[a0_,b0_,m_] :=
Module[{},
a = N[a0];  
b = N[b0];  
c = ((a + b)/2); 
k = 0; 
output={{k,a,c,b,f[c]}}; 
While[k < m,
If[ Sign[f[b]] ==  Sign[f[c]], 
b = c, a = c; ]; 
c = ((a + b)/2); 
k = k+1; 
output=Append[output,{k,a,c,b,f[c]}]; ]; 
Print[NumberForm[TableForm[output,
TableHeadings->{None,{"k","(Subscript[a, k])","(Subscript[c, k])","(Subscript[b, k])","f[(Subscript[c, k])]"}}],16]]; 
Print["  c  = ",NumberForm[c,16] ]; 
Print[" Δc  = ±",((b-a)/2)]; 
Print["f[c] = ",NumberForm[f[c],16] ]; ]

Example 1.  Find all the real solutions to the cubic equation  (x^3)+4 (x^2)-10=0.

Solution 1.

f[x_]=(x^3)+4 (x^2)-10; 
Print["f[x] = ",f[x] ];

Plot the function.

(Needs["Graphics`Colors`"];) Plot[f[x],{x,-4,2},PlotStyle->Magenta] (Print["y = f[x] = ",f[x]];)

There appears to be only one real root which lies in the interval [1,2].

Plot[f[x],{x,1,2},PlotStyle->Magenta] (Print["y = f[x] = ",f[x]];)

Call the Bisection subroutine on the interval [1,2] using 10 iterations

Bisection[1,2,10];

After 10 iterations, the interval has been reduced to [a,b] where

Print["a = ",NumberForm[a,16] ]; 
Print["b = ",NumberForm[b,16] ]; 
Print[""]; 
Print["[a, b] = [",a,", ",b,"]"];

The root lies somewhere in the interval [a,b] the width of which is

Print["b-a = ",NumberForm[b-a,16] ];

The reported root is alleged to be

Print["c = ",NumberForm[c,16] ];

The accuracy we can guarantee is one half of the interval width.

Print["((b - a)/2) = ",NumberForm[((b-a)/2),16] ];

Is this the desired accuracy you want ?  If not, more iterations are required.

Bisection[1,2,20];

Compare our result with Mathematica's built in root finder.

solset=NSolve[f[x]==0,x]; 
NumberForm[TableForm[solset],16]

Question.  Why is Mathematica's answer different ?
How many bisections would it take to reduce the interval width to (10^(-16)) ?

Bisection[1,2,50];

Remember. The bisection method can only be used to find a real root in an interval [a,b] in which f[x] changes sign.

Example 2.  Use the cubic equation  (x^3)+4 (x^2)-10=0  in Example 1 and perform the following call to the bisection method. 

Bisection[0,1,30];

Solution 2.

Bisection[0,1,30];

Caution. You must be able to explain what the output means.  Would you give it to your boss ?

Reduce the volume of printout.

After you have debugged you program and it is working properly, delete the unnecessary print statements.

Concise Program for the Bisection Method

Bisection[a0_,b0_,m_] :=
Module[{a = N[a0],b = N[b0]},
c = ((a + b)/2); 
k = 0; 
While[k < m,
If[ Sign[f[b]] ==  Sign[f[c]], 
b = c, a = c; ]; 
c = ((a + b)/2); 
k = k+1;  ]; 
Print["  c  = ",NumberForm[c,16] ]; 
Print[" Δc  = ±",((b-a)/2)]; 
Print["f[c] = ",NumberForm[f[c],16] ]; ];

Now test the example to see if it still works. Use the last case in Example 1 given above and compare with the previous results.

Bisection[1,2,30];

Reducing the Computational Load for the Bisection Method

The following program uses fewer computations in the bisection method and is the traditional way to do it.  Can you determine how many fewer functional evaluations are used ?

Bisection[a0_,b0_,m_] :=
Module[{a = N[a0],b = N[b0]},
c = ((a + b)/2); 
Yb = f[b]; 
Yc = f[c]; 
k = 0; 
While[k < m,
If[ Sign[Yb] ==  Sign[Yc], 
b = c; 
Yb = Yc, 
a = c; ]; 
c = ((a + b)/2); 
Yc = f[c]; 
k = k+1;  ]; 
  Print["  c  = ",NumberForm[c,16] ]; 
  Print[" Δc  = ±",((b-a)/2)]; 
  Print["f[c] = ",NumberForm[Yc,16] ]; ]

Various Scenarios and Animations for the Bisection Method.

Bisection[a0_,b0_,m_] :=
Module[{},
a = N[a0];  
b = N[b0];  
c = ((a + b)/2); 
k = 0; 
output={{k,a,c,b,f[c]}}; 
While[k < m,
If[ Sign[f[b]] ==  Sign[f[c]], 
b = c, a = c; ]; 
c = ((a + b)/2); 
k = k+1; 
output=Append[output,{k,a,c,b,f[c]}]; ]; 
Print[NumberForm[TableForm[output,
TableHeadings->{None,{"k","(Subscript[a, k])","(Subscript[c, k])","(Subscript[b, k])","f[(Subscript[c, k])]"}}],16]]; 
Print["  c  = ",NumberForm[c,16] ]; 
Print[" Δc  = ±",((b-a)/2)]; 
Print["f[c] = ",NumberForm[f[c],16] ]; ]

Example 3.  Convergence  Find the solution to the cubic equation  (x^3)+4 (x^2)-10=0.  Use the starting interval  [a,b]=[-1,2].

Solution 3.

(Needs["Graphics`Colors`"];) (f[x_]=(x^3)+4 (x^2)-10;) (a=-1.`;) (b=2.`;) (Bisection[a,b,30];) Plot[f[x],{x,-1.05`,2.05`},PlotRange->{{-1.05`,2.05`},{-10,15}},PlotStyle->Magenta]

Animation 1.  ( Bisection Method  Bisection Method ).  Internet hyperlink.

Example 4.  Not a root located  Find the solution to the equation  Tan[x]=0.  Use the starting interval  [a,b]=[0,2].

Solution 4.

(Needs["Graphics`Colors`"];) (f[x_]=Tan[x];) (a=0.`;) (b=2.`;) (Bisection[a,b,15];) Plot[f[x],{x,-0.05`,2.05`},PlotRange->{{-0.05`,2.05`},{-15,15}},PlotStyle->Magenta]

Note. The bisection method located a pole of  f[x]=Tan[x].  This is where the graph has a vertical asymptote.

Solve[1/f[x] == 0,x]

Animation 2.  ( Bisection Method  Bisection Method ).  Internet hyperlink.

Animations (Bisection Method  Bisection Method).  Internet hyperlinks to animations.

Old Lab Project (Bisection Method  Bisection Method).  Internet hyperlinks to an old lab project.

Research Experience for Undergraduates

Bisection Method  Bisection Method  Internet hyperlinks to web sites and a bibliography of articles.