-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBisectionMethod.cpp
More file actions
51 lines (49 loc) · 1.08 KB
/
BisectionMethod.cpp
File metadata and controls
51 lines (49 loc) · 1.08 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
#include<iostream>
#include<cmath>
using namespace std;
class Bisection
{
public:
double error=0.00001;
int max_Iteration;
double c;
double func(double x)
{
return x*x-4*x-10;
}
void findRoot(double a, double b)
{
if(func(a)*func(b)>=0)
{
cout<<"Please Enter Correct Value of a & b :";
return;
}
max_Iteration=1;
while(abs(a-b)>=error && max_Iteration<=30)
{
c=((a+b)/2); //Find Middle Point
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;
max_Iteration++;
}
}
};
int main()
{
Bisection obj;
obj.findRoot(-2,-1);
return 0;
}