Finding root by bisection method in Mathematica

The bisection method in mathematics is a root-finding method that repeatedly bisects an interval and then selects a subinterval in which a root must lie for further processing. It is a very simple and robust method, but it is also relatively slow.


Let us do it on mathematica. The program code is here.

Clear["`*"];
f[x_] := x^3 + 3x + 1;
Plot[f[x],{x,-5,5}];
a = Input["Enter a"];
b = Input["Enter b"];
tol = Input["Enter tolerance"];
n = Input["Enter total iteration"];
If[f[a]*f[b]>0, {Print["No solution exists"] Exit[]}];
Print["     n       a            b           c         f(c)"];
Do[
{ c = (a+b)/2; 
If[f[a]*f[c]>0, a = c, b = c],
Print[PaddedForm[i,5],
PaddedForm[N[a],{10,6}],
PaddedForm[N[b],{10,6}],
PaddedForm[N[c],{10,6}],
PaddedForm[N[f[c]],{10,6}]]
If[Abs[a-b]<tol, {Print["The solution is: ", c] Exit[]}]},
{i,1,n}];
Print["The maximum iteration failed"];


When you run it you will see this image.

We see here the root lies between -2 and 0. So you will input
a = -2
b = 0
tol = 0.001
n = 25

Hence the methods are done. Thank you for Reading.

Related Posts
Previous
« Prev Post