Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F121648044
Test.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Jul 12, 19:34
Size
4 KB
Mime Type
text/x-c
Expires
Mon, Jul 14, 19:34 (2 d)
Engine
blob
Format
Raw Data
Handle
27366741
Attached To
R7917 Guggisberg-Liardon - Numerical integration
Test.cpp
View Options
#include <iostream>
#include <math.h>
#include <iostream>
#include <vector>
#include <gtest/gtest.h>
#include "IntegrationSolver1D.hpp"
#include "IntegrationSolver2DCartesian.hpp"
using
namespace
std
;
/**
* User defined functions
* @param x: vector of double
* @return: double
* Function is defined as f: Rn -> R where n is at least 1
*/
double
myFunction1D
(
vector
<
double
>
x
)
{
return
x
[
0
]
*
x
[
0
];
}
double
myFunction2D
(
vector
<
double
>
x
)
{
return
x
[
1
]
+
x
[
0
];
}
/**
* Testing the accuracy of integration of the 1D methods
*/
TEST
(
IntegrationAcurracy
,
Method1D
){
// Exact value of the integral of x² on [0,5]:
double
exactValue
=
41.6666666
;
// Function
double
(
*
functionPointer1D
)(
vector
<
double
>
);
// Function method
functionPointer1D
=
&
myFunction1D
;
// Function pointer
FunctionRntoR
function1D
(
functionPointer1D
);
// Function object
// Domain
DomainCartesian
domain1D
(
0
,
5
,
100
);
// x from 0 to 5, 100 subdivisions
// Method
Method
Midpoint1D
(
"Midpoint"
);
Method
Simpson1D
(
"Simpson"
);
Method
Trapz1D
(
"Trapz"
);
// Solving
IntegrationSolver1D
Midpoint
(
function1D
,
domain1D
,
Midpoint1D
);
IntegrationSolver1D
Simpson
(
function1D
,
domain1D
,
Simpson1D
);
IntegrationSolver1D
Trapz
(
function1D
,
domain1D
,
Trapz1D
);
double
MidpointResult
=
Midpoint
.
GetResult
();
double
SimpsonResult
=
Simpson
.
GetResult
();
double
TrapzResult
=
Trapz
.
GetResult
();
std
::
cout
<<
"The exact result of 1D integration is: "
<<
exactValue
<<
"
\n
"
;
std
::
cout
<<
"The result of 1D integration is: "
<<
MidpointResult
<<
" using "
<<
Midpoint1D
.
name
<<
"'s method."
<<
"
\n
"
;
std
::
cout
<<
"The result of 1D integration is: "
<<
SimpsonResult
<<
" using "
<<
Simpson1D
.
name
<<
"'s method."
<<
"
\n
"
;
std
::
cout
<<
"The result of 1D integration is: "
<<
TrapzResult
<<
" using "
<<
Trapz1D
.
name
<<
"'s method."
<<
"
\n
"
;
// Testing
ASSERT_NEAR
(
exactValue
,
MidpointResult
,
0.01
);
ASSERT_NEAR
(
exactValue
,
SimpsonResult
,
0.01
);
ASSERT_NEAR
(
exactValue
,
TrapzResult
,
0.01
);
}
/**
* Testing the accuracy of integration of the 2D methods
*/
TEST
(
IntegrationAcurracy
,
Method2D
){
// Exact value of the integral of x+y on {[0,5][0,5]}:
double
exactValue
=
125
;
// Function
double
(
*
functionPointer2D
)(
vector
<
double
>
);
// Function method
functionPointer2D
=
&
myFunction2D
;
// Function pointer
FunctionRntoR
function1D
(
functionPointer2D
);
// Function object
// Boundaries matrix : x and y from 0 to 5
vector
<
vector
<
double
>>
boundaries2D
;
vector
<
double
>
dim1
;
vector
<
double
>
dim2
;
dim1
.
push_back
(
0
);
dim1
.
push_back
(
5
);
dim2
.
push_back
(
0
);
dim2
.
push_back
(
5
);
boundaries2D
.
push_back
(
dim1
);
boundaries2D
.
push_back
(
dim2
);
// Subdivision vector : 100 x 100 subdivisions
vector
<
int
>
subdivision
;
subdivision
.
push_back
(
100
);
subdivision
.
push_back
(
100
);
// Creation of the domain of integration - Cartesian domain (rectangle of n dimensions)
DomainCartesian
domain2D
(
boundaries2D
,
subdivision
);
// Method
Method
Midpoint2D
(
"Midpoint"
);
Method
Simpson2D
(
"Simpson"
);
Method
Trapz2D
(
"Trapz"
);
// Solving
IntegrationSolver2DCartesian
Midpoint
(
function1D
,
domain2D
,
Midpoint2D
);
IntegrationSolver2DCartesian
Simpson
(
function1D
,
domain2D
,
Simpson2D
);
IntegrationSolver2DCartesian
Trapz
(
function1D
,
domain2D
,
Trapz2D
);
double
MidpointResult
=
Midpoint
.
GetResult
();
double
SimpsonResult
=
Simpson
.
GetResult
();
double
TrapzResult
=
Trapz
.
GetResult
();
std
::
cout
<<
"The exact result of 2D integration is: "
<<
exactValue
<<
"
\n
"
;
std
::
cout
<<
"The result of 2D integration is: "
<<
MidpointResult
<<
" using "
<<
Midpoint2D
.
name
<<
"'s method."
<<
"
\n
"
;
std
::
cout
<<
"The result of 2D integration is: "
<<
SimpsonResult
<<
" using "
<<
Simpson2D
.
name
<<
"'s method."
<<
"
\n
"
;
std
::
cout
<<
"The result of 2D integration is: "
<<
TrapzResult
<<
" using "
<<
Trapz2D
.
name
<<
"'s method."
<<
"
\n
"
;
// Testing
ASSERT_NEAR
(
exactValue
,
MidpointResult
,
0.01
);
ASSERT_NEAR
(
exactValue
,
SimpsonResult
,
0.3
);
// this method is less accurate so the tolerance is higher.
ASSERT_NEAR
(
exactValue
,
TrapzResult
,
0.01
);
}
int
main
(
int
argc
,
char
**
argv
)
{
// running tests
testing
::
InitGoogleTest
(
&
argc
,
argv
);
return
RUN_ALL_TESTS
();
}
Event Timeline
Log In to Comment