[go: up one dir, main page]

0% found this document useful (0 votes)
10 views4 pages

Bisection Method PDF

Uploaded by

Vanshika Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Bisection Method PDF

Uploaded by

Vanshika Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

(*Bisection 1*)

(*Function = x^3-5x+1
a=0
b=1
No. of iterations = 15
tolerance value = 0.0001*)
Print["f(x)= ", f[x_] = Input["Enter the function :"]];
Print["Graph of the function in the interval[-4,4]"]
Plot[f[x], {x, - 4, 4}]
a = Input["Enter the value of a:"];
b = Input["Enter the value of b:"];
If[f[a] * f[b] > 0, Print["The given interval does not contain the root"];
Exit[]];
ϵ = Input["Enter the prescribed tolerance error :"];
k = Input["Enter the maximum number of iterations :"];
Print["Graph of the function in the interval [", a, ", ", b, "]"]
Plot[f[x], {x, a, b}]
m = Na + b  2;
i = 1;
Print["Iteration Number= ", i, ", a= ", a, ", m= ", m, ", b= ", b, ", f(m)= ", f (m)]
Fori = 2, i ≤ k, i ++,

If[Abs[f[m]] < ϵ, Break[]],
If[f[a] * f[m] < 0, b = m, a = m], m = Na + b  2,
Print["Iteration Number= ", i, "a= ", a, "m= ", m, "b= ", b, "f(m)= ", f[m]]


If[i ⩵ k + 1 && Abs[f[m]] > ϵ, Print["Maximum no. of iterations has been exceeded"],
Print["Desired accuracy has been achieved"]];

Print["The root of the equation ", f[x], "=0 after ", i - 1, " iterations is :", N[m, 9]];
Print["The value of the function is: ", N[f[m], 9]]
f(x)= 1 - 5 x + x3

Graph of the function in the interval[-4,4]


20

15

10

-4 -2 2 4

-5

-10

-15

Graph of the function in the interval [0, 1]


2 Bisection method nb.nb

0.2 0.4 0.6 0.8 1.0

-1

-2

-3

Iteration Number= 1, a= 0, m= 0.5, b= 1, f(m)= 0.5 f

Iteration Number= 2a= 0m= 0.25b= 0.5f(m)= -0.234375

Iteration Number= 3a= 0m= 0.125b= 0.25f(m)= 0.376953

Iteration Number= 4a= 0.125m= 0.1875b= 0.25f(m)= 0.0690918

Iteration Number= 5a= 0.1875m= 0.21875b= 0.25f(m)= -0.0832825

Iteration Number= 6a= 0.1875m= 0.203125b= 0.21875f(m)= -0.00724411

Iteration Number= 7a= 0.1875m= 0.195313b= 0.203125f(m)= 0.0308881

Iteration Number= 8a= 0.195313m= 0.199219b= 0.203125f(m)= 0.0118129

Iteration Number= 9a= 0.199219m= 0.201172b= 0.203125f(m)= 0.00228208

Iteration Number= 10a= 0.201172m= 0.202148b= 0.203125f(m)= -0.0024816

Iteration Number= 11a= 0.201172m= 0.20166b= 0.202148f(m)= -0.0000999043

Desired accuracy has been achieved

The root of the equation 1 - 5 x + x3 =0 after 11 iterations is :0.20166

The value of the function is: -0.0000999043


Bisection method nb.nb 3

(*Bisection 2*)
Print["f(x)= ", f[x_] = Input["Enter the function :"]];
Print["Graph of the function in the interval[-4,4]"]
Plot[f[x], {x, - 4, 4}]
a = Input["Enter the value of a:"];
b = Input["Enter the value of b:"];
If[f[a] * f[b] > 0, Print["The given interval does not contain the root"];
Exit[]];
ϵ = Input["Enter the prescribed tolerance error :"];
k = Input["Enter the maximum number of iterations :"];
Print["Graph of the function in the interval [", a, ", ", b, "]"]
Plot[f[x], {x, a, b}]
m = Na + b  2;
m1 = 1;
i = 1;
Print["Iteration Number= ", i, ", a= ", a, ", m= ", m, ", b= ", b, ", f(m)= ", f[m]]
Fori = 2, i ≤ k, i ++,

If[Abs[m - m1] < ϵ, Break[]],
If[f[a] * f[m] < 0, b = m, a = m],
m1 = m,
m = Na + b  2,
Print["Iteration Number= ", i, ", a= ", a, ", m= ", m, ", b= ", b, ", f(m)= ", f[m]]


If[i ⩵ k + 1 && Abs[m - m1] > ϵ, Print["Maximum no. of iterations has been exceeded"],
Print["Desired accuracy has been achieved"]];

Print["The root of the equation ", f[x], "=0 after ", i - 1, " iterations is :", N[m, 9]];
Print["The value of the function is: ", N[f[m], 9]]
x Log[x]
f(x)= -1.2 +
Log[10]
Graph of the function in the interval[-4,4]

1.0

0.5

-4 -2 2 4

-0.5

-1.0

-1.5

Graph of the function in the interval [2, 3]


4 Bisection method nb.nb

0.2

2.2 2.4 2.6 2.8 3.0

-0.2

-0.4

-0.6

Iteration Number= 1, a= 2, m= 2.5, b= 3, f(m)= -0.20515

Iteration Number= 2, a= 2.5, m= 2.75, b= 3, f(m)= 0.00816491

Iteration Number= 3, a= 2.5, m= 2.625, b= 2.75, f(m)= -0.0997856

Iteration Number= 4, a= 2.625, m= 2.6875, b= 2.75, f(m)= -0.046126

Iteration Number= 5, a= 2.6875, m= 2.71875, b= 2.75, f(m)= -0.0190585

Iteration Number= 6, a= 2.71875, m= 2.73438, b= 2.75, f(m)= -0.0054662

Iteration Number= 7, a= 2.73438, m= 2.74219, b= 2.75, f(m)= 0.00134452

Iteration Number= 8, a= 2.73438, m= 2.73828, b= 2.74219, f(m)= -0.00206205

Iteration Number= 9, a= 2.73828, m= 2.74023, b= 2.74219, f(m)= -0.000359068

Iteration Number= 10, a= 2.74023, m= 2.74121, b= 2.74219, f(m)= 0.00049265

Iteration Number= 11, a= 2.74023, m= 2.74072, b= 2.74121, f(m)= 0.0000667723

Iteration Number= 12, a= 2.74023, m= 2.74048, b= 2.74072, f(m)= -0.000146153

Iteration Number= 13, a= 2.74048, m= 2.7406, b= 2.74072, f(m)= -0.0000396913

Iteration Number= 14, a= 2.7406, m= 2.74066, b= 2.74072, f(m)= 0.0000135402

Desired accuracy has been achieved


x Log[x]
The root of the equation -1.2 + =0 after 14 iterations is :2.74066
Log[10]
The value of the function is: 0.0000135402

You might also like