/
integrate.c
122 lines (77 loc) · 2.54 KB
/
integrate.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
#include <stdio.h>
#include <math.h>
#include "timer.h"
#include "adjust.h"
#include "integral_general.h"
void integral_recur (int nmin, int nmax, double vals[]) {
// works backwards from I_100 using a given value at 100, and calculates the
// values of the integral between nmin and nmax and stores them (as well as
// calculating for all values > nmax, but it does not store them)
double vals1[100 + 1];
vals1[100] = 0.00367843028136748866811;
for(int i = 99; i >= nmin; vals1[i] = vals1[i + 1] / (i + 1) + 1 / (M_E * (i + 1)), i--);
int j = 0;
for(int i = nmin; i <= nmax; i++, j++) {
vals[j] = vals1[i];
}
}
void integral_gen (int nmin, int nmax, double vals[]);
void integral_gen (int nmin, int nmax, double vals[]) {
// this function uses the gsl library to calculate the value of the integral
// numerically
gsl_integration_function(nmax, nmin, vals);
}
int main(void) {
#define NMAX 100 //array size for storing the values of I_n
int nminn = 0; //min value of n to be calulated for integrals
int nmaxx = 100; //max value of n to be calculated for integrals
double vals1[NMAX + 1], vals2[NMAX + 1];
integral_recur (nminn, nmaxx, vals1);
integral_gen (nminn, nmaxx, vals2);
// prints and compares the values of the different methods
for(int i = 0,j = nminn; i <= nmaxx - nminn; i++,j++) {
printf("%.18f\t%.18f\t%d\n", vals1[i], vals2[i], j);
}
int count = 10;
double time;
double time1;
double tmin = 1;
double tmax = 2;
// times integral_recur and integral_gen per function call
// adjusts the number of calls such that there are enough calls to get a good
// average time per function call
printf("integral_recur\n");
do {
timer_start ();
for (int k = 0; k <= count; k++) {
integral_recur (nminn, nmaxx, vals1);
}
time = timer_stop ();
time1 = time / count;
printf (" %10.2f usec %10.6f sec %10d\n",
time1 * 1.e6, time, count);
/*
* adjust count such that cpu time is between
* tmin and tmax
*/
count = adjust_rep_count (count, time, tmin, tmax);
} while ((time > tmax) || (time < tmin));
printf("integral_gen\n");
count = 10;
do {
timer_start ();
for (int k = 0; k <= count; k++) {
integral_gen (nminn, nmaxx, vals2);
}
time = timer_stop ();
time1 = time / count;
printf (" %10.2f usec %10.6f sec %10d\n",
time1 * 1.e6, time, count);
/*
* adjust count such that cpu time is between
* tmin and tmax
*/
count = adjust_rep_count (count, time, tmin, tmax);
} while ((time > tmax) || (time < tmin));
return 0;
}