Numerical Integration

This chapter describes routines for performing numerical integration (quadrature) of a function in one dimension. There are routines for adaptive and non-adaptive integration of general functions, with specialised routines for specific cases. These include integration over infinite and semi-infinite ranges, singular integrals, including logarithmic singularities, computation of Cauchy principal values and oscillatory integrals. The library reimplements the algorithms used in QUADPACK, a numerical integration package written by Piessens, de Doncker-Kapenga, Ueberhuber and Kahaner. Fortran code for QUADPACK is available on Netlib. Also included are non-adaptive, fixed-order Gauss-Legendre integration routines with high precision coefficients by Pavel Holoborodko.

The functions described in this chapter are declared in the header file gsl_integration.h.


Building GSL on Windows

3.22.2014

A few pointers for compiling Windows binaries of the GNU Scientific Library (GSL).

Description

The GNU Scientific Library (GSL) is a fantastic library for scientific computing. Within the C/C++ world I don't think there's anything comparable. My only gripe is the C-style interface, I would prefer C++. Anyways, I tend to use it quite a bit. Personally I use a combination of OS X and Linux, both of which are easy platforms for GSL - the latter moreso, but on OS X you can get it easily through the package managers (I currently use brew).

Windows

Unfortunately many other people in this world still use Windows for scientific computing, so I have to be cross-platform. I've starting using the Visual Studio toolchain, in particular Visual Studio 2013. I can get it for free as a student (through Dreamspark) and it plays nicely with Python.

After some internet sleuthing I found that someone has already made a Visual Studio project for compiling GSL, which can be foundhere. Thanks Brian! He only advertises it as working with Visual Studio 2012. However with a few tweaks I managed to get it to work on 2013. There were a few header files the project looked for in the wrong place. Also it is finicky about order. I found out that you have to build the statically-linked libraries first, and then the dynamically-linked libraries. For the latter I had to copy the *.lib files into wherever it wanted them. Fortunately the error messages encountered tend to be helpful.

As a simplicity for anyone who wants them, I built binaries (lib and dll) using GSL v1.16 and VS2013 for both 32-bit and 64-bit architecture, and they are available here (zip, 12MB). Have fun!


Examples

The integrator QAGS will handle a large class of definite integrals. For example, consider the following integral, which has an algebraic-logarithmic singularity at the origin,

\int_0^1 x^{-1/2} log(x) dx = -4

The program below computes this integral to a relative accuracy bound of 1e-7.

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>

double f (double x, void * params) {
  double alpha = *(double *) params;
  double f = log(alpha*x) / sqrt(x);
  return f;
}

int
main (void)
{
  gsl_integration_workspace * w 
    = gsl_integration_workspace_alloc (1000);
  
  double result, error;
  double expected = -4.0;
  double alpha = 1.0;

  gsl_function F;
  F.function = &f;
  F.params = &alpha;

  gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                        w, &result, &error); 

  printf ("result          = % .18f\n", result);
  printf ("exact result    = % .18f\n", expected);
  printf ("estimated error = % .18f\n", error);
  printf ("actual error    = % .18f\n", result - expected);
  printf ("intervals =  %d\n", w->size);

  gsl_integration_workspace_free (w);

  return 0;
}

The results below show that the desired accuracy is achieved after 8 subdivisions.

$ ./a.out 
result          = -3.999999999999973799
exact result    = -4.000000000000000000
estimated error =  0.000000000000246025
actual error    =  0.000000000000026201
intervals =  8

In fact, the extrapolation procedure used by QAGS produces an accuracy of almost twice as many digits. The error estimate returned by the extrapolation procedure is larger than the actual error, giving a margin of safety of one order of magnitude.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值