Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F74413710
IntegrationLib.py
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 27, 17:16
Size
1 KB
Mime Type
text/x-python
Expires
Mon, Jul 29, 17:16 (2 d)
Engine
blob
Format
Raw Data
Handle
19384924
Attached To
rPUBNUMANALYSIS Public Numerical Analysis Jupyter Notebook
IntegrationLib.py
View Options
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 30
@author: Simone Deparis
"""
import
numpy
as
np
# This function just provides the midpoint quadrature rule.
def
Midpoint
(
a
,
b
,
N
,
f
)
:
# [a,b] : inteval
# N : number of subintervals
# f : fonction to integrate using the midpoint rule
# size of the subintervals
H
=
(
b
-
a
)
/
N
# quadrature nodes
x
=
np
.
linspace
(
a
+
H
/
2
,
b
-
H
/
2
,
N
)
# approximate integral
return
H
*
np
.
sum
(
f
(
x
)
);
# This function just provides the midpoint quadrature rule.
def
Trapezoidal
(
a
,
b
,
N
,
f
)
:
# [a,b] : inteval
# N : number of subintervals
# f : fonction to integrate using the trapezoidal rule
# size of the subintervals
H
=
(
b
-
a
)
/
N
# quadrature nodes
x
=
np
.
linspace
(
a
,
b
,
N
+
1
)
# approximate integral
return
H
/
2
*
(
f
(
a
)
+
f
(
b
)
)
+
H
*
sum
(
f
(
x
[
1
:
N
])
);
# This function just provides the Simpson quadrature rule.
def
Simpson
(
a
,
b
,
N
,
f
)
:
# [a,b] : inteval
# N : number of subintervals
# f : fonction to integrate using the trapezoidal rule
# size of the subintervals
H
=
(
b
-
a
)
/
N
# points defining intervals
x
=
np
.
linspace
(
a
,
b
,
N
+
1
)
# left quadrature nodes
xl
=
x
[
0
:
-
1
]
# right quadrature nodes
xr
=
x
[
1
:]
# middle quadrature nodes
xm
=
(
xl
+
xr
)
/
2
# approximate integral
return
H
/
6
*
sum
(
f
(
xl
)
+
4
*
f
(
xm
)
+
f
(
xr
)
);
Event Timeline
Log In to Comment