-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (43 loc) · 1.07 KB
/
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;
falsi(a0, b0);
return 0;
}
double equation(double x) {
return x*sin(x) - 1;
}
void falsi(double a0, double b0) {
int i = 1;
double fa0, fb0, c0, fc0;
fa0 = equation(a0);
fb0 = equation(b0);
if (fa0*fb0 > 0) { cout << "\n" << "Cant find any root in given range." << "\n"; return; }
if (fb0 < 0) { c0 = a0; a0 = b0; b0 = c0; }
cout << "Iteration #" << i << ": a0 = " << a0 << "\t, b0 = " << b0 << "\n";
i++;
do {
fa0 = equation(a0);
fb0 = equation(b0);
c0 = (a0*fb0 - b0*fa0) / (fb0 - fa0);
fc0 = equation(c0);
if (modulus(fc0) < pow(10, -10)) break;
if (fc0 < 0) a0 = c0;
else b0 = c0;
cout << "Iteration #" << i << ": a0 = " << a0 << "\t, b0 = " << b0 << "\n";
i++;
} while (modulus(a0-b0) > pow(10, -10));
cout << "Final Result = " << c0 << "\n";
}
inline double modulus(double x) {
if (x < 0) x = -x;
return x;
}