Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F63817244
IntegralSolver1D.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
Wed, May 22, 16:38
Size
1 KB
Mime Type
text/x-c
Expires
Fri, May 24, 16:38 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17822909
Attached To
R7917 Guggisberg-Liardon - Numerical integration
IntegralSolver1D.cpp
View Options
#include "IntegralSolver1D.hpp"
#include "Function.hpp"
//constructor, deconstructor
IntegralSolver1D
::
IntegralSolver1D
(
Function
function_
,
Domain
domain
,
Method
method
)
{
a
=
domain
.
boundaries
[
0
][
0
];
b
=
domain
.
boundaries
[
0
][
1
];
n
=
(
double
)
domain
.
subdivision1D
;
type
=
method
.
name
;
function
=
function_
;
if
(
type
==
"Midpoint"
)
{
result
=
this
->
IntegrationMidpoint
();
}
if
(
type
==
"Trapz"
)
{
result
=
this
->
IntegrationTrapeze
();
}
if
(
type
==
"Simpson"
)
{
result
=
this
->
IntegrationSimpson
();
}
}
IntegralSolver1D
::~
IntegralSolver1D
()
{}
//Midpoint method:
double
IntegralSolver1D
::
IntegrationMidpoint
()
{
double
result
=
0
;
double
size
=
(
b
-
a
)
/
n
;
for
(
int
i
=
0
;
i
<
n
;
i
++
){
vector
<
double
>
inputVector
;
inputVector
.
assign
(
1
,
size
/
2
+
i
*
size
);
result
+=
size
*
function
.
GetResult
(
inputVector
);
}
return
result
;
}
//Trapeze method:
double
IntegralSolver1D
::
IntegrationTrapeze
()
{
double
result
=
0
;
double
size
=
(
b
-
a
)
/
n
;
for
(
int
i
=
0
;
i
<
n
;
i
++
){
vector
<
double
>
arg1
;
arg1
.
assign
(
1
,
size
*
i
);
vector
<
double
>
arg2
;
arg2
.
assign
(
1
,
size
*
(
i
+
1
));
result
+=
size
*
(
function
.
GetResult
(
arg1
)
+
function
.
GetResult
(
arg2
)
)
/
2
;
}
return
result
;
}
//Simpson Method:
double
IntegralSolver1D
::
IntegrationSimpson
()
{
double
result
=
0
;
double
size
=
(
b
-
a
)
/
n
;
for
(
int
i
=
0
;
i
<
n
;
i
++
){
vector
<
double
>
arg1
;
vector
<
double
>
arg2
;
vector
<
double
>
arg3
;
arg1
.
assign
(
1
,
size
*
i
);
arg2
.
assign
(
1
,
size
/
2
+
i
*
size
);
arg3
.
assign
(
1
,
size
*
(
i
+
1
));
result
+=
size
/
6
*
(
function
.
GetResult
(
arg1
)
+
4
*
function
.
GetResult
(
arg2
)
+
function
.
GetResult
(
arg3
)
);
}
return
result
;
}
// Get result method
double
IntegralSolver1D
::
GetResult
()
{
return
result
;
}
Event Timeline
Log In to Comment