Skip to content

Commit 2619ab6

Browse files
ggkogkouggkogkougithub-actions
authored
merge: Added bisection method (#827)
* feat: Added bisection method * Auto-update DIRECTORY.md Co-authored-by: ggkogkou <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent f692da2 commit 2619ab6

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
* [BinaryConvert](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/BinaryConvert.js)
146146
* [BinaryExponentiationIterative](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/BinaryExponentiationIterative.js)
147147
* [BinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/BinaryExponentiationRecursive.js)
148+
* [BisectionMethod](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/BisectionMethod.js)
148149
* [CheckKishnamurthyNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/CheckKishnamurthyNumber.js)
149150
* [Coordinate](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Coordinate.js)
150151
* [CoPrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/CoPrimeCheck.js)

Maths/BisectionMethod.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
*
3+
* @file
4+
* @brief Find real roots of a function in a specified interval [a, b], where f(a)*f(b) < 0
5+
*
6+
* @details Given a function f(x) and an interval [a, b], where f(a) * f(b) < 0, find an approximation of the root
7+
* by calculating the middle m = (a + b) / 2, checking f(m) * f(a) and f(m) * f(b) and then by choosing the
8+
* negative product that means Bolzano's theorem is applied,, define the new interval with these points. Repeat until
9+
* we get the precision we want [Wikipedia](https://en.wikipedia.org/wiki/Bisection_method)
10+
*
11+
* @author [ggkogkou](https://github.com/ggkogkou)
12+
*
13+
*/
14+
15+
const findRoot = (a, b, func, numberOfIterations) => {
16+
// Check if a given real value belongs to the function's domain
17+
const belongsToDomain = (x, f) => {
18+
const res = f(x)
19+
return !Number.isNaN(res)
20+
}
21+
if (!belongsToDomain(a, func) || !belongsToDomain(b, func)) throw Error("Given interval is not a valid subset of function's domain")
22+
23+
// Bolzano theorem
24+
const hasRoot = (a, b, func) => {
25+
return func(a) * func(b) < 0
26+
}
27+
if (hasRoot(a, b, func) === false) { throw Error('Product f(a)*f(b) has to be negative so that Bolzano theorem is applied') }
28+
29+
// Declare m
30+
const m = (a + b) / 2
31+
32+
// Recursion terminal condition
33+
if (numberOfIterations === 0) { return m }
34+
35+
// Find the products of f(m) and f(a), f(b)
36+
const fm = func(m)
37+
const prod1 = fm * func(a)
38+
const prod2 = fm * func(b)
39+
40+
// Depending on the sign of the products above, decide which position will m fill (a's or b's)
41+
if (prod1 > 0 && prod2 < 0) return findRoot(m, b, func, --numberOfIterations)
42+
else if (prod1 < 0 && prod2 > 0) return findRoot(a, m, func, --numberOfIterations)
43+
else throw Error('Unexpected behavior')
44+
}
45+
46+
export { findRoot }

Maths/test/BisectionMethod.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { findRoot } from '../BisectionMethod'
2+
3+
test('Equation f(x) = x^2 - 3*x + 2 = 0, has root x = 1 in [a, b] = [0, 1.5]', () => {
4+
const root = findRoot(0, 1.5, (x) => { return Math.pow(x, 2) - 3 * x + 2 }, 8)
5+
expect(root).toBe(0.9990234375)
6+
})
7+
8+
test('Equation f(x) = ln(x) + sqrt(x) + π*x^2 = 0, has root x = 0.36247037 in [a, b] = [0, 10]', () => {
9+
const root = findRoot(0, 10, (x) => { return Math.log(x) + Math.sqrt(x) + Math.PI * Math.pow(x, 2) }, 32)
10+
expect(Number(Number(root).toPrecision(8))).toBe(0.36247037)
11+
})
12+
13+
test('Equation f(x) = sqrt(x) + e^(2*x) - 8*x = 0, has root x = 0.93945851 in [a, b] = [0.5, 100]', () => {
14+
const root = findRoot(0.5, 100, (x) => { return Math.exp(2 * x) + Math.sqrt(x) - 8 * x }, 32)
15+
expect(Number(Number(root).toPrecision(8))).toBe(0.93945851)
16+
})

0 commit comments

Comments
 (0)