-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBisectionMethod.cpp
97 lines (72 loc) · 1.47 KB
/
BisectionMethod.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
float quad(float a,float b,float c,float x);
float absu(float f);
int main()
{
float a,b,c,result;
cout<<"Enter the coeff of quadratic equations: a,b,c respectively:\n";
cin>>a>>b>>c;
float left,right,m;
int accuracy;
cout<<"Enter accuracy\n";
cin>>accuracy;
float arr=pow(10,-accuracy);
cout<<"\n"<<arr;
cout<<"Enter left and right limit respectively:\n";
cin>>left>>right;
if(quad(a, b, c,left)>quad( a, b, c,right))
{
float temp;
temp=left;
left=right;
right=temp;
}
float temp;
int t=100;
do
{
m=(left+right)/2;
result=quad(a,b,c,m);
// cout<<" M is:"<<m<<"\n";
if(result>0)
{
right=m;
}
else
left=m;
temp=quad(a,b,c,(left+right)/2);
//cout<<"Temp is:" <<temp;
}while(absu(result-temp)>arr);
cout<<"Answer is :"<<m;
return 0;
}
float quad(float a,float b,float c,float x)
{
return (a*x*x + b*x +c);
}
float absu(float f)
{
if(f>0)
{
return f;
}
else
{
return -f;
}
}
/*float compute(float left,float right,float a,float b,float c,float x)
{
float m=(left+right)/2
float result=quad(a,b,c,x);
if(result>0)
{
right=m;
}
else
left=m;
return m;
}*/