Bisection methods
Bisection methods
-)
I t, a
•
q1ven
24
25 e = 0.000 1;
26 i = 0;
27
28 cout << " Please Enter The
Value Of a:";
29 cin » a;
30
31 cout << " Please Enter The
Value Of b: ";
32 cin » b;
33
34 cout << " Please Enter The
Value Of c: ";
35 cin » c;
36
37 cout << " Please Enter The
Value Of d: ";
38 cin » d;
39
40 cout << " Please Enter The
Value Of The Initial Guess At
The Root:"
41 cin » x0;
42
43 h = f(a, b, c, d, x0) / df(a, b, c,
d, x0);
44
45 while (fabs(h) >= e)
46 {
47 x 1 = x0 - h;
48 h = f( a, b, C, d, x 1) / df(a, b,
C, d, x 1);
49 x0= x l;
so i++;
51 }
S2 cout << " The Root Of The
Equation Is : " << x0 << "\n
And The No. Of Iterations Is:"
<< i;
53 }
P1eose Enter The Vo1ue Of o :
1
P1eose Enter The Vo1ue Of b :
1
P1eose Enter The Vo1ue Of c :
3
P1eose Enter The Vo1ue Of d :
4
P1eose Enter The Vo1ue Of The
In itio 1Guess Rt The Root : 2
The Root Of The Equotion Is :
-1.22249
find The No. Of Iterotions Is
:8
[ProgrofY1 finished] I
1 include <iostream >
2 #include <conio.h>
3 #include <math.h >
4 #define f(x) (x - cos(x))
5 using namespace std;
6 int main()
7 {
8 float x 1, x2, xm, e, i = 0, fx 1,
fx2;
9 cout << "\nFor the function
x-cosx\n";
10 cout << "\nEnter accuracy
required : ";
11 cin >> e;
12 // Condition : x2 >X 1
13 cout << "Enter value of x 1 :
14 cin » x 1;
15 cout << "Enter value of x2 :
16 cin » x2;
17 fx 1 = f(x 1);
18 fx 2 = f(x2);
19 while (fx 1 * fx2 > 0)
20 {
21 cout << "\n Tr4 Again with
better guess\n";
22 cout << "\nEnter value of
X 1 : ";
23 cin >> x 1;
24 cout « "Enter value of
x2: ";
25 cin >> x2;
26 fx 1 = f(x 1);
27 fx2 = f(x2);
28 }
29 cout << "Value of f(x) at x 1
" << fx 1 << "\n";
30 cout << "Value at f(x) at x2
" << fx2 << "\n";
31 while (x2 - x 1 > e)
32 {
33 xm = (x 1 + x2) / 2;
34 if (f(xm) == 0)
35 {
36 break;
37 }
38 else if (f(xm) < 0)
39 {
40 X 1 = Xr<l;
41 }
42 else
43 {
44 X2 = Xr<l;
45 }
46 i+= l;
47 }
48 cout « "Root is->" « xm;
49 cout << "\nNumber of
iterations : " << i;
so }
For the function x-cosx