-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFalsePosition.cpp
57 lines (45 loc) · 1.07 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
#include<bits/stdc++.h>
using namespace std;
double func(double m)
{
return 4*exp(-m)*sin(m)-1;
}
void FalsePosition(double a, double b, double error)
{
if (func(a) * func(b) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}
double x = a;
int iteration = 1;
while(func(a)>error || func(b)>error){
cout<< fixed
<< " Iteration=" << iteration
<< "\ta=" << a << "\tb=" << b;
x = (a*func(b) - b*func(a)) / (func(b) - func(a));
// xn+1 = a*f(b) - b*f(a) / {f(b) - f(a)}
if (func(x)==0)
{
break;
}
else if(func(x)*func(a) < 0)
{
b = x;
}
else
{
a = x;
}
cout << "\tx" << iteration << "=" << x;
cout << "\tf(x" << iteration << ")=" << func(b) << endl << endl;
iteration++;
}
cout << endl << endl << " The root is " << setprecision(3) << x << endl;
}
int main()
{
double a = 0, b = 0.5, error = 0.0001;
FalsePosition(a, b, error);
return 0;
}