"git@developer.sourcefind.cn:OpenDAS/pytorch3d.git" did not exist on "4d9215b3b4851a209d24b19e4503360c08b42ea8"
integrate_function_adapt_simp_ex.cpp 2.83 KB
Newer Older
Davis King's avatar
Davis King committed
1
// The contents of this file are in the public domain.  See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
2
3
/*

Davis King's avatar
Davis King committed
4
5
6
7
8
    This example demonstrates the usage of the numerical quadrature function
    integrate_function_adapt_simp().  This function takes as input a single variable
    function, the endpoints of a domain over which the function will be integrated, and a
    tolerance parameter.  It outputs an approximation of the integral of this function over
    the specified domain.  The algorithm is based on the adaptive Simpson method outlined in: 
9

Davis King's avatar
Davis King committed
10
11
12
        Numerical Integration method based on the adaptive Simpson method in
        Gander, W. and W. Gautschi, "Adaptive Quadrature – Revisited,"
        BIT, Vol. 40, 2000, pp. 84-101
13
14
15
16
17
18

*/

#include <iostream>
#include <dlib/matrix.h>
#include <dlib/numeric_constants.h>
19
#include <dlib/numerical_integration.h>
20
21
22
23

using namespace std;
using namespace dlib;

Davis King's avatar
Davis King committed
24
25
// Here we the set of functions that we wish to integrate and comment in the domain of
// integration.
26

27
// x in [0,1]
Davis King's avatar
Davis King committed
28
double gg1(double x)
29
30
31
32
33
{
    return pow(e,x);
}   

// x in [0,1]
Davis King's avatar
Davis King committed
34
double gg2(double x)
35
36
37
38
39
{
    return x*x;
}

// x in [0, pi]
Davis King's avatar
Davis King committed
40
double gg3(double x)
41
42
43
44
45
{
    return 1/(x*x + cos(x)*cos(x));
}

// x in [-pi, pi]
Davis King's avatar
Davis King committed
46
double gg4(double x)
47
48
49
50
51
{
    return sin(x);
}

// x in [0,2]
Davis King's avatar
Davis King committed
52
double gg5(double x)
53
54
55
{
    return 1/(1 + x*x);
}
56
57
58

int main()
{
59
    // We first define a tolerance parameter.  Roughly speaking, a lower tolerance will
60
61
62
63
64
65
66
67
68
69
70
    // result in a more accurate approximation of the true integral.  However, there are 
    // instances where too small of a tolerance may yield a less accurate approximation
    // than a larger tolerance.  We recommend taking the tolerance to be in the
    // [1e-10, 1e-8] region.
    
    double tol = 1e-10;


    // Here we compute the integrals of the five functions defined above using the same 
    // tolerance level for each.

71
72
73
74
75
    double m1 = integrate_function_adapt_simp(&gg1, 0.0, 1.0, tol);
    double m2 = integrate_function_adapt_simp(&gg2, 0.0, 1.0, tol);
    double m3 = integrate_function_adapt_simp(&gg3, 0.0, pi, tol);
    double m4 = integrate_function_adapt_simp(&gg4, -pi, pi, tol);
    double m5 = integrate_function_adapt_simp(&gg5, 0.0, 2.0, tol);
76
77
78
79

    // We finally print out the values of each of the approximated integrals to ten
    // significant digits.

80
81
82
83
84
    cout << "\nThe integral of exp(x) for x in [0,1] is "          << std::setprecision(10) <<  m1  << endl; 
    cout << "The integral of x^2 for in [0,1] is "                 << std::setprecision(10) <<  m2  << endl; 
    cout << "The integral of 1/(x^2 + cos(x)^2) for in [0,pi] is " << std::setprecision(10) <<  m3  << endl;
    cout << "The integral of sin(x) for in [-pi,pi] is "           << std::setprecision(10) <<  m4  << endl;
    cout << "The integral of 1/(1+x^2) for in [0,2] is "           << std::setprecision(10) <<  m5  << endl;
Davis King's avatar
Davis King committed
85
    cout << endl;
86
87
88
89

    return 0;
}