Application Center - Maplesoft

App Preview:

Bisection Method Maplet

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

Bisection Method
by Judith Koeller, University of Waterloo, Canada, jakoelle@math.uwaterloo.ca

Note: This worksheet demonstrates the the bisection method for finding roots of an equation.

2004 Judith Koeller

The bisection method can be used to approximate a solution p to an equation f(p)=0 where f(x) is a continuous function. It calculates a sequence of values of
c_1 c_1 c_1 , c_2, c_3,...., each of which if an approximation to the root p. It may take infinitely many c_i's to get to p, so we stop with a c_i whose distance from p is at most some small number that we choose.

Bisection Method

Start with a,b (such that a<b and f(a), f(b) have opposite signs). If f is continuous, by the Intermediate Value Theorem, there is a number p in [a,b] such that f(p)=0. The distance from a to b is |b-a|. So b is no further than |b-a| from the root p.

Let c_1 = 1 2 a + 1 2 b c_1 1 2 a 1 2 b c_1 = 1/2*a+1/2*b , which is the midpoint of [a,b]. Determine f(c_1). If f(a) and f(c_1) have the same sign, choose the next endpoints as [c_1,b]; if f(c_1) and f(b) have the same sign, choose the next endpoints as [a,c_1]. The size of either interval is 1 2 b - a 1 2 b a 1/2*abs(b-a) , so p is no further from c_1 than 1 2 b - a 1 2 b a 1/2*abs(b-a) .

We repeat this process as often as we need to, each time finding a new c_i half-way between the two endpoints. At each step, the distance between the endpoints becomes half as long, so our error is cut in half each time. We continue the process until the distance between the two endpoints is suitably short; we know that both our last c_i and our root p lie in a very small interval, therefore they must be very close together.


The following maplet demonstrates the bisection method to approximate a root to the function
x 2 - 5 x 2 -5 x^2-5 . The "Next Step" button can be used to show the next c_i. The "Zoom" button can be changes the viewing rectangle of the graph to focus on the interval of bisection.

> restart:
with(plots):

with(Maplets[Elements]):


#Global variables

i:=0: #counts iterations of the bisection method

c := array(1..50): #stores the successive approximations to the root

FirstIt := 1: #when you zoom in, you will no longer graph the first approximations c1,c2,.... Start with FirstIt.

#Viewing Rectangle

yMin := -5:

yMax := 4:

xLeft :=2:

xRight := 3.01:


# Evaluates the function at a given point

fEval := proc(xP)

        local g;

        g := x-> evalf(x^2-5):

        return g(xP):

end:


#Adjusts the viewing rectangle of the function and replots it

ZoomPlot:= proc(a,b,midp)

     local curve, bisector, areacurve, leftright, approx,approxText:

     global c,i, FirstIt, xLeft, xRight, yMin, yMax:


     #We only show the most recents c_i's - drop the earlier ones

     if (i>1) then

        FirstIt := i-1:

     end if:


     #Adjust the height of the viewing rectangle

     xLeft := evalf[10](a):

     xRight := evalf[10](b + (b-midp)/128):

     yMin := fEval(xLeft):

     yMax := fEval(xRight):


     #Create and display the elements of the plot

     curve := implicitplot({y=x^2-5},x=xLeft..xRight,y=yMin..yMax,color=green):

     bisector := implicitplot({x=midp},x=xLeft..xRight,y=yMin..yMax,color=red):

     leftright := implicitplot({x=a,x=b},x=xLeft..xRight,y=yMin..yMax,color=blue, linestyle=DASH):

     approx := pointplot({seq([c[j],fEval(c[j])],j=FirstIt..i)},color=green);

     approxText := textplot({seq([c[j],fEval(c[j]),c_||j],j=FirstIt..i)},align={BELOW,RIGHT});

     display(curve,bisector,leftright,approx,approxText);

end:


# Plots the function with the endpoints and bisection lines

BisectionPlot:= proc(a,b,midp,InitLeft,InitRight)

     local curve, bisector, areacurve, leftright,approx, approxText:

     global c, i:

        

     #Record the next approximation c_i

     i:=i+1:

     c[i]:=midp:


     #Create and display the elements of the plot

     curve := implicitplot({y=x^2-5},x=xLeft..xRight,y=yMin..yMax,color=green):

     bisector := implicitplot({x=midp},x=xLeft..xRight,y=yMin..yMax,color=red):

     leftright := implicitplot({x=a,x=b},x=xLeft..xRight,y=yMin..yMax,color=blue, linestyle=DASH):

     approx := pointplot({seq([c[j],fEval(c[j])],j=FirstIt..i)},color=green);

     approxText := textplot({seq([c[j],fEval(c[j]),c_||j],j=FirstIt..i)},align={BELOW,RIGHT});

     display(curve,bisector,leftright,approx,approxText);

end:


#Triggers the redraw of the graph zoomed in to the endpoints

ZoomIn:=()->Action(

       Evaluate('PL1' = 'ZoomPlot(a,b,midp)')

):



#Triggers the redraw of the graph with new bisection points, maintaining the previous viewing rectangle

BisectIteration:=()->Action(

       Evaluate('a' = `if`(fEval(midp)*fEval(a)>0,evalf(midp),a)),        

       Evaluate('b' = `if`(fEval(midp)*fEval(b)>0,evalf(midp),b)),

       Evaluate('midp' = evalf[10]((a+b)/2)),

       Evaluate('PL1' = 'BisectionPlot(a,b,midp,InitLeft,InitRight)'),

       Evaluate('fmidp' = 'evalf[15](fEval(midp))')

):



maplet := Maplet(

 Window('title'= "Bisection Method",

 BoxLayout['BL1'](

  BoxColumn(

 BoxRow('halign'=left,"f(x): ", MathMLViewer['eq']('value' = MathML[Export](y=x^2-5), 'height'=50, 'width'=120)),

 BoxRow('halign'=left,Label['lbla']("Endpoints: from "), TextField[a]("2", 'editable'=false, 'halign'=right, 'width'=10)),

   BoxRow('halign'=left, " to " , TextField[b]("3", 'editable'=false, 'halign'=right, 'width'=10)),

  BoxRow('halign'=left,Label['ci']("Midpoint: c_i = "), TextField[midp]("2.5", 'editable'=false, 'halign'=right, 'width'=10)),

 BoxRow('halign'=left,"f(c_i) =", TextField[fmidp]("1.25", 'editable'=false, 'halign'=right, 'width'=10)),


  BoxRow('halign'=left,Plotter['PL1'](BisectionPlot(2,3,2.5,0,3) )),

   BoxRow('halign'=left, Button['Bisect']("Next midpoint", 'onclick'=BisectIteration()),Button['ZoomIn']("Zoom In", 'onclick'=ZoomIn()))

 )

) #box layout

) #window

 

):



Maplets[Display](maplet);