2
0
mirror of https://github.com/boostorg/math.git synced 2026-02-10 11:32:33 +00:00

Adaptive Trapezoidal Quadrature

This routine estimates the definite integral of a function f.
Assuming that f is periodic, it can be shown that this routine converges exponentially fast.
In fact, the test cases given exhibit exponential convergence with decreasing stepsize.

A potential improvement is using the Bulirsch sequence rather than the Romberg sequence to schedule the refinements.
However, the convergence is so rapid for functions of the class specified above that there seems to be no need at present.

This code is cppcheck clean, and runs successfully under AddressSanitizer and UndefinedBehaviorSanitizer.
This commit is contained in:
Nick Thompson
2017-03-05 19:05:41 -06:00
parent 4c19a1ec34
commit 136e7411f5
4 changed files with 330 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
/*
* Copyright Nick Thompson, 2017
*
* This example shows to to numerically integrate a periodic function using the adaptive_trapezoidal routine provided by boost.
*/
#include <iostream>
#include <cmath>
#include <limits>
#include <boost/math/quadrature/adaptive_trapezoidal.hpp>
int main()
{
using boost::math::constants::two_pi;
using boost::math::constants::third;
// This function has an analytic form for its integral over a period: 2pi/3.
auto f = [](double x) { return 1/(5 - 4*cos(x)); };
double Q = boost::math::adaptive_trapezoidal(f, (double) 0, two_pi<double>());
std::cout << std::setprecision(std::numeric_limits<double>::digits10);
std::cout << "The adaptive trapezoidal rule gives the integral of our function as " << Q << "\n";
std::cout << "The exact result is " << two_pi<double>()*third<double>() << "\n";
}