| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 072523d commit 1cd3b86
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -174,6 +174,7 @@ | |||
| 174 | 174 | * [MatrixExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixExponentiationRecursive.js) | |
| 175 | 175 | * [MatrixMultiplication](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixMultiplication.js) | |
| 176 | 176 | * [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MeanSquareError.js) | |
| 177 | + * [MidpointIntegration](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MidpointIntegration.js) | ||
| 177 | 178 | * [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ModularBinaryExponentiationRecursive.js) | |
| 178 | 179 | * [NumberOfDigits](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/NumberOfDigits.js) | |
| 179 | 180 | * [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js) | |
| Original file line number | Diff line number | Diff 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 } | ||
| Original file line number | Diff line number | Diff 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 | + }) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments