From 02b31342e0dba3703c1a3a91352f7ae19764d7ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Mon, 29 Nov 2021 13:18:22 +0100 Subject: gauss quadratur zeugs --- buch/chapters/060-integral/gq/kreis.cpp | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 buch/chapters/060-integral/gq/kreis.cpp (limited to 'buch/chapters/060-integral/gq/kreis.cpp') diff --git a/buch/chapters/060-integral/gq/kreis.cpp b/buch/chapters/060-integral/gq/kreis.cpp new file mode 100644 index 0000000..c9723cc --- /dev/null +++ b/buch/chapters/060-integral/gq/kreis.cpp @@ -0,0 +1,66 @@ +/* + * kreis.cpp -- Gauss Quadrature Example + * + * (c) 2021 Prof Dr Andreas Müller, OST Ostschweizer Fachhochschule + */ +#include +#include +#include +#include +#include +#include + +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; +} -- cgit v1.2.1