-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFalse Position.cpp
More file actions
163 lines (129 loc) · 3.45 KB
/
False Position.cpp
File metadata and controls
163 lines (129 loc) · 3.45 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <cmath>
using namespace std;
int degree;
double coff[1000];
double function( double x){
double fx = 0;
for (int i = 0; i <= degree; i++){
fx += coff[i] * pow(x,i);
}
return fx;
}
int main(){
char choice;
int I;
int i;
bool cond;
double e;
double a;
double b;
double fa;
double fb;
double c;
double oc;
double fc;
char again;
char keepf;
cout << "<------THIS PROGRAM CALCULATE THE ROOT FOR ANY POLYNOMIAL FUNCTION USING BISECTION METHOD------>" << endl;
do{
//Creating a function
cout << "Enter the degree of the function: ";
cin >> degree;
for (int i = 0; i <= degree; i++){
cout << "Enter a coffetient value for x^" << i << ": ";
cin >> coff[i];
}
do{
cond = false;
i = 1;
cout << endl << "for Iterations Enter 'I'" << endl << "for Error Enter 'E' " << endl;
cin >> choice;
if(choice == 'I' || choice == 'i'){
cout << "Enter how many Iterations would you like to do: ";
cin >> I;
cout << "Enter a value for a: ";
cin >> a;
cout << "Enter a value for b: ";
cin >> b;
for (int i = 1; i <= I; i++){
fa = function(a);
fb = function(b);
c = b - ( ( fb * (b-a) ) / (fb - fa) );
fc = function(c);
cout << endl << "Iteration " << i << ": " << endl;
cout
<< "a = " << a << endl
<< "b = " << b << endl
<< "c = " << c << endl
<< "f(a) = " << fa << endl
<< "f(b) = " << fb << endl
<< "f(c) = " << fc << endl;
if( fc * fa < 0){
b = c;
}
else{
a = c;
}
}
}
else if (choice == 'E' || choice == 'e'){
cout << "Enter your prefered error: (the smaller the more accurate)" << endl;
cin >> e;
cout << "Enter a value for a: ";
cin >> a;
cout << "Enter a value for b: ";
cin >> b;
while (cond == false){
oc = c;
fa = function(a);
fb = function (b);
c = b - ( ( fb * (b-a) ) / (fb - fa) );
fc = function (c);
cout << endl << "Iteration " << i << ": " << endl;
cout
<< "a = " << a << endl
<< "b = " << b << endl
<< "c = " << c << endl
<< "f(a) = " << fa << endl
<< "f(b) = " << fb << endl
<< "f(c) = " << fc << endl;
if( abs(fc) < e ){
cond = true;
cout << "|f(c)| is less than error <-------------" << endl;
}
else{
cout << "No" << endl;
}
if( i > 1 && abs( oc - c) < e ){
cond =true;
cout << "|C - C-node| is less than error <-------------" << endl;
}
else{
cout << "No" << endl;
}
if (cond == false){
i ++;
if( fc * fa < 0){
b = c;
}
else{
a = c;
}
}
}
cout << endl << "FINAL ANSWER: The root is ~= " << c << endl;
}
else{
cout << "Please Enter a valid input";
}
cout << endl << "Would you like to start again? (Y/N) " << endl;
cin >> again;
if (again != 'Y' && again != 'y'){
return 0;
}
cout << "Keep the function? " << endl;
cin >> keepf;
} while (keepf == 'Y' || keepf == 'y');
} while(again == 'Y' || again == 'y');
}