1 /* 2 * Copyright Nick Thompson, 2017 3 * Use, modification and distribution are subject to the 4 * Boost Software License, Version 1.0. (See accompanying file 5 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 * 7 * This example shows to to numerically integrate a periodic function using the adaptive_trapezoidal routine provided by boost. 8 */ 9 10 #include <iostream> 11 #include <cmath> 12 #include <limits> 13 #include <boost/math/quadrature/trapezoidal.hpp> 14 main()15int main() 16 { 17 using boost::math::constants::two_pi; 18 using boost::math::constants::third; 19 using boost::math::quadrature::trapezoidal; 20 // This function has an analytic form for its integral over a period: 2pi/3. 21 auto f = [](double x) { return 1/(5 - 4*cos(x)); }; 22 23 double Q = trapezoidal(f, (double) 0, two_pi<double>()); 24 25 std::cout << std::setprecision(std::numeric_limits<double>::digits10); 26 std::cout << "The adaptive trapezoidal rule gives the integral of our function as " << Q << "\n"; 27 std::cout << "The exact result is " << two_pi<double>()*third<double>() << "\n"; 28 29 } 30