From 1cd3b8683a8647f83fedebf447486484e25a182d Mon Sep 17 00:00:00 2001 From: ggkogkou <76820848+ggkogkou@users.noreply.github.com> Date: Thu, 28 Oct 2021 15:39:40 +0300 Subject: [PATCH] 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 Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + Maths/MidpointIntegration.js | 54 ++++++++++++++++++++++++++ Maths/test/MidpointIntegration.test.js | 16 ++++++++ 3 files changed, 71 insertions(+) create mode 100644 Maths/MidpointIntegration.js create mode 100644 Maths/test/MidpointIntegration.test.js diff --git a/DIRECTORY.md b/DIRECTORY.md index fdfc8233e..52edb49ba 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -174,6 +174,7 @@ * [MatrixExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixExponentiationRecursive.js) * [MatrixMultiplication](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixMultiplication.js) * [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MeanSquareError.js) + * [MidpointIntegration](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MidpointIntegration.js) * [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ModularBinaryExponentiationRecursive.js) * [NumberOfDigits](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/NumberOfDigits.js) * [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js) diff --git a/Maths/MidpointIntegration.js b/Maths/MidpointIntegration.js new file mode 100644 index 000000000..175875a47 --- /dev/null +++ b/Maths/MidpointIntegration.js @@ -0,0 +1,54 @@ +/** +* +* @title Midpoint rule for definite integral evaluation +* @author [ggkogkou](https://github.com/ggkogkou) +* @brief Calculate definite integrals with midpoint method +* +* @details The idea is to split the interval in a 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 * {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)} +* +* N must be > 0 and a= 2') } // check if N > 0 + if (a > b) { throw Error('a must be less or equal than b') } // Check if a < b + if (a === b) return 0 // If a === b integral is zero + + // 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+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)} + let temp + for (let i = 0; i < N; i++) { + temp = func(xi + h / 2) + pointsArray.push(temp) + xi += h + } + + // Calculate the integral + let result = h + 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 does not belong to the functions domain') } + + return result +} + +export { integralEvaluation } diff --git a/Maths/test/MidpointIntegration.test.js b/Maths/test/MidpointIntegration.test.js new file mode 100644 index 000000000..4e2d62578 --- /dev/null +++ b/Maths/test/MidpointIntegration.test.js @@ -0,0 +1,16 @@ +import { integralEvaluation } from '../MidpointIntegration' + +test('Should return the integral of f(x) = sqrt(x) in [1, 3] to be equal 2.797434', () => { + const result = integralEvaluation(10000, 1, 3, (x) => { return Math.sqrt(x) }) + expect(Number(result.toPrecision(6))).toBe(2.79743) +}) + +test('Should return the integral of f(x) = sqrt(x) + x^2 in [1, 3] to be equal 11.46410161', () => { + const result = integralEvaluation(10000, 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(20000, 5, 12, (x) => { return Math.log(x) + Math.PI * Math.pow(x, 3) }) + expect(Number(result.toPrecision(10))).toBe(15809.91415) +})