FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Maths/MidpointIntegration.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Maths
/
MidpointIntegration.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
63 lines (54 loc) · 1.88 KB
Breadcrumbs
JavaScript
/
Maths
/
MidpointIntegration.js
Copy path
File metadata and controls
63 lines (54 loc) · 1.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
*
*
@title
Midpoint rule for definite integral evaluation
*
@author
[ggkogkou](https://github.com/ggkogkou)
*
@brief
Calculate definite integrals with midpoint method
*
*
@details
The idea is to split the interval in a number N of intervals and use as interpolation points the xi
* for which it applies that xi = x0 + i*h, where h is a step defined as h = (b-a)/N where a and b are the
* first and last points of the interval of the integration [a, b].
*
* We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:
* I = h * {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
*
* N must be > 0 and a<b. By increasing N, we also increase precision
*
* [More info link](https://tutorial.math.lamar.edu/classes/calcii/approximatingdefintegrals.aspx)
*
*/
function
integralEvaluation
(
N
,
a
,
b
,
func
)
{
// Check if all restrictions are satisfied for the given N, a, b
if
(
!
Number
.
isInteger
(
N
)
||
Number
.
isNaN
(
a
)
||
Number
.
isNaN
(
b
)
)
{
throw
new
TypeError
(
'Expected integer N and finite a, b'
)
}
if
(
N
<=
0
)
{
throw
Error
(
'N has to be >= 2'
)
}
// check if N > 0
if
(
a
>
b
)
{
throw
Error
(
'a must be less or equal than b'
)
}
// Check if a < b
if
(
a
===
b
)
return
0
// If a === b integral is zero
// Calculate the step h
const
h
=
(
b
-
a
)
/
N
// Find interpolation points
let
xi
=
a
// initialize xi = x0
const
pointsArray
=
[
]
// Find the sum {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
let
temp
for
(
let
i
=
0
;
i
<
N
;
i
++
)
{
temp
=
func
(
xi
+
h
/
2
)
pointsArray
.
push
(
temp
)
xi
+=
h
}
// Calculate the integral
let
result
=
h
temp
=
pointsArray
.
reduce
(
(
acc
,
currValue
)
=>
acc
+
currValue
,
0
)
result
*=
temp
if
(
Number
.
isNaN
(
result
)
)
{
throw
Error
(
'Result is NaN. The input interval does not belong to the functions domain'
)
}
return
result
}
export
{
integralEvaluation
}
Back
|
FazBrowse Home
|
New Git URL