File:Bisection method.gif
Summary
| Description |
English: Finding the roots of a function is a very common problem in computational Physics, and the bisection method is a simple and effective (albeit far from optimal) way to do that.
The idea is that you start by "bracketing" your root between a value where the function is negative and one where it is positive. You then take the midpoint between them, check if your function there is positive or negative and update the bracket. |
| Date | |
| Source | https://mathstodon.xyz/@j_bertolotti/114834711962600450 |
| Author | Clodovendro |
| Permission (Reusing this file) |
https://mathstodon.xyz/@j_bertolotti/114533575175127912 |
Mathematica 14.0 code
f[x_] := Tan[x] - Sqrt[(8/x)^2 - 1];
negativebraket = 0.5;
positivebraket = 1.5;
bracketlist = {{negativebraket, positivebraket}};
Do[
midpoint = Mean[{negativebraket, positivebraket}];
tmp = Transpose[{{negativebraket, midpoint, positivebraket},
Sign[f[#] & /@ {negativebraket, midpoint, positivebraket}]}];
negativebraket = Sort[Select[tmp, #[[2]] < 0 &], #1[[2]] < #2[[2]] &][[1, 1]];
positivebraket = Sort[Select[tmp, #[[2]] > 0 &], #1[[2]] < #2[[2]] &][[-1, 1]];
AppendTo[bracketlist, {negativebraket, positivebraket}];
, 30];
speedstep[t_] := t^2;
plot1[a_, b_, t_] :=
Plot[f[x], {x, 0.5, \[Pi]/2}, PlotStyle -> Black,
AxesLabel -> {"z",
"tan(z)-\!\(\*SqrtBox[\(\*SuperscriptBox[\((\*SubscriptBox[\(z\),\
\(0\)]/z)\), \(2\)] - 1\)]\)"}, LabelStyle -> {Bold, Black},
Epilog -> {PointSize -> 0.02, Red,
Point[{{a + speedstep[t] ((a + b)/2 - a),
0}, {b + speedstep[t] ((a + b)/2 - b), 0}}], Gray,
Point[{{a, 0}, {b, 0}}], Opacity[0.3],
Rectangle[{-10, -100}, {a, 100}], Rectangle[{b, -100}, {10, 100}]}];
plot2[a_, b_, t_] :=
Plot[f[x], {x, 0.5, \[Pi]/2}, PlotStyle -> Black,
AxesLabel -> {"z",
"tan(z)-\!\(\*SqrtBox[\(\*SuperscriptBox[\((\*SubscriptBox[\(z\),\
\(0\)]/z)\), \(2\)] - 1\)]\)"}, LabelStyle -> {Bold, Black},
Epilog -> {PointSize -> 0.02 (1 - t), Red, Point[{(a + b)/2, 0}],
Line[{{(a + b)/2, -30*speedstep[t]}, {(a + b)/2, 30*speedstep[t]}}],
Gray, PointSize -> 0.02, Point[{{a, 0}, {b, 0}}], Opacity[0.3],
Rectangle[{-10, -100}, {a, 100}], Rectangle[{b, -100}, {10, 100}]}];
plot3[a_, b_, a1_, b1_, t_] :=
Plot[f[x], {x, 0.5, \[Pi]/2}, PlotStyle -> Black,
AxesLabel -> {"z",
"tan(z)-\!\(\*SqrtBox[\(\*SuperscriptBox[\((\*SubscriptBox[\(z\),\
\(0\)]/z)\), \(2\)] - 1\)]\)"}, LabelStyle -> {Bold, Black},
Epilog -> {Opacity[1 - t], Red,
Line[{{(a + b)/2, -30}, {(a + b)/2, 30}}], Opacity[1], Gray,
PointSize -> 0.02,
Point[{{a + speedstep[t] (a1 - a), 0}, {b + speedstep[t] (b1 - b),
0}}], Opacity[0.3],
Rectangle[{-10, -100}, {a + speedstep[t] (a1 - a), 100}],
Rectangle[{b + speedstep[t] (b1 - b), -100}, {10, 100}]}];
frames =
Flatten@Table[
Join[Table[
plot1[bracketlist[[j, 1]],
bracketlist[[j, 2]], \[Tau]], {\[Tau], 0, 1, 1/(
15 (bracketlist[[j, 2]] - bracketlist[[j, 1]]))}],
Table[plot2[bracketlist[[j, 1]],
bracketlist[[j, 2]], \[Tau]], {\[Tau], 0, 1, 1/5}],
Table[plot3[bracketlist[[j, 1]], bracketlist[[j, 2]],
bracketlist[[j + 1, 1]],
bracketlist[[j + 1, 2]], \[Tau]], {\[Tau], 0, 1, 1/(
15 (bracketlist[[j, 2]] - bracketlist[[j, 1]]))}] ], {j, 1, 8}];
ListAnimate[frames]
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
| This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication. | |
| The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
|