The Bisection Method

Helmut Knaust, Department of Mathematical Sciences, UTEP, El Paso TX 79968
hknaust@utep.edu
1/30/2012

Bisect[f_,x_,x0_,y0_,steps_:10]:=Module[{F,a,b,tab},F=Function[x,f];{a[1],b[1]}={N[x0],N[y0]};If[Sign[F[a[1]]]==Sign[F[b[1]]],Print["The function must have opposite signs at the two initial points. \nf(",x0,")=", F[a[1]],"\tf(",y0,") = ",F[b[1]]],
a[n_]:=a[n]=If[Sign[F[(a[n-1]+b[n-1])/2]]==Sign[F[a[1]]],
(a[n-1]+b[n-1])/2,a[n-1]];b[n_]:=b[n]=If[Sign[F[(a[n-1]+b[n-1])/2]]==Sign[F[a[1]]],
b[n-1],(a[n-1]+b[n-1])/2];
tab=Table[{{a[k],F[a[k]]},{b[k],F[b[k]]},(a[k]+b[k])/2},{k,1,steps}];Manipulate[Show[ListPlot[Transpose[tab[[1;;k]]][[1;;2]],PlotRange->All,PlotLabel->(a[k]+b[k])/2,Epilog->{Blue,AbsoluteThickness[2],Line[{{a[k],0},{b[k],0}}]},PlotStyle->Directive[Red,AbsolutePointSize[5]]],Plot[F[x],{x,a[1],b[1]},PlotRange->All,PlotStyle->Dotted]],{k,1,steps,1}]]]

The command Bisect[f(x),x,(Subscript[x, 0]),(Subscript[y, 0]),steps] finds approximations to a root of the function f (x), with independent variable x, starting at the initial values (Subscript[x, 0]) and (Subscript[y, 0]). The last argument “steps” is optional and gives the number of iterations of the algorithm.
The Bisection Method is only guaranteed to work if f is continuous on [(Subscript[x, 0]),(Subscript[y, 0])] and if f((Subscript[x, 0])) and f((Subscript[y, 0])) have opposite signs.

Bisect[z^5-z^3+z^2-4z+5,z,-2.2,2]

Bisect[t^2+1,t,-2,2]