Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eed30c6

Browse files
author
ggkogkou
committedOct 27, 2021
Created composite Simpson's integration method.Tests included
1 parent 970b4f2 commit eed30c6

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
 

‎Maths/SimpsonIntegration.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
*
3+
* @file
4+
* @title Composite Simpson's rule for definite integral evaluation
5+
* @author: [ggkogkou](https://github.com/ggkogkou)
6+
* @brief Calculate definite integrals using composite Simpson's numerical method
7+
*
8+
* @details The idea is to split the interval in an EVEN number N of intervals and use as interpolation points the xi
9+
* 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
10+
* first and last points of the interval of the integration [a, b].
11+
*
12+
* We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:
13+
* I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
14+
*
15+
* That means that the first and last indexed i f(xi) are multiplied by 1,
16+
* the odd indexed f(xi) by 4 and the even by 2.
17+
*
18+
* N must be even number and a<b. By increasing N, we also increase precision
19+
*
20+
* More info: [Wikipedia link](https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule)
21+
*
22+
*/
23+
24+
25+
function integralEvaluation(N, a, b, func) {
26+
// Check if N is an even integer
27+
let isNEven = (N) => {
28+
if(N%2 === 0)
29+
return true;
30+
return false;
31+
}
32+
if(!Number.isInteger(N) || Number.isNaN(a) || Number.isNaN(b)) { throw new TypeError("Expected integer N and finite a, b"); }
33+
if(!isNEven) { throw "N is not an even number"; }
34+
35+
// Check if a < b
36+
if(a > b) { throw "a must be less or equal than b"; }
37+
if(a === b) return 0;
38+
39+
// Calculate the step h
40+
const h = (b - a) / N;
41+
42+
// Find interpolation points
43+
let xi = a; // initialize xi = x0
44+
let pointsArray = [];
45+
46+
// Find the sum {f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
47+
let temp;
48+
for(let i = 0; i < N+1; i++){
49+
if(i === 0 || i === N) temp = func(xi);
50+
else if(i%2 === 0) temp = 2*func(xi);
51+
else temp = 4*func(xi);
52+
53+
pointsArray.push(temp);
54+
xi += h;
55+
}
56+
57+
// Calculate the integral
58+
let result = h/3;
59+
temp = 0;
60+
for(let i=0; i < pointsArray.length; i++) temp += pointsArray[i];
61+
62+
result *= temp;
63+
64+
if (Number.isNaN(result)) { throw "Result is NaN. The input interval doesn't belong to the function's domain"; }
65+
66+
return result;
67+
}
68+
69+
export { integralEvaluation }

‎Maths/test/SimpsonIntegration.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { integralEvaluation } from "./SimpsonIntegration";
2+
3+
test("Should return the integral of f(x) = sqrt(x) in [1, 3] to be equal 2.797434", () => {
4+
const result = integralEvaluation(16, 1, 3, (x) => {return Math.sqrt(x);});
5+
expect(Number(result.toPrecision(7))).toBe(2.797434);
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(64, 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(128, 5, 12, (x) => {return Math.log(x) + Math.PI*Math.pow(x, 3);});
15+
expect(Number(result.toPrecision(12))).toBe(15809.9141543);
16+
});

0 commit comments

Comments
 (0)
Please sign in to comment.