-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Created composite Simpson's integration method. Tests included. #819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eed30c6
Created composite Simpson's integration method.Tests included
6ccb157
Minor corrections
4a615a8
Auto-update DIRECTORY.md
0626309
Merge branch 'master' of https://github.com/TheAlgorithms/Javascript ...
524c2b8
Merge branch 'simpson_branch' of https://github.com/ggkogkou/Javascri...
1f15216
Styled with standard.js
ffba60c
chore: remove blank line
raklaptudirm 445a56d
chore: remove blank line
raklaptudirm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 doesnt belong to the functions domain') } | ||
|
||
return result | ||
} | ||
|
||
export { integralEvaluation } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { integralEvaluation } from '../SimpsonIntegration' | ||
|
||
test('Should return the integral of f(x) = sqrt(x) in [1, 3] to be equal 2.797434', () => { | ||
const result = integralEvaluation(16, 1, 3, (x) => { return Math.sqrt(x) }) | ||
expect(Number(result.toPrecision(7))).toBe(2.797434) | ||
}) | ||
|
||
test('Should return the integral of f(x) = sqrt(x) + x^2 in [1, 3] to be equal 11.46410161', () => { | ||
const result = integralEvaluation(64, 1, 3, (x) => { return Math.sqrt(x) + Math.pow(x, 2) }) | ||
expect(Number(result.toPrecision(10))).toBe(11.46410161) | ||
}) | ||
|
||
test('Should return the integral of f(x) = log(x) + Pi*x^3 in [5, 12] to be equal 15809.9141543', () => { | ||
const result = integralEvaluation(128, 5, 12, (x) => { return Math.log(x) + Math.PI * Math.pow(x, 3) }) | ||
expect(Number(result.toPrecision(12))).toBe(15809.9141543) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.