CppNoddy  0.92
Loading...
Searching...
No Matches
Quadrature.cpp
Go to the documentation of this file.
1/// \file Quadrature.cpp
2/// \ingroup Tests
3/// \ingroup Generic
4/// Compute the integral
5/// \f[ \int_0^{20} \cos(x) \exp \left ( -\frac{x}{4} \right ) \mbox{d}x \f] with varying
6/// schemes, then compare the result to the exact value
7/// \f[ \frac{4 e^{-5}}{17} \left ( e^5 - \cos (20) + 4\sin (20) \right ) \f]
8
9#include <Generic_bundle.h>
10#include <FnQuadrature.h>
11
12namespace CppNoddy
13{
14 namespace Example
15 {
16 bool failed = false;
17
18 /// The function that defines the integrand.
19 void Qfn( const double &x, double &f )
20 {
21 f = std::cos( x ) * std::exp( -x / 4. );
22 }
23
24 /// The test will fail if it requires more than a set number
25 /// of subintervals in the integration to converge to the
26 /// exact value to within the specified tol.
27 void test( unsigned n )
28 {
29 if ( n > 16777216 )
30 {
31 failed = true;
32 }
33 }
34
35 } // end Example namespace
36} // end CppNoddy namespace
37
38using namespace CppNoddy;
39using namespace std;
40
41int main()
42{
43
44 cout << "\n";
45 cout << "=== Quadrature: integral of cos(x)exp(-x/4) =========\n";
46 cout << "\n";
47
48 /// Define I as being a Quadrature object with limits 0,20
49 /// and a default of 1 sub-interval.
50 FnQuadrature I( Example::Qfn, 0.0, 20.0 , 1 );
51
52 double result;
53 unsigned n = 1;
54 const double exact = ( 4. / 17. ) * ( exp( 5.0 ) - cos( 20.0 ) + 4 * sin( 20.0 ) ) * exp( -5.0 );
55 const double tol = 1.e-7;
56 cout << " Tolerance used : " << tol << "\n";
57 cout << "\n";
58 cout << " Trapezium summation \n";
59 cout.precision( 12 );
60
61 do
62 {
63 I.set_subintervals( n );
64 result = I.trapezium() - exact;
65 cout << "n = " << n << " Integral error = " << abs( result ) << "\n";
66 n *= 2;
67 Example::test( n );
68 }
69 while ( ( abs( result ) > tol ) && !Example::failed );
70
71 cout << " required " << n / 2 << " sub-intervals. \n";
72 cout << "\n";
73 cout << " Sub_Gauss with 1 Guass point \n";
74
75 n = 1;
76 do
77 {
78 I.set_subintervals( n );
79 result = I.sub_Gauss( 1 ) - exact;
80 cout << "n = " << n << " Integral error = " << abs( result ) << "\n";
81 n *= 2;
82 Example::test( n );
83 }
84 while ( ( abs( result ) > tol ) && !Example::failed );
85
86 cout << " required " << n / 2 << " sub-intervals. \n";
87 cout << "\n";
88 cout << " Sub_Gauss with 2 Guass points \n";
89
90 n = 1;
91 do
92 {
93 I.set_subintervals( n );
94 result = I.sub_Gauss( 2 ) - exact;
95 cout << "n = " << n << " Integral error = " << abs( result ) << "\n";
96 n *= 2;
97 Example::test( n );
98 }
99 while ( ( abs( result ) > tol ) && !Example::failed );
100
101 cout << " required " << n / 2 << " sub-intervals. \n";
102 cout << "\n";
103 cout << " Sub_Gauss with 3 Guass points \n";
104
105 n = 1;
106 do
107 {
108 I.set_subintervals( n );
109 result = I.sub_Gauss( 3 ) - exact;
110 cout << "n = " << n << " Integral error = " << abs( result ) << "\n";
111 n *= 2;
112 Example::test( n );
113 }
114 while ( ( abs( result ) > tol ) && !Example::failed );
115
116 cout << " required " << n / 2 << " sub-intervals. \n";
117
118 if ( Example::failed )
119 {
120 cout << "\033[1;31;48m * FAILED \033[0m\n";
121 return 1;
122 }
123 else
124 {
125 cout << "\033[1;32;48m * PASSED \033[0m\n";
126 return 0;
127 }
128}
@ f
Definition: BVPBerman.cpp:15
A specification for quadrature classes.
A shorter bundled include file for ODE_BVP and PDE_IBVP codes.
int main()
Definition: Quadrature.cpp:41
A quadrature class that takes a function pointer.
Definition: FnQuadrature.h:15
double sub_Gauss(const int &n)
Evaluate the integral by applying an n-point Gauss rule on each of N sub-intervals.
void set_subintervals(const unsigned &n)
A set method to define a UNIFORM number of sub intervals.
double trapezium()
Quick trapezium summation again for sanity checking.
void Qfn(const double &x, double &f)
The function that defines the integrand.
Definition: Quadrature.cpp:19
void test(unsigned n)
The test will fail if it requires more than a set number of subintervals in the integration to conver...
Definition: Quadrature.cpp:27
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt