-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoots.c
400 lines (363 loc) · 11.4 KB
/
Roots.c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <stdio.h> // Standard input-output header
#include <math.h> // Math equations
//#include <string.h> To manipulate Strings, only necessary if use "void enterFunction()"
#include <stdlib.h> // Standard library
//#include <ctype.h> Most of the functions in this library serve to sort ASCII characters, only necessary if use "void enterFunction()"
#include <windows.h> // Contains declarations for all Windows API functions
//char function[100]; Probably need to use the GSL
float err = 0; // Global variable
int inter = 0; // Global variable
/*
Probably need to use the GSL
void enterFunction(){
int i;
char correct;
printf("\n\n\t\t Insert your function of x: ");
scanf("%s", &function);
getchar();
for ( i = 0; i < strlen(function); i++){
function[i] = tolower(function[i]);
}
do{
printf("\n\n\t\t This is your f(x) = %s, is it correct? y/n ", &function);
scanf("%c", &correct);
correct = tolower(correct);
getchar();
if(correct != 'y'){
printf("\n\n\t\t Insert your function of x again: ");
scanf("%s", &function);
getchar();
for ( i = 0; i < strlen(function); i++){
function[i] = tolower(function[i]);
}
}
}while (correct != 'y');
}
*/
// Function to calculate the f(x)
double f(double x){
double fun;
fun = exp(-x)-x; // (e^-x)-x
return fun;
}
// Function to calculate the derivate of f(x)
double fdx(double x){
double fundx;
fundx = -exp(-x)-1; // (-e^-x)-1
return fundx;
}
// Function to enter with the error to stop the program
void initialError(){
printf("\n\n\t\t Enter error to stop: ");
scanf("%f", &err);
if(err >= 1){
printf("\n\n\t\t Error to stop entered too big, enter again: ");
scanf("%f", &err);
}
system("cls");
}
// Function to enter with the amount of interections that the program has to execute
void interaction(){
printf("\n\n\t\t Enter the amount of interaction: ");
scanf("%d", &inter);
while (inter < 1)
{
printf("\n\n\t\t Amount of interaction less then 1, enter again: ");
scanf("%d", &inter);
}
system("cls");
}
// Function to calculate the absolute value of any real value
double absol(double x){
if(x < 0){
return -x;
}
else{
return x;
}
}
// Function to calculate the relative error [(xn - xn-1)/xn]
double relativeError(double xk, double xkl1){
double calc_error;
calc_error = absol((xk - xkl1)/xk); // Error must always be positive
return calc_error;
}
// Function to calculate xk for the Bisection Method
double xkBisection(double a, double b){
double xk;
xk = (a+b)/2; // Average Value
return xk;
}
// Void Function to realize the Bisection Method
void bisection(){
double a, b;
double fa, fb;
double x0[inter], f0[inter];
double err1 = 1;
double signal;
int i = 0;
printf("\n\n\t\t Enter with value of a: ");
scanf("%lf", &a);
printf("\n\n\t\t Enter with value of b: ");
scanf("%lf", &b);
fa = f(a);
fb = f(b);
signal = fa*fb;
if(signal < 0){
printf("\n\n\t\t INT \t\t A \t\t\t F(A) \t\t\t B \t\t\t F(B) \t\t\t XK \t\t\t F(XK) \t\t\t ERR");
while (err1 > err && inter > i){
x0[i] = xkBisection(a, b); // Calculate the xk and store it in a vector
f0[i] = f(x0[i]); // Calculate f(xk)
if(i > 0){
err1 = relativeError(x0[i], x0[i-1]); // Calculate error
}
else{
err1 = 0; // The first error
}
printf("\n\n\t\t %d \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf", i, a, fa, b, fb, x0[i], f0[i], err1); // Printing results row by row
// Keeping the theorem always valid
if(f0[i]*fa < 0){
b = x0[i];
fb = f0[i];
}
else if(f0[i]*fb < 0){
a = x0[i];
fa = f0[i];
}
if(i == 0){
err1 = 1;
}
i++;
}
if (err1 > err){
printf("\n\n\n");
printf("\n\n\t\t Number of interactions wasn't enough");
}
printf("\n\n\n");
printf("\n\t\t Result: %lf", x0[i-1]);
printf("\n\t\t Interactions: %d", i);
printf("\n\t\t Error: %lf", err1);
printf("\n\n\n");
printf("\t\t Press any key to continue ");
fflush(stdin); //Capture \n
getchar();
}
else{
printf("\n\n\t\t Interval does not respect the Intermediate Value Theorem"); // The base of bissection method
Sleep(3500);
}
}
// Function to calculate xk for the False Position Method
double xkFalsePosition(double a, double b){
double xk;
double num, den;
num = a*f(b) - b*f(a);
den = f(b) - f(a);
xk = num/den;
return xk;
}
// Void Function to realize the False Position Method using the same logic of Bisection Method, just changing how to calculate x0
void falsePosition(){
double a, b;
double fa, fb;
double x0[inter], f0[inter];
double err1 = 1;
double signal;
int i = 0;
printf("\n\n\t\t Enter with value of a: ");
scanf("%lf", &a);
printf("\n\n\t\t Enter with value of b: ");
scanf("%lf", &b);
fa = f(a);
fb = f(b);
signal = fa*fb;
if(signal < 0){
printf("\n\n\t\t INT \t\t A \t\t\t F(A) \t\t\t B \t\t\t F(B) \t\t\t XK \t\t\t F(XK) \t\t\t ERR");
while (err1 > err && inter > i){
x0[i] = xkFalsePosition(a, b);
f0[i] = f(x0[i]);
if(i > 0){
err1 = relativeError(x0[i], x0[i-1]);
}
else{
err1 = 0;
}
printf("\n\n\t\t %d \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf", i, a, fa, b, fb, x0[i], f0[i], err1);
if(f0[i]*fa < 0){
b = x0[i];
fb = f0[i];
}
else if(f0[i]*fb < 0){
a = x0[i];
fa = f0[i];
}
if(i == 0){
err1 = 1;
}
i++;
}
if (err1 > err){
printf("\n\n\n");
printf("\n\n\t\t Number of interactions wasn't enough");
}
printf("\n\n\n");
printf("\n\t\t Result: %lf", x0[i-1]);
printf("\n\t\t Interactions: %d", i);
printf("\n\t\t Error: %lf", err1);
printf("\n\n\n");
printf("\t\t Press any key to continue ");
fflush(stdin);
getchar();
}
else{
printf("\n\n\t\t Interval does not respect the Intermediate Value Theorem");
Sleep(3500);
}
}
// Function to calculate xk for the Newton Method
double xkNewton(double x){
double xk;
xk = x - (f(x)/fdx(x));
return xk;
}
// Void Function to realize the Newton Method
void newton(){
double x0;
double xk[inter], fk[inter], fdk[inter];
double err1 = 1;
double fx0, fdx0;
int i = 0;
printf("\n\n\t\t Enter with value of X0: ");
scanf("%lf", &x0); // Can be changed by a the first position on xk vector
fx0 = f(x0);
fdx0 = fdx(x0);
printf("\n\n\t\t INT \t\t X \t\t\t F(X) \t\t\t F'(X) \t\t\t XK \t\t\t F(XK) \t\t\t F'(XK) \t\t\t ERR");
while (err1 > err && inter > i){
if(i == 0){
xk[i] = xkNewton(x0);
fk[i] = f(xk[i]);
fdk[i] = fdx(xk[i]);
err1 = 0;
printf("\n\n\t\t %d \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf", i, x0, fx0, fdx0, xk[i], fk[i], fdk[i], err1);
err1 = 1;
}
else{
xk[i] = xkNewton(xk[i-1]);
fk[i] = f(xk[i]);
fdk[i] = fdx(xk[i]);
err1 = relativeError(xk[i], xk[i-1]);
printf("\n\n\t\t %d \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf", i, xk[i-1], fk[i-1], fdk[i-1], xk[i], fk[i], fdk[i], err1);
}
i++;
}
if (err1 > err){
printf("\n\n\n");
printf("\n\n\t\t Number of interactions wasn't enough or will diverge"); // This Method doesn't ensure the convergence
}
printf("\n\n\n");
printf("\n\t\t Result: %lf", xk[i-1]);
printf("\n\t\t Interactions: %d", i);
printf("\n\t\t Error: %lf", err1);
printf("\n\n\n");
printf("\t\t Press any key to continue ");
fflush(stdin);
getchar();
}
// Function to calculate xk for the Secant Method
double xkSecant(double xkl1, double xk){
double xkm1;
double num, den;
num = xkl1*f(xk) - xk*f(xkl1);
den = f(xk) - f(xkl1);
xkm1 = num/den;
return xkm1;
}
// Void Function to realize the Secant Method using the same logic of False Position Method, just changing the entries
void secant(){
double xk[inter], fk[inter];
double err1 = 1;
int i = 0;
printf("\n\n\t\t Enter with value of XO: ");
scanf("%lf", &xk[0]);
printf("\n\n\t\t Enter with value of X1 : ");
scanf("%lf", &xk[1]);
fk[0] = f(xk[0]);
fk[1] = f(xk[1]);
printf("\n\n\t\t INT \t\t A \t\t\t F(A) \t\t\t B \t\t\t F(B) \t\t\t XK \t\t\t F(XK) \t\t\t ERR");
while (err1 > err && inter > i){
xk[i+2] = xkSecant(xk[i], xk[i+1]);
fk[i+2] = f(xk[i+2]);
if(i > 0){
err1 = relativeError(xk[i+2], xk[i+1]);
}
else{
err1 = 0;
}
printf("\n\n\t\t %d \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf \t\t %lf", i, xk[i], fk[i], xk[i+1], fk[i+1], xk[i+2], fk[i+2], err1);
if(i == 0){
err1 = 1;
}
i++;
}
if (err1 > err){
printf("\n\n\n");
printf("\n\n\t\t Number of interactions wasn't enough or will diverge"); // This Method doesn't ensure the convergence
}
printf("\n\n\n");
printf("\n\t\t Result: %lf", xk[i+1]);
printf("\n\t\t Interactions: %d", i);
printf("\n\t\t Error: %lf", err1);
printf("\n\n\n");
printf("\t\t Press any key to continue ");
fflush(stdin);
getchar();
}
// Initializing the program
int main(){
int choose;
do{
printf("\n\n\t\t ********************************************************************");
printf("\n\t\t WELCOME TO THE ROOTS DISCOVERER");
printf("\n\t\t ********************************************************************");
printf("\n\n\t\t 1 - Bisection Method");
printf("\n\n\t\t 2 - False Position Method");
printf("\n\n\t\t 3 - Newton Method");
printf("\n\n\t\t 4 - Secant Method");
printf("\n\n\t\t 5 - Exit");
printf("\n\n\t\t Choose an option: ");
scanf("%d", &choose);
while(choose < 1 || choose > 5){
printf("\n\n\t\t Choose a right option: ");
scanf("%d", &choose);
}
system("cls");
if (choose != 5){
interaction();
initialError();
system("cls");
//enterFunction();
}
switch (choose)
{
case 1:
bisection();
system("cls");
break;
case 2:
falsePosition();
system("cls");
break;
case 3:
newton();
system("cls");
break;
case 4:
secant();
system("cls");
break;
}
}while (choose != 5);
printf("\n\n\t\t\t\t\t\t\t That's it \2"); //🙂
Sleep(500);
return 0;
}