CppNoddy  0.92
Loading...
Searching...
No Matches
Namespaces | Functions | Variables
Quadrature.cpp File Reference

Compute the integral. More...

#include <Generic_bundle.h>
#include <FnQuadrature.h>

Go to the source code of this file.

Namespaces

namespace  CppNoddy
 A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechanics.
 
namespace  CppNoddy::Example
 

Functions

void CppNoddy::Example::Qfn (const double &x, double &f)
 The function that defines the integrand. More...
 
void CppNoddy::Example::test (unsigned n)
 The test will fail if it requires more than a set number of subintervals in the integration to converge to the exact value to within the specified tol. More...
 
int main ()
 

Variables

bool CppNoddy::Example::failed = false
 

Detailed Description

Compute the integral.

\[ \int_0^{20} \cos(x) \exp \left ( -\frac{x}{4} \right ) \mbox{d}x \]

with varying schemes, then compare the result to the exact value

\[ \frac{4 e^{-5}}{17} \left ( e^5 - \cos (20) + 4\sin (20) \right ) \]

Definition in file Quadrature.cpp.

Function Documentation

◆ main()

int main ( )

Define I as being a Quadrature object with limits 0,20 and a default of 1 sub-interval.

Definition at line 41 of file Quadrature.cpp.

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}
A quadrature class that takes a function pointer.
Definition: FnQuadrature.h:15

References CppNoddy::FnQuadrature::set_subintervals(), CppNoddy::FnQuadrature::sub_Gauss(), and CppNoddy::FnQuadrature::trapezium().

© 2012

R.E. Hewitt