-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBisection.cpp
More file actions
106 lines (105 loc) · 2.55 KB
/
Bisection.cpp
File metadata and controls
106 lines (105 loc) · 2.55 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
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
98
99
100
101
102
103
104
105
106
#include<iostream>
#include<cmath>//Bissection Method
using namespace std;
double intervalChecker(int *coeff,double a,int deg)
{
double po,temp=deg,final=0;
for (int i = 0; i < deg+1; i++)
{
po = pow(a, temp);
po = coeff[i] * po;
final += po;
temp--;
}
return final;
}
void display(int* coeff_arr, int deg)
{
int temp;
cout << endl << " <--------------------------------------Equation--------------------------------->"<<endl;
temp = deg;
cout << " ";
for (int i = 0; i < deg + 1; i++)
{
cout << "(" << coeff_arr[i] << ")" << "x^" << temp;
if (i < deg)
{
cout << "+";
}
temp--;
}
}
int main()
{
int * coeff_arr,accu;
int deg;
double a,b,fun1,fun2,avg, temp1,iter;
cout << "Enter Highest Degree of x:";
cin >> deg;
int temp = deg;
coeff_arr = new int[deg + 1];
for (int i = 0; i < deg+1; i++)
{
cout << "Enter Coefficient of x^"<<temp<<":";
cin >> coeff_arr[i];
temp--;
}
system("cls");
display(coeff_arr, deg);
cout << endl;
do {
cout << "Enter Interval value a:";
cin >> a;
cout << "Enter Interval value b:";
cin >> b;
fun1=intervalChecker(coeff_arr, a, deg);
fun2= intervalChecker(coeff_arr, b, deg);
cout << "Interval value: [" << a << "," << b << "]" << endl;
cout << "f(a):" << fun1 << endl;
cout << "f(b):" << fun2 << endl;
} while (fun1 < 0 && fun2 < 0 || fun1>0 && fun2>0);
cout << "Enter Accuracy value in terms of 10^:";
cin >> accu;
iter = b - a;
iter = (accu - log10(iter)) / log10(2);
iter = iter + 1;
iter = iter * -1;
for (int i = 1; i <iter; i++)
{
avg = (a + b) / 2;
temp1 = intervalChecker(coeff_arr, avg, deg);
if (temp1*fun1<0)
{
if (avg < a)
{
b = a;
a = avg;
cout << "-----------------------------------------------------------------------------------" << endl;
cout << "Interval value: [" << a << "," << b << "]"<<endl;
}
else
{
b = avg;
cout << "-----------------------------------------------------------------------------------" << endl;
cout << "Interval value: [" << a << "," << b << "]" << endl;
}
}
else
{
if (avg < b)
{
a = avg;
cout << "-----------------------------------------------------------------------------------" << endl;
cout << "Interval value: [" << a << "," << b << "]" << endl;
}
else
{
b = avg;
cout << "-----------------------------------------------------------------------------------" << endl;
cout << "Interval value: [" << a << "," << b << "]" << endl;
}
}
cout << "Function value =" << temp1 << endl;
}
return 0;
}