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
|
/*
* kreis.cpp -- Gauss Quadrature Example
*
* (c) 2021 Prof Dr Andreas Müller, OST Ostschweizer Fachhochschule
*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <gsl/gsl_integration.h>
double xmax = 0.990;
double f(double x, void *) {
return sqrt(1-x*x);
}
double do_integration(size_t n) {
// set up integration gauss quadrature
gsl_function func;
func.function = f;
func.params = NULL;
gsl_integration_fixed_workspace *ws
= gsl_integration_fixed_alloc(
gsl_integration_fixed_legendre,
n, -xmax, xmax, 0, 0);
double result = 0;
gsl_integration_fixed(&func, &result, ws);
gsl_integration_fixed_free(ws);
return result;
}
double do_trapez(size_t n) {
double h = 2 * xmax / n;
double s = 0;
for (int i = 0; i <= n; i++) {
double x = -xmax + i * h;
double a = f(x, NULL);
if ((i == 0) || (i == n)) {
a = a * 0.5;
}
s += a;
}
return h * s;
}
void do_table(double limit) {
xmax = limit;
for (int n = 2; n <= 20; n += 2) {
printf("%d & %.16f & %.16f \\\\\n", n, do_integration(n),
do_trapez(10 * n));
}
double amax = acos(xmax);
double expected = (M_PI / 2 - amax) + sin(amax) * cos(amax);
printf("\\infty & %.16f & %.16f \\\\\n", expected, expected);
}
int main(int argc, char *argv[]) {
do_table(0.5);
do_table(0.9);
do_table(0.99);
do_table(0.999);
return EXIT_SUCCESS;
}
|