Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1cd3b86

Browse files
ggkogkouggkogkougithub-actions
authored
merge: Created midpoint integration numerical method (#822)
* Created midpoint integration numerical method * Auto-update DIRECTORY.md * Added resources link * Fixed doxumentation * Fixed spelling error Co-authored-by: ggkogkou <ggkogkou@ggkogkou.gr> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 072523d commit 1cd3b86

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
* [MatrixExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixExponentiationRecursive.js)
175175
* [MatrixMultiplication](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixMultiplication.js)
176176
* [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MeanSquareError.js)
177+
* [MidpointIntegration](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MidpointIntegration.js)
177178
* [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ModularBinaryExponentiationRecursive.js)
178179
* [NumberOfDigits](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/NumberOfDigits.js)
179180
* [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js)

‎Maths/MidpointIntegration.js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
*
3+
* @title Midpoint rule for definite integral evaluation
4+
* @author [ggkogkou](https://github.com/ggkogkou)
5+
* @brief Calculate definite integrals with midpoint method
6+
*
7+
* @details The idea is to split the interval in a number N of intervals and use as interpolation points the xi
8+
* 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
9+
* first and last points of the interval of the integration [a, b].
10+
*
11+
* We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:
12+
* I = h * {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
13+
*
14+
* N must be > 0 and a<b. By increasing N, we also increase precision
15+
*
16+
* [More info link](https://tutorial.math.lamar.edu/classes/calcii/approximatingdefintegrals.aspx)
17+
*
18+
*/
19+
20+
function integralEvaluation (N, a, b, func) {
21+
// Check if all restrictions are satisfied for the given N, a, b
22+
if (!Number.isInteger(N) || Number.isNaN(a) || Number.isNaN(b)) { throw new TypeError('Expected integer N and finite a, b') }
23+
if (N <= 0) { throw Error('N has to be >= 2') } // check if N > 0
24+
if (a > b) { throw Error('a must be less or equal than b') } // Check if a < b
25+
if (a === b) return 0 // If a === b integral is zero
26+
27+
// Calculate the step h
28+
const h = (b - a) / N
29+
30+
// Find interpolation points
31+
let xi = a // initialize xi = x0
32+
const pointsArray = []
33+
34+
// Find the sum {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
35+
let temp
36+
for (let i = 0; i < N; i++) {
37+
temp = func(xi + h / 2)
38+
pointsArray.push(temp)
39+
xi += h
40+
}
41+
42+
// Calculate the integral
43+
let result = h
44+
temp = 0
45+
for (let i = 0; i < pointsArray.length; i++) temp += pointsArray[i]
46+
47+
result *= temp
48+
49+
if (Number.isNaN(result)) { throw Error('Result is NaN. The input interval does not belong to the functions domain') }
50+
51+
return result
52+
}
53+
54+
export { integralEvaluation }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { integralEvaluation } from '../MidpointIntegration'
2+
3+
test('Should return the integral of f(x) = sqrt(x) in [1, 3] to be equal 2.797434', () => {
4+
const result = integralEvaluation(10000, 1, 3, (x) => { return Math.sqrt(x) })
5+
expect(Number(result.toPrecision(6))).toBe(2.79743)
6+
})
7+
8+
test('Should return the integral of f(x) = sqrt(x) + x^2 in [1, 3] to be equal 11.46410161', () => {
9+
const result = integralEvaluation(10000, 1, 3, (x) => { return Math.sqrt(x) + Math.pow(x, 2) })
10+
expect(Number(result.toPrecision(10))).toBe(11.46410161)
11+
})
12+
13+
test('Should return the integral of f(x) = log(x) + Pi*x^3 in [5, 12] to be equal 15809.9141543', () => {
14+
const result = integralEvaluation(20000, 5, 12, (x) => { return Math.log(x) + Math.PI * Math.pow(x, 3) })
15+
expect(Number(result.toPrecision(10))).toBe(15809.91415)
16+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /