CppNoddy  0.92
Loading...
Searching...
No Matches
FnQuadrature.cpp
Go to the documentation of this file.
1/// \file FnQuadrature.cpp
2/// Implementation of the real quadrature class. Note that
3/// the object uses a function pointer for the integrand.
4
5#include <string>
6
7#include <Types.h>
8#include <FnQuadrature.h>
9#include <Exceptions.h>
10#include <Utility.h>
11
12namespace CppNoddy {
13
14 // ctor
16 const double& x1,
17 const double& x2,
18 const unsigned& num_of_regions) :
19 A(x1),
20 B(x2),
21 p_FN(ptr_to_fn) {
22 NODES = Utility::uniform_node_vector(x1, x2, num_of_regions);
23 }
24
25 // ctor
27 const double& x1,
28 const double& x2,
29 const DenseVector<double>& nodes) :
30 A(x1),
31 B(x2),
32 p_FN(ptr_to_fn) {
33 NODES = nodes;
34 }
35
36 void FnQuadrature::set_subintervals(const unsigned& n) {
37 NODES = Utility::uniform_node_vector(A, B, n);
38 }
39
40 double FnQuadrature::Gauss(const int& n) {
41 DenseVector<double> t; // evaluation points in [-1,1]
42 DenseVector<double> w; // respective weight values
43 switch(n) {
44 case 1:
45 t.push_back(0.0);
46 w.push_back(2.0);
47 break;
48 case 2:
49 t.push_back(-1. / sqrt(3.));
50 w.push_back(1.0);
51 t.push_back(1. / sqrt(3.));
52 w.push_back(1.0);
53 break;
54 case 3:
55 t.push_back(0.0);
56 w.push_back(8. / 9.);
57 t.push_back(-0.774596669241483);
58 w.push_back(5. / 9.);
59 t.push_back(0.774596669241483);
60 w.push_back(5. / 9.);
61 break;
62 default:
63 std::string problem;
64 problem = " The Quadrature.Gauss method is trying to apply \n";
65 problem += " a Gauss rule with more points than 3. \n";
66 problem += " Currenlty only n=1,2,3 are supported! \n";
67 throw ExceptionRuntime(problem);
68 }
69
70 double sum = 0.0;
71 double c = 0.5 * (B + A);
72 double m = 0.5 * (B - A);
73 double f, x;
74 t.scale(m);
75
76 for(unsigned i = 0; i < t.size(); ++i) {
77 x = c + t[ i ];
78 p_FN(x, f);
79 sum += w[ i ] * f;
80 }
81
82 sum = sum * m;
83
84 return sum;
85 }
86
87
88 double FnQuadrature::sub_Gauss(const int& n) {
89 double x_left, x_right;
90 double sum = 0.0;
91 unsigned num = NODES.size();
92 // double h = ( B - A ) / num;
93
94 for(unsigned i = 0; i < num - 1; ++i) {
95 //x_left = A + i * h;
96 x_left = NODES[ i ];
97 //x_right = x_left + h;
98 x_right = NODES[ i + 1 ];
99 FnQuadrature I(p_FN, x_left, x_right, num);
100 sum += I.Gauss(n);
101 }
102
103 return sum;
104 }
105
107 double sum = 0.0;
108 unsigned num = NODES.size();
109 double h = (B - A) / num;
110 double x_left, x_right;
111 double f_left, f_right;
112
113 x_left = A;
114 p_FN(x_left, f_left);
115
116 for(unsigned i = 0; i < num; ++i) {
117 x_right = A + (i + 1) * h;
118 p_FN(x_right, f_right);
119 sum += (f_left + f_right);
120 f_left = f_right;
121 }
122 sum *= h / 2.;
123 return sum;
124 }
125
126
127}
128
@ f
Definition: BVPBerman.cpp:15
The collection of CppNoddy exceptions.
A specification for quadrature classes.
A spec for a collection of utility functions.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
void push_back(const _Type &fill)
A pass-thru definition of push_back.
Definition: DenseVector.h:310
std::size_t size() const
A pass-thru definition to get the size of the vector.
Definition: DenseVector.h:330
void scale(const _Type &scale)
Scale each element of the vector, equivalent to *=.
Definition: DenseVector.cpp:33
A generic runtime exception.
Definition: Exceptions.h:158
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 Gauss(const int &n)
n-point Gauss rule inefficiently written!
FnQuadrature(fn_ptr ptr_to_fn, const double &x1, const double &x2, const unsigned &num_of_regions)
Constructor.
double trapezium()
Quick trapezium summation again for sanity checking.
DenseVector< double > uniform_node_vector(const double &lower, const double &upper, const std::size_t &N)
Return a DENSE vector with the nodal points of a uniform mesh distributed between the upper/lower bou...
Definition: Utility.cpp:113
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt