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
|
/* integration/romberg.c
*
* Copyright (C) 2018 Patrick Alken
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* the code in this module performs Romberg integration */
#include <config.h>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_integration.h>
#define ROMBERG_PRINT_ROW(i, r) \
do { \
size_t jj; \
fprintf(stderr, "R[%zu] = ", i); \
for (jj = 0; jj <= i; ++jj) \
fprintf(stderr, "%.8e ", r[jj]); \
fprintf(stderr, "\n"); \
} while (0)
gsl_integration_romberg_workspace *
gsl_integration_romberg_alloc(const size_t n)
{
gsl_integration_romberg_workspace *w;
/* check inputs */
if (n < 1)
{
GSL_ERROR_VAL ("workspace size n must be at least 1", GSL_EDOM, 0);
}
w = calloc(1, sizeof(gsl_integration_romberg_workspace));
if (w == NULL)
{
GSL_ERROR_VAL ("unable to allocate workspace", GSL_ENOMEM, 0);
}
/* ceiling on n, since the number of points is 2^n + 1 */
w->n = GSL_MIN(n, 30);
w->work1 = malloc(w->n * sizeof(double));
if (w->work1 == NULL)
{
gsl_integration_romberg_free(w);
GSL_ERROR_VAL ("unable to allocate previous row", GSL_ENOMEM, 0);
}
w->work2 = malloc(w->n * sizeof(double));
if (w->work2 == NULL)
{
gsl_integration_romberg_free(w);
GSL_ERROR_VAL ("unable to allocate current row", GSL_ENOMEM, 0);
}
return w;
}
void
gsl_integration_romberg_free(gsl_integration_romberg_workspace * w)
{
if (w->work1)
free(w->work1);
if (w->work2)
free(w->work2);
free(w);
}
int
gsl_integration_romberg(const gsl_function * f, const double a, const double b,
const double epsabs, const double epsrel, double * result,
size_t * neval, gsl_integration_romberg_workspace * w)
{
if (epsabs < 0.0)
{
GSL_ERROR("epsabs must be non-negative", GSL_EDOM);
}
else if (epsrel < 0.0)
{
GSL_ERROR("epsrel must be non-negative", GSL_EDOM);
}
else
{
const size_t n = w->n;
double *Rp = &(w->work1[0]); /* previous row */
double *Rc = &(w->work2[0]); /* current row */
double *Rtmp;
double h = 0.5 * (b - a); /* step size */
size_t i;
/* R(0,0) */
Rp[0] = h * (GSL_FN_EVAL(f, a) + GSL_FN_EVAL(f, b));
*neval = 2;
/*ROMBERG_PRINT_ROW((size_t) 0, Rp);*/
for (i = 1; i < n; ++i)
{
size_t j;
double sum = 0.0;
double err;
double four_j; /* 4^j */
size_t two_i = 1 << i; /* 2^i */
for (j = 1; j < two_i; j += 2)
{
sum += GSL_FN_EVAL(f, a + j * h);
++(*neval);
}
/* R(i,0) */
Rc[0] = sum * h + 0.5 * Rp[0];
four_j = 4.0;
for (j = 1; j <= i; ++j)
{
Rc[j] = (four_j * Rc[j - 1] - Rp[j - 1]) / (four_j - 1.0);
four_j *= 4.0;
}
/*ROMBERG_PRINT_ROW(i, Rc);*/
/*
* compute difference between current and previous result and
* check for convergence
*/
err = fabs(Rc[i] - Rp[i - 1]);
if ((err < epsabs) || (err < epsrel * fabs(Rc[i])))
{
*result = Rc[i];
return GSL_SUCCESS;
}
/* swap Rp and Rc */
Rtmp = Rp;
Rp = Rc;
Rc = Rtmp;
h *= 0.5;
}
/* did not converge - return best guess */
*result = Rp[n - 1];
return GSL_EMAXITER;
}
}
|