-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01.BisectionMethod.cpp
More file actions
53 lines (50 loc) · 1.16 KB
/
01.BisectionMethod.cpp
File metadata and controls
53 lines (50 loc) · 1.16 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
#include<bits/stdc++.h>
using namespace std;
class Bisection
{
public:
double error=0.00001;
int max_Iteration;
double c;
double func(double x)
{
return 3*x - cos(x) -1;
}
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<< fixed << setprecision(4);
cout<<"Iteration "<<max_Iteration<<"\ta:"<<a<<"\tb:"<<b<<"\troot:"<<c<<"\tf(c):"<<" "<<func(c)<<endl;
max_Iteration++;
}
cout<<endl<<"Root is : "<<c;
}
};
int main()
{
Bisection obj;
obj.findRoot(0,1);
return 0;
}