Define I as being a Quadrature object with limits 0,20 and a default of 1 sub-interval.
42{
43
44 cout << "\n";
45 cout << "=== Quadrature: integral of cos(x)exp(-x/4) =========\n";
46 cout << "\n";
47
48
49
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}
A quadrature class that takes a function pointer.