-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFalsePosition.cpp
61 lines (49 loc) · 1.11 KB
/
FalsePosition.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
57
58
59
60
61
#include <iostream>
#include <iomanip>
#include <math.h>
#define f(x) x*x-x-2
using namespace std;
int main()
{
float x0, x1, x, f0, f1, f, e;
int step = 1;
cout << setprecision(6) << fixed;
up:
cout << "Enter first guess: ";
cin >> x0;
cout << "Enter second guess: ";
cin >> x1;
cout << "Enter tolerable error: ";
cin >> e;
f0 = f(x0);
f1 = f(x1);
if (f0 * f1 > 0.0)
{
cout << "Incorrect Initial Guesses." << endl;
goto up;
}
cout << endl
<< "*********************" << endl;
cout << "False Position Method" << endl;
cout << "*********************" << endl;
do
{
x = x0 - (x0 - x1) * f0 / (f0 - f1);
f = f(x);
cout << "Iteration-" << step << ":\t x = " << setw(10) << x << " and f(x) = " << setw(10) << f(x) << endl;
if (f0 * f < 0)
{
x1 = x;
f1 = f;
}
else
{
x0 = x;
f0 = f;
}
step = step + 1;
} while (fabs(f) > e);
cout << endl
<< "Root is: " << x << endl;
return 0;
}