Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

PRACTICAL 2

BISECTION METHOD
(1.1) BISECTION METHOD WITH NUMBER OF ITERATIONS AS STOPPING
CRITERIA -

QUES1. (1) PERFORM 10 ITERATIONS OF THE BISECTION METHOD TO OBTAIN A REAL ROOT OF THE FOLLOW-
ING IN THE INTERVAL (0, 1)

f HxL = x3 - 5 x + 1 = 0

SOLUTION : -

BisectionMethod @a0_, b0_, n_, f_D :=


Module@8<, a = N@a0D; b = N@b0D; m = Ha + bL  2;
i = 0;
If@f@aD * f@bD ³ 0,
Print@"We can not continue with Bisection method as ", f, "HaL.", f, "HbL>=0."D;
Return@DD;

OutputDetails = 88i, a, m, b, f@mD<<;

While@i < n,
If@Sign@f@aDD Š Sign@f@mDD, a = m, b = mD;
m = Ha + bL  2;
i ++;
OutputDetails = Append@OutputDetails , 8i, a, m, b, f@mD<D;D;

Print@NumberForm@
TableForm@OutputDetails , TableHeadings ® 8None, 8"i", "ai ", "mi ", "bi ", "f@mi D"<<D, 8DD;

Print@"Root after ", n, "iterations", NumberForm@m, 8DD;


Print@"Accuracy=", Abs@Hb - aL  2DD;
Print@"Function value at approximated root,f@mD = ", NumberForm@f@mD, 8DD;D;

f@x_D := x3 - 5 x + 1;
BisectionMethod @0, 1, 10, fD
2 NA PRACTICAL 2 BISECTION METHOD.nb

i ai mi bi f@mi D
0 0. 0.5 1. -1.375
1 0. 0.25 0.5 -0.234375
2 0. 0.125 0.25 0.37695313
3 0.125 0.1875 0.25 0.069091797
4 0.1875 0.21875 0.25 -0.083282471
5 0.1875 0.203125 0.21875 -0.0072441101
6 0.1875 0.1953125 0.203125 0.030888081
7 0.1953125 0.19921875 0.203125 0.011812866
8 0.19921875 0.20117188 0.203125 0.0022820756
9 0.20117188 0.20214844 0.203125 -0.0024815956
10 0.20117188 0.20166016 0.20214844 -0.000099904253
Root after 10iterations0.20166016

Accuracy=0.000488281

Function value at approximated root,f@mD = -0.000099904253

Plot@f@xD, 8x, - 3, 4<, PlotStyle ® 8Thickness@0.01D<D

15

10

-3 -2 -1 1 2 3 4

-5

-10

OBTAIN REAL ROOT OF THE FOLOWING EQUATION IN THE INTERVAL H0, 1L


QUES2.PERFORM 10 ITERATIONS OF THE BISECTION METHOD TO

f HxL = Cos HxL - xex = 0.

SOLUTION : -
NA PRACTICAL 2 BISECTION METHOD.nb 3

In[1]:= BisectionMethod @a0_, b0_, n_, f_D :=


Module@8<,
a = N@a0D; b = N@b0D; m = Ha + bL  2;
i = 0;
If@f@aD * f@bD >= 0, Print@
"We cannot continue with Bisection method as ", f, "HaL.", f, "HbL >= 0"D; Return@DD;

OutputDetails = 88i, a, m, b, f@mD<<;

While@i < n,
If@Sign@f@aDD Š Sign@f@mDD, a = m, b = mD;
m = Ha + bL  2; i ++; OutputDetails = Append@OutputDetails , 8i, a, m, b, f@mD<D;D;

Print@NumberForm@
TableForm@OutputDetails , TableHeadings ® 8None, 8"i", "ai ", "mi ", "bi ", "f@mi D"<<D, 5DD;

Print@"Root after ", n, " iterations ", NumberForm@m, 5DD;


Print@"Accuracy = ", Abs@Hb - aL  2DD;
Print@"Function value at approximated root,f@mD = ", NumberForm@f@mD, 5DD;D;

g@x_D := Cos@xD - x Exp@xD;


BisectionMethod @0, 1, 10, gD
i ai mi bi f@mi D
0 0. 0.5 1. 0.053222
1 0.5 0.75 1. -0.85606
2 0.5 0.625 0.75 -0.35669
3 0.5 0.5625 0.625 -0.14129
4 0.5 0.53125 0.5625 -0.041512
5 0.5 0.51563 0.53125 0.0064753
6 0.51563 0.52344 0.53125 -0.017362
7 0.51563 0.51953 0.52344 -0.0054044
8 0.51563 0.51758 0.51953 0.00054518
9 0.51758 0.51855 0.51953 -0.0024272
10 0.51758 0.51807 0.51855 -0.00094039
Root after 10 iterations 0.51807

Accuracy = 0.000488281

Function value at approximated root,f@mD = -0.00094039


4 NA PRACTICAL 2 BISECTION METHOD.nb

In[4]:= Plot@g@xD, 8x, - 1, 1<, PlotStyle ® 8Thickness@0.01D<D

1.0

0.5

-1.0 -0.5 0.5 1.0

Out[4]= -0.5

-1.0

-1.5

-2.0

a real root of the following equaition in the interval H2, 3L


QUES3. PERFORM 12 ITERATIONS OF THE BISECTION METHOD TO obtain

f HxL = x4 - 3 x2 + x - 10 = 0.
SOLUTION : -
In[5]:= BisectionMethod @a0_, b0_, n_, f_D :=
Module@8<,
a = N@a0D; b = N@b0D; m = Ha + bL  2;
i = 0;
If@f@aD * f@bD >= 0, Print@
"We cannot continue with Bisection method as ", f, "HaL.", f, "HbL >= 0"D; Return@DD;

OutputDetails = 88i, a, m, b, f@mD<<;

While@i < n,
If@Sign@f@aDD Š Sign@f@mDD, a = m, b = mD;
m = Ha + bL  2; i ++; OutputDetails = Append@OutputDetails , 8i, a, m, b, f@mD<D;D;

Print@NumberForm@
TableForm@OutputDetails , TableHeadings ® 8None, 8"i", "ai ", "mi ", "bi ", "f@mi D"<<D, 8DD;

Print@"Root after ", n, " iterations ", NumberForm@m, 8DD;D;

h@x_D = x ^ 4 - 3 x ^ 2 + x - 10;
BisectionMethod @2, 3, 12, hD
NA PRACTICAL 2 BISECTION METHOD.nb 5

i ai mi bi f@mi D
0 2. 2.5 3. 12.8125
1 2. 2.25 2.5 2.6914063
2 2. 2.125 2.25 -1.0310059
3 2.125 2.1875 2.25 0.72975159
4 2.125 2.15625 2.1875 -0.17494106
5 2.15625 2.171875 2.1875 0.2712279
6 2.15625 2.1640625 2.171875 0.046611492
7 2.15625 2.1601563 2.1640625 -0.06454622
8 2.1601563 2.1621094 2.1640625 -0.0090629158
9 2.1621094 2.1630859 2.1640625 0.018750376
10 2.1621094 2.1625977 2.1630859 0.004837755
11 2.1621094 2.1623535 2.1625977 -0.0021140738
12 2.1623535 2.1624756 2.1625977 0.0013614672
Root after 12 iterations 2.1624756

In[8]:= Plot@h@xD, 8x, 0, 3<, PlotStyle ® 8Thickness@0.01D<D

40

30

20
Out[8]=

10

0.5 1.0 1.5 2.0 2.5 3.0

-10

(1.2) Bisection Method with error tolerance as stopping criteria - :

QUES4. Find the root of the equation f HxL =


x3 - 5 x + 1 = 0 in the interval H0, 1L
by Bisection Method with absolute error tolerance 10-2 .
Solution - :
6 NA PRACTICAL 2 BISECTION METHOD.nb

In[9]:= BisectionMethodErrorT @a0_, b0_, error_, f_D :=


Module@8<,
a = N@a0D; b = N@b0D; m = Ha + bL  2;
If@f@aD * f@bD ³ 0,
Print@"We can not continue with Bisection Method as function values are
not opposite sign at end points of the interval"D; Return@DD;
i = 0;
OutputDetails = 88i, a, m, b, Hb - aL  2<<;
While@Abs@b - aD > 2 * error,
If@Sign@f@aDD Š Sign@f@mDD, a = m, b = mD;
m = Ha + bL  2;
i ++;
OutputDetails = Append@OutputDetails , 8i, a, m, b, Hb - aL  2<D;D;
Print@NumberForm@
TableForm@OutputDetails , TableHeadings ® 8None, 8"i", "ai ", "mi ", "bi ", "f@mi D"<<D, 8DD;
Print@"Number of iterations required to achieve desired accuracy = ", iD;
Print@"Root after ", i, " iterations ", NumberForm@m, 8DD;
Print@"Function value at approximated root,f@mD = ", NumberForm@f@mD, 8DD;D;

g@x_D := x ^ 3 - 5 x + 1;
error = 10 ^ - 2;
a = 0; b = 1;
BisectionMethodErrorT @a, b, error, gD
i ai mi bi f@mi D
0 0. 0.5 1. 0.5
1 0. 0.25 0.5 0.25
2 0. 0.125 0.25 0.125
3 0.125 0.1875 0.25 0.0625
4 0.1875 0.21875 0.25 0.03125
5 0.1875 0.203125 0.21875 0.015625
6 0.1875 0.1953125 0.203125 0.0078125
Number of iterations required to achieve desired accuracy = 6

Root after 6 iterations 0.1953125

Function value at approximated root,f@mD = 0.030888081


NA PRACTICAL 2 BISECTION METHOD.nb 7

In[14]:= Plot@g@xD, 8x, - 3, 2<, PlotStyle ® 8Thickness@0.01D<D

-3 -2 -1 1 2

Out[14]=

-5

-10

QUES5.Find the root of the equation f HxL =


x3 - 5 x + 1 = 0 in the interval H0, 1L
by Bisection Method with absolute error tolerance 10-4 .
Solution - :
In[15]:= g@x_D := x ^ 3 - 5 x + 1;
error = 10 ^ - 4;
a = 0; b = 1;
BisectionMethodErrorT @a, b, error, gD
i ai mi bi f@mi D
0 0. 0.5 1. 0.5
1 0. 0.25 0.5 0.25
2 0. 0.125 0.25 0.125
3 0.125 0.1875 0.25 0.0625
4 0.1875 0.21875 0.25 0.03125
5 0.1875 0.203125 0.21875 0.015625
6 0.1875 0.1953125 0.203125 0.0078125
7 0.1953125 0.19921875 0.203125 0.00390625
8 0.19921875 0.20117188 0.203125 0.001953125
9 0.20117188 0.20214844 0.203125 0.0009765625
10 0.20117188 0.20166016 0.20214844 0.00048828125
11 0.20117188 0.20141602 0.20166016 0.00024414063
12 0.20141602 0.20153809 0.20166016 0.00012207031
13 0.20153809 0.20159912 0.20166016 0.000061035156
Number of iterations required to achieve desired accuracy = 13

Root after 13 iterations 0.20159912

Function value at approximated root,f@mD = 0.00019782746

QUES 6. Find the root of the equation f HxL =


x3 + 2 x2 - 3 x - 1 = 0 in the interval H1, 2L
by Bisection Method with absolute error tolerance 10-5 .
Solution - :
8 NA PRACTICAL 2 BISECTION METHOD.nb

In[19]:= BisectionMethodErrorT @a0_, b0_, error_, f_D :=


Module@8<,
a = N@a0D; b = N@b0D; m = Ha + bL  2;
If@f@aD * f@bD ³ 0,
Print@"We can not continue with Bisection Method as function values are
not opposite sign at end points of the interval"D; Return@DD;
i = 0;
OutputDetails = 88i, a, m, b, Hb - aL  2<<;
While@Abs@b - aD > 2 * error,
If@Sign@f@aDD Š Sign@f@mDD, a = m, b = mD;
m = Ha + bL  2;
i ++;
OutputDetails = Append@OutputDetails , 8i, a, m, b, Hb - aL  2<D;D;
Print@NumberForm@
TableForm@OutputDetails , TableHeadings ® 8None, 8"i", "ai ", "mi ", "bi ", "f@mi D"<<D, 8DD;
Print@"Number of iterations required to achieve desired accuracy = ", iD;
Print@"Root after ", i, " iterations ", NumberForm@m, 8DD;D;

f@x_D := x ^ 3 + 2 x ^ 2 - 3 x - 1;
error = 10 ^ - 5;
a = 1; b = 2;
BisectionMethodErrorT @a, b, error, fD
i ai mi bi f@mi D
0 1. 1.5 2. 0.5
1 1. 1.25 1.5 0.25
2 1. 1.125 1.25 0.125
3 1.125 1.1875 1.25 0.0625
4 1.1875 1.21875 1.25 0.03125
5 1.1875 1.203125 1.21875 0.015625
6 1.1875 1.1953125 1.203125 0.0078125
7 1.1953125 1.1992188 1.203125 0.00390625
8 1.1953125 1.1972656 1.1992188 0.001953125
9 1.1972656 1.1982422 1.1992188 0.0009765625
10 1.1982422 1.1987305 1.1992188 0.00048828125
11 1.1982422 1.1984863 1.1987305 0.00024414063
12 1.1984863 1.1986084 1.1987305 0.00012207031
13 1.1986084 1.1986694 1.1987305 0.000061035156
14 1.1986694 1.1987 1.1987305 0.000030517578
15 1.1986694 1.1986847 1.1987 0.000015258789
16 1.1986847 1.1986923 1.1987 7.6293945 ´ 10-6
Number of iterations required to achieve desired accuracy = 16

Root after 16 iterations 1.1986923


NA PRACTICAL 2 BISECTION METHOD.nb 9

In[24]:= Plot@f@xD, 8x, - 3, 2<, PlotStyle ® 8Thickness@0.01D<D

4
Out[24]=

-3 -2 -1 1 2

-2

You might also like