-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (43 loc) · 1004 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <math.h>
#include "main.h"
using std::cin;
using std::cout;
int main() {
double a0, b0;
cout << "Enter a0: ";
cin >> a0;
cout << "Enter b0: ";
cin >> b0;
bisection(a0, b0);
return 0;
}
double equation(double x) {
return tan(x) - x;
}
void bisection(double a0, double b0) {
int i = 1;
double fa, fb, mid = 0.0, fval;
cout << "Iteration #" << i << ": a0 = " << a0 << "\tb0 = " << b0 << "\n";
i++;
fa = equation(a0);
fb = equation(b0);
if (fa*fb > 0) {
cout << "The roots donot exist in this interval.\n";
return;
}
if (fa > 0 && fb < 0) { fval = a0; a0 = b0; b0 = fval; }
while (modulus(b0-a0) > pow(10, -10)) {
mid = (a0 + b0) / 2.0;
fval = equation(mid);
if (fval < 0) a0 = mid;
else b0 = mid;
cout << "Iteration #" << i << ": a0 = " << a0 << "\tb0 = " << b0 << "\n";
i++;
}
cout << "Final Result = " << mid << "\n";
}
inline double modulus(double x) {
if (x < 0) x = -x;
return x;
}