# How to find roots using the bisection method [duplicate]

First I plot the function and then I try to find a domain such that I can see the curve cut through the x-axis. I do this via trial and error

f[x_] := x^6 - x - 1;
Plot[f[x], {x, -1, 1}]


this is my code. How can I improve it?

a = -1;
b = 0; (*define the initial domain [a,b] *)

(*tolerence*)
epsilon = 0.001;

(*initialistation*)
count = 0;
countvalve = 100;
c = 0.5*(a + b);

While[Abs[f[c]] > epsilon,
count = count + 1;
c = 0.5*(a + b);
If[f[c]*f[a] < 0, b = c, a = c];
If[count > countvalve, Break[]];
Print[{count, c, f[c], epsilon}];];
Print["While loop exited"];

Print[{count, c, f[c], epsilon}];
Plot[f[x], {x, c - 0.1, c + 0.1}]


this is the output

{1,-0.5,-0.484375,0.001}
{2,-0.75,-0.0720215,0.001}
{3,-0.875,0.323795,0.001}
{4,-0.8125,0.1002,0.001}
{5,-0.78125,0.00862368,0.001}
{6,-0.765625,-0.0329578,0.001}
{7,-0.773438,-0.0124947,0.001}
{8,-0.777344,-0.00201909,0.001}
{9,-0.779297,0.00328119,0.001}
{10,-0.77832,0.000625803,0.001}
While loop exited
{10,-0.77832,0.000625803,0.001}


Suppose the function contains two roots. how do I find the other one?

but this is the real question,

How do I write a code to implement bisection method so that, given any continuous function $f(x)$, the code can

1. Count the number of roots in a domain [a,b]

2. Evaluate each of these roots one by one in sequence.

where epsilon = 0.001

these are sample functions.

f[x] = Exp[x] - x - 2; (* for all x *)
f[x] = x^3 + (2*x)^2 - 3*x - 1; (*for all x *)
f[x] = (1/x)Sin[x]; (* for -3 π <= x <= 3 π *)
f[x] = Tan[π*x] -x - 6; (* for -3 π <= x <= 3 π *)


The code should be able to find all the roots in all the functions automatically and without manual intervention.

• How about: FindRoot[f[x], {x, 0.1}]. Then scan through many different starting values. Apr 28 '16 at 1:01
• Check this answer here Apr 28 '16 at 1:25
• NSolve[f[x] == 0 && a <= x <= b, x]? -- Are you required to use the bisection method? You'll need another algorithm to isolate the roots. I take it this is a homework assignment, because the only other reason I can think of trying this way is for fun. Apr 28 '16 at 11:37

A methode i like to use and a starter. Please see Bisection method and Bisection,

in particular:

Let $a_n$ and $b_n$ be the endpoints at the nth iteration (with $a_1=a$ and $b_1=b$) and let $r_n$ be the nth approximate solution. Then the number of iterations required to obtain an error smaller than epsilon is found by noting that $b_n-a_n=(b-a)/(2^{(n-1)})$

f[x_] := x^6 - x - 1
Plot[f[x], {x, -\[Pi]/3, \[Pi]/2}]


NSolve[Evaluate@f[x] == 0, x]


{{x->-0.77809},{x->-0.629372-0.735756 I},{x->-0.629372+0.735756 I},{x->0.451055 -1.00236 I},{x->0.451055 +1.00236 I},{x->1.13472}}

FindRoot[f[x], {x, 0}]


{x->-0.77809}

a[1] = 0;
b[1] = -1;

Do[p[n] = N[1/2 (a[n] + b[n])];
If[N[f[b[n]] f[p[n]]] < 0, a[n + 1] = p[n]; b[n + 1] = b[n],
a[n + 1] = a[n]; b[n + 1] = p[n]], {n, 1, 20}]

TableForm[Table[{a[n], b[n], p[n], N[f[a[n]]], N[f[b[n]]],
N[f[p[n]]]}, {n, 1, 20}]]