CppNoddy  0.92
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
CppNoddy::FnQuadrature Class Reference

A quadrature class that takes a function pointer. More...

#include <FnQuadrature.h>

Inheritance diagram for CppNoddy::FnQuadrature:
CppNoddy::Uncopyable

Public Types

typedef void(* fn_ptr) (const double &, double &)
 The function pointer associated with this instance. More...
 

Public Member Functions

 FnQuadrature (fn_ptr ptr_to_fn, const double &x1, const double &x2, const unsigned &num_of_regions)
 Constructor. More...
 
 FnQuadrature (fn_ptr ptr_to_fn, const double &x1, const double &x2, const DenseVector< double > &nodes)
 Constructor. More...
 
void set_subintervals (const unsigned &n)
 A set method to define a UNIFORM number of sub intervals. More...
 
double Gauss (const int &n)
 n-point Gauss rule inefficiently written! More...
 
double sub_Gauss (const int &n)
 Evaluate the integral by applying an n-point Gauss rule on each of N sub-intervals. More...
 
double trapezium ()
 Quick trapezium summation again for sanity checking. More...
 

Detailed Description

A quadrature class that takes a function pointer.

Definition at line 15 of file FnQuadrature.h.

Member Typedef Documentation

◆ fn_ptr

typedef void(* CppNoddy::FnQuadrature::fn_ptr) (const double &, double &)

The function pointer associated with this instance.

Definition at line 20 of file FnQuadrature.h.

Constructor & Destructor Documentation

◆ FnQuadrature() [1/2]

CppNoddy::FnQuadrature::FnQuadrature ( fn_ptr  ptr_to_fn,
const double &  x1,
const double &  x2,
const unsigned &  num_of_regions 
)

Constructor.

Parameters
ptr_to_fnthe function that defines the integrand.
x1left hand boundary of the domain.
x2right hand boundary of the domain.
num_of_regionsinitial number of sub-regions to divide the domain into.

Definition at line 15 of file FnQuadrature.cpp.

18 :
19 A(x1),
20 B(x2),
21 p_FN(ptr_to_fn) {
22 NODES = Utility::uniform_node_vector(x1, x2, num_of_regions);
23 }
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

References CppNoddy::Utility::uniform_node_vector().

◆ FnQuadrature() [2/2]

CppNoddy::FnQuadrature::FnQuadrature ( fn_ptr  ptr_to_fn,
const double &  x1,
const double &  x2,
const DenseVector< double > &  nodes 
)

Constructor.

Parameters
ptr_to_fnthe function that defines the integrand.
x1left hand boundary of the domain.
x2right hand boundary of the domain.
nodesA vector of nodal positions.

Definition at line 26 of file FnQuadrature.cpp.

29 :
30 A(x1),
31 B(x2),
32 p_FN(ptr_to_fn) {
33 NODES = nodes;
34 }

Member Function Documentation

◆ Gauss()

double CppNoddy::FnQuadrature::Gauss ( const int &  n)

n-point Gauss rule inefficiently written!

Parameters
nThe number of points.
Returns
Approximation to the integral

Definition at line 40 of file FnQuadrature.cpp.

40 {
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 }
@ f
Definition: BVPBerman.cpp:15

References f, m, CppNoddy::DenseVector< _Type >::push_back(), CppNoddy::DenseVector< _Type >::scale(), and CppNoddy::DenseVector< _Type >::size().

Referenced by sub_Gauss().

◆ set_subintervals()

void CppNoddy::FnQuadrature::set_subintervals ( const unsigned &  n)

A set method to define a UNIFORM number of sub intervals.

Parameters
nNumber of sub intervals

Definition at line 36 of file FnQuadrature.cpp.

36 {
37 NODES = Utility::uniform_node_vector(A, B, n);
38 }

References CppNoddy::Utility::uniform_node_vector().

Referenced by main().

◆ sub_Gauss()

double CppNoddy::FnQuadrature::sub_Gauss ( const int &  n)

Evaluate the integral by applying an n-point Gauss rule on each of N sub-intervals.

This is inefficient in terms of class instantiation for each sub-interval, but not currently an issue.

Parameters
nThe order of the Gauss rule.
Returns
Approximation to the integral.

Definition at line 88 of file FnQuadrature.cpp.

88 {
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 }
std::size_t size() const
A pass-thru definition to get the size of the vector.
Definition: DenseVector.h:330
FnQuadrature(fn_ptr ptr_to_fn, const double &x1, const double &x2, const unsigned &num_of_regions)
Constructor.

References Gauss(), and CppNoddy::DenseVector< _Type >::size().

Referenced by main().

◆ trapezium()

double CppNoddy::FnQuadrature::trapezium ( )

Quick trapezium summation again for sanity checking.

Should be essentially equivalent to the 1-point Gauss rule.

Returns
An approximation to the integral.

Definition at line 106 of file FnQuadrature.cpp.

106 {
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 }

References h, and CppNoddy::DenseVector< _Type >::size().

Referenced by main().


The documentation for this class was generated from the following files:

© 2012

R.E. Hewitt