Bisection Method

quad[x_]:=(x^2-2)/2;
cos[x_]:=Cos[x];
expo[x_]:=Exp[x]-2-x;
cube[x_]:=10(x-1.1)(x-0.8)(x-0.6);
plotoptions={PlotRange->{{0,2.5},{-1,1}}, PlotRangePadding->0.25,AspectRatio->Automatic,ImageSize->400};
bisec[f_,{a_,b_}]:=If[f[a]f[b]≤0,If[f[((a+b)/2)]f[a]≤0,{a,((a+b)/2)},If[f[((a+b)/2)]f[b]<0,{((a+b)/2),b }]],{a,b}]
linie[f_,{a_,b_},1]={Min[a,b],Max[a,b]};
linie[f_,{a_,b_},n_]:=If[f[a]f[b]≤0,bisec[f,linie[f,{a,b},n-1]],{a,b}]
intervalle[f_,{a_,b_},nn_]:=Table[{{linie[f,{a,b},i][[1]],-i/30},{linie[f,{a,b},i][[2]],-i/30}},{i,1,nn}];
vertline[a_]:=Line[{{a,-1},{a,1}}]
pl[f_,{a_,b_},n_]:=If[f[a]f[b]<=0,Row[{"current interval: [",linie[f,{a,b},n][[1]],", ",linie[f,{a,b},n][[2]] ,"]"}],Row[{"no sign change in current interval"}]]

Manipulate[Show[{Plot[f[x],{x,0,2.5},PlotStyle->{{Thickness[0.005]},{Thickness[0.001]},{Thickness[0.001]},{Thickness[0.001]},{Thickness[0.001]}},Evaluate[plotoptions],Filling->{1->Axis},PlotLabel ->pl[f,{aP[[1]],bP[[1]]},nn ]], 
Graphics[{Red,Map[Line,intervalle[f,{aP[[1]],bP[[1]]},nn]] } ],
Graphics[{Dashed,Map[vertline,linie[f,{aP[[1]],bP[[1]]},nn]]  }]},
ImageSize->{500,375}
],
{{f,quad,""},{quad->"((x^2)/2)-1",cos->"cos(x)",expo->"(E^x)-2-x", cube->"10(x-1.1)(x-0.8)(x-0.6)"},ControlType->RadioButtonBar,ControlPlacement->Top},
{{aP,{0.5,0}},{0,0},{2.5,0},Locator,Appearance->Style["|",20,Bold,Red]},
{{bP,{2,    0}},{0,0},{2.5,0},Locator,Appearance->Style["|",20,Bold,Red]},
{{nn,1,"iteration step"},1,12,1,Appearance->"Labeled"},TrackedSymbols->Manipulate,
SaveDefinitions->True]

CAPTION

This Demonstration shows the steps of the bisection root-finding method for a set of functions. You can choose the initial interval by dragging the vertical, dashed lines. Each iteration step halves the current interval into two subintervals; the next interval in the sequence is the subinterval with a sign change for the function (indicated by the red horizontal lines). The method always converges to a root of f if f is continuous and f(a) and f(b) have opposite sign.

THIS NOTEBOOK IS THE SOURCE CODE FROM

"Bisection Method" from the Wolfram Demonstrations Project
 http://demonstrations.wolfram.com/BisectionMethod/

Contributed by: Edda Eich-Soellner (University of Applied Sciences, München, Germany)

A full-function Wolfram Mathematica system (Version 6 or higher) is required to edit this notebook.
GET WOLFRAM MATHEMATICA »

© Wolfram Demonstrations Project & Contributors   |   Terms of Use   |   Make a new version of this Demonstration »