Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
JavaScript/Maths/SimpsonIntegration.js at master · 96ADM/JavaScript · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
96ADM
JavaScript
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
JavaScript
/
Maths
/
SimpsonIntegration.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
67 lines (55 loc) · 2.2 KB
master
Breadcrumbs
JavaScript
/
Maths
/
SimpsonIntegration.js
Copy path
Top
File metadata and controls
Code
Blame
67 lines (55 loc) · 2.2 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
64
65
66
67
/*
*
*
@file
*
@title
Composite Simpson's rule for definite integral evaluation
*
@author
: [ggkogkou](https://github.com/ggkogkou)
*
@brief
Calculate definite integrals using composite Simpson's numerical method
*
*
@details
The idea is to split the interval in an EVEN 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/3 * {f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
*
* That means that the first and last indexed i f(xi) are multiplied by 1,
* the odd indexed f(xi) by 4 and the even by 2.
*
* N must be even number and a<b. By increasing N, we also increase precision
*
* More info: [Wikipedia link](https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule)
*
*/
function
integralEvaluation
(
N
,
a
,
b
,
func
)
{
// Check if N is an even integer
let
isNEven
=
true
if
(
N
%
2
!==
0
)
isNEven
=
false
if
(
!
Number
.
isInteger
(
N
)
||
Number
.
isNaN
(
a
)
||
Number
.
isNaN
(
b
)
)
{
throw
new
TypeError
(
'Expected integer N and finite a, b'
)
}
if
(
!
isNEven
)
{
throw
Error
(
'N is not an even number'
)
}
if
(
N
<=
0
)
{
throw
Error
(
'N has to be >= 2'
)
}
// Check if a < b
if
(
a
>
b
)
{
throw
Error
(
'a must be less or equal than b'
)
}
if
(
a
===
b
)
return
0
// 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) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
let
temp
for
(
let
i
=
0
;
i
<
N
+
1
;
i
++
)
{
if
(
i
===
0
||
i
===
N
)
temp
=
func
(
xi
)
else
if
(
i
%
2
===
0
)
temp
=
2
*
func
(
xi
)
else
temp
=
4
*
func
(
xi
)
pointsArray
.
push
(
temp
)
xi
+=
h
}
// Calculate the integral
let
result
=
h
/
3
temp
=
0
for
(
let
i
=
0
;
i
<
pointsArray
.
length
;
i
++
)
temp
+=
pointsArray
[
i
]
result
*=
temp
if
(
Number
.
isNaN
(
result
)
)
{
throw
Error
(
"Result is NaN. The input interval doesn't belong to the functions domain"
)
}
return
result
}
export
{
integralEvaluation
}
You can’t perform that action at this time.