-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFalsePositionMethod.cpp
More file actions
53 lines (48 loc) · 1.12 KB
/
FalsePositionMethod.cpp
File metadata and controls
53 lines (48 loc) · 1.12 KB
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
//Regular Falsi Method
#include<iostream>
using namespace std;
class FalsePosition
{
public:
int max_Iteration=10;
double c;
double func(double x)
{
return x*x-4*x-10;
}
// Prints root of func(x) in interval [a, b]
void findRoot(double a, double b)
{
if(func(a)*func(b)>=0)
{
cout<<"Please Enter Correct Value of a & b :";
return;
}
for(int i=0;i<max_Iteration;i++)
{
// Find the point that touches x axis
c = (a*func(b) - b*func(a))/ (func(b) - func(a));
if(func(c)==0.0) //Check if middle point is Root
{
break;
}
if(func(a)*func(c)<0)
{
b=c;
a=a;
}
else
{
a=c;
b=b;
}
cout<<"Iteration"<<max_Iteration<<"a:"<<a<<" b:"<<b<<" root:"<<c<<" f(c):"<<" "<<func(c)<<endl;
}
}
};
int main()
{
FalsePosition obj;
obj.findRoot(-2,-1);
return 0;
}