-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumerical.c
350 lines (322 loc) · 9.91 KB
/
numerical.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
#include <stdio.h>
#include <math.h>
double f(double x){ // f(x) Definition
return exp(x)-10;
}
int fibonacci(int n){
if(n==0){
return 0;
}
if(n==1){
return 1;
}
return fibonacci(n-1)+fibonacci(n-2);
}
// Numerical Differentiation
double derivative(double f(double x), double x0){
double eps=1.0e-6; // eps is the Offset Used to Calculate the Derivative
double x1 = x0 - eps;
double x2 = x0 + eps;
double y1 = f(x1);
double y2 = f(x2);
return (y2 - y1) / (x2 - x1);
}
/* Numerical Integration */
// Left Side Integral Approximation
double leftIntegral(double f(double x), double a, double b, double n){
double x,h,sum=0,integral;
int i;
h=fabs(b-a)/n;
for(i=0;i<n;i++){
x=a+i*h;
sum=sum+f(x);
}
integral=h*sum;
return integral;
}
// Right Side Integral Approximation
double rightIntegral(double f(double x), double a, double b, double n){
double x,h,sum=0,integral;
int i;
h=fabs(b-a)/n;
for(i=1;i<=n;i++){
x=a+i*h;
sum=sum+f(x);
}
integral=h*sum;
return integral;
}
// Midpoint Riemann Sum Integral Approximation
double middleIntegral(double f(double x), double a, double b, double n){
double x,h,sum=0,integral;
int i;
h=fabs(b-a)/n;
for(i=0;i<n;i++){
x=a+(2*i+1)*h/2;
sum=sum+f(x);
}
integral=h*sum;
return integral;
}
// Trapezoidal Rule
double trapezoidal(double f(double x), double a, double b, int n){
double x,h,sum=0,integral;
int i;
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+f(x);
}
integral=(h/2)*(f(a)+f(b)+2*sum);
return integral;
}
// Simpsons' Rule
double simpsons(double f(double x), double a, double b, int n){
double x,h,oddTermSum=0,evenTermSum=0,integral;
int i;
h=fabs(b-a)/n;
for(i=1;i<n;i=i+2){
x=a+i*h;
oddTermSum=oddTermSum+f(x);
}
for(i=2;i<n;i=i+2){
x=a+i*h;
evenTermSum=evenTermSum+f(x);
}
oddTermSum=4*oddTermSum;
evenTermSum=2*evenTermSum;
integral=h*(oddTermSum+evenTermSum+f(a)+f(b))/3;
return integral;
}
/* Root Approximation */
// Newton Raphson Method
double newtonRaphson(double f(double x), double eps, double x0, double maxIterations){
double x;
int i=1;
while(fabs((f(x)-0))>eps&&i<=maxIterations){
x=x0;
if(fabs(derivative(f,x0))>=0.0000000001){
x0=x-f(x)/derivative(f,x);
i++;
}
}
return x0;
}
double newtonRaphsonRecursion(double f(double x), int maxIterations, double eps, double x0){
if(maxIterations==1){
return x0-f(x0)/derivative(f,x0);
}
double secondLastValue = newtonRaphsonRecursion(f,maxIterations-1,eps,x0);
if(fabs(f(secondLastValue)-0)<eps){
return secondLastValue;
}
double lastValue=secondLastValue-f(secondLastValue)/derivative(f,secondLastValue);
return lastValue;
}
double printNewtonRaphson(double f(double x), double eps, double x0, double maxIterations){
int i=1;
double x;
for(int j=1;j<=83;j++){
printf("_");
}
printf("\n");
printf("S.No.\tf(x)\t\tf'(x)\t\tx0\t\t|x-x0|\t\tf(x0)\n");
for(int j=1;j<=83;j++){
printf("-");
}
printf("\n");
while(fabs(f(x0)-0)>eps&&i<=maxIterations){
x=x0;
if(fabs(derivative(f,x0))>=0.0000000001){
x0=x-f(x)/derivative(f,x);
printf("%d\t%lf\t%lf\t%lf\t%lf\t%lf\n",i,f(x),derivative(f,x),x0,fabs(x-x0),f(x0));
i++;
}
}
for(int j=1;j<=83;j++){
printf("-");
}
printf("\n");
return x0;
}
double fprintNewtonRaphson(double f(double x), double eps, double x0, double maxIterations){
int i=1;
double x;
for(int j=1;j<=83;j++){
fprintf(fopen("Calculator History.txt","a"),"_");
}
fprintf(fopen("Calculator History.txt","a"),"\n");
fprintf(fopen("Calculator History.txt","a"),"S.No.\tf(x)\t\tf'(x)\t\tx0\t\t|x-x0|\t\tf(x0)\n");
for(int j=1;j<=83;j++){
fprintf(fopen("Calculator History.txt","a"),"-");
}
fprintf(fopen("Calculator History.txt","a"),"\n");
while(fabs(f(x0)-0)>eps&&i<=maxIterations){
x=x0;
if(fabs(derivative(f,x0))>=0.0000000001){
x0=x-f(x)/derivative(f,x);
fprintf(fopen("Calculator History.txt","a"),"%d\t%lf\t%lf\t%lf\t%lf\t%lf\n",i,f(x),derivative(f,x),x0,fabs(x-x0),f(x0));
i++;
}
}
for(int j=1;j<=83;j++){
fprintf(fopen("Calculator History.txt","a"),"-");
}
fprintf(fopen("Calculator History.txt","a"),"\n");
return x0;
}
// Bisection Method
double printBisection(double f(double x),double a, double b, double eps, int maxIterations){
double c;
if(f(a)*f(b)<=0){
int i=1;
for(int j=0;j<83;j++){
printf("_");
}
printf("\n");
printf("S.No.\t a\t\tb\t\tc\t\tf(c)\t\t|a-b|\n");
for(int j=0;j<83;j++){
printf("-");
}
printf("\n");
do{
c=(a+b)/2;
printf("%d\t%lf\t%lf\t%lf\t%lf\t%lf\n",i,a,b,c,f(c),fabs(a-b));
if(f(a)*f(c)>0){
a=c;
}
else if(f(a)*f(c)<0){
b=c;
}
i++;
}while(fabs(a-b)>eps&&i<=maxIterations);
for(int j=0;j<83;j++){
printf("-");
}
printf("\n");
return c;
}
else{
printf("\nThe Root does NOT Lie within Specified Bounds.\n");
}
return c;
}
// Secant Method
double printSecant(double f(double x), double x1, double x2, double eps, int maxIterations){
int i=1;
double x3;
for(int j=0;j<66;j++){
printf("_");
}
printf("\n");
printf("S.No.\t x1\t\tx2\t\tx3\t\tf(x3)\n");
for(int j=0;j<66;j++){
printf("-");
}
printf("\n");
do{
x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
printf("%d\t%lf\t%lf\t%lf\t%lf\n",i,x1,x2,x3,f(x3));
x1=x2;
x2=x3;
i++;
}while(fabs(f(x3))>eps&&i<=maxIterations);
for(int j=0;j<66;j++){
printf("-");
}
printf("\n");
return x3;
}
int main(){
FILE *fp;
fp = fopen("Calculator History.txt","w");
double infinity=1.7976931348623157E+308;
printf("Hi There!\n");
printf("Which of the following tasks would you like to perform?\n");
printf("1.Numerical Differentiation\n");
printf("2.Numerical Integration\n");
printf("3.Root Approximation\n");
printf("Enter S.No. of desired task: ");
int a;
scanf("%d",&a);
if(a==1){
double x0;
printf("Enter point at which you wish to compute derivative: ");
scanf("%lf",&x0);
printf("Derivative: %lf\n", derivative(f,x0));
fprintf(fp,"Derivative: %lf\n", derivative(f,x0));
}
if(a==2){
printf("Which method would you like?\n");
printf("1.Riemann Sum\n");
printf("2.Simpsons' Rule\n");
int b;
scanf("%d",&b);
if(b==1){
double a,b,n;
printf("Enter Lower Limit, Upper Limit and Partitions: ");
scanf("%lf %lf %lf",&a,&b,&n);
printf("Using Left Riemann Sum: %lf\n",leftIntegral(f,a,b,n));
printf("Using Right Riemann Sum: %lf\n",rightIntegral(f,a,b,n));
printf("Using Midpoint Rule: %lf\n",middleIntegral(f,a,b,n));
printf("Using Trapezoidal Rule: %lf\n",trapezoidal(f,a,b,n));
fprintf(fp,"Using Left Riemann Sum: %lf\n",leftIntegral(f,a,b,n));
fprintf(fp,"Using Right Riemann Sum: %lf\n",rightIntegral(f,a,b,n));
fprintf(fp,"Using Midpoint Rule: %lf\n",middleIntegral(f,a,b,n));
fprintf(fp,"Using Trapezoidal Rule: %lf\n",trapezoidal(f,a,b,n));
}
if(b==2){
double a,b,n;
printf("Enter Lower Limit, Upper Limit and Partitions: ");
scanf("%lf %lf %lf",&a,&b,&n);
printf("Using Simpsons' Rule: %lf\n",simpsons(f,a,b,n));
fprintf(fp,"Using Simpsons' Rule: %lf\n",simpsons(f,a,b,n));
}
}
if(a==3){
printf("Which method would you like?\n");
printf("1.Newton-Raphson Method\n");
printf("2.Bisection Method\n");
printf("3.Secant Method\n");
int c;
scanf("%d",&c);
if(c==1){
double x,eps;
int maxIterations;
printf("Enter the Initial Guess: ");
scanf("%lf",&x);
printf("Enter the Desired Accuracy: ");
scanf("%lf",&eps);
printf("Enter the Max. Number of Steps: ");
scanf("%d",&maxIterations);
printf("One of the Roots of the Given Equation is: %lf\n",printNewtonRaphson(f,x,eps,maxIterations));
printf("One of the Roots of (using Recursion) is: %lf\n",newtonRaphsonRecursion(f,maxIterations,eps,x));
fprintf(fp,"One of the Roots of the Given Equation is: %lf\n",printNewtonRaphson(f,x,eps,maxIterations));
fprintf(fp,"One of the Roots of (using Recursion) is: %lf\n",newtonRaphsonRecursion(f,maxIterations,eps,x));
fprintNewtonRaphson(f,eps,x,maxIterations);
}
if(c==2){
double a,b,x,eps;
int maxIterations;
printf("Enter the Initial Guesses: ");
scanf("%lf %lf",&a, &b);
printf("Enter the Desired Accuracy: ");
scanf("%lf",&eps);
printf("Enter the Max. Number of Steps: ");
scanf("%d",&maxIterations);
printBisection(f,a,b,eps,maxIterations);
}
if(c==3){
double x1,x2,eps;
int maxIterations;
printf("Enter the Initial Guesses: ");
scanf("%lf %lf",&x1,&x2);
printf("Enter the Desired Accuracy: ");
scanf("%lf",&eps);
printf("Enter the Max. Number of Steps: ");
scanf("%d",&maxIterations);
printSecant(f,x1,x2,eps,maxIterations);
}
}
return 0;
}