Skip to content

Commit 9b477f1

Browse files
Added the implementation for bijection method binary to decimal and euler method
1 parent 44127b2 commit 9b477f1

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

maths/bisection_method.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @function bisectionMethod
3+
* @description Bisection method is a root-finding method that applies to any continuous function for which one knows two values with opposite signs.
4+
* @param {number} a - The first value
5+
* @param {number} b - The second value
6+
* @param {number} e - The error value
7+
* @param {Function} f - The function
8+
* @return {number} - The root of the function
9+
* @see [BisectionMethod](https://en.wikipedia.org/wiki/Bisection_method)
10+
* @example bisectionMethod(1, 2, 0.01, (x) => x**2 - 2) = 1.4140625
11+
* @example bisectionMethod(1, 2, 0.01, (x) => x**2 - 3) = 1.732421875
12+
*/
13+
14+
export const bisectionMethod = (a: number, b: number, e: number, f: Function): number => {
15+
let c = a
16+
while ((b - a) >= e) {
17+
c = (a + b) / 2
18+
if (f(c) === 0.0) {
19+
break
20+
} else if (f(c) * f(a) < 0) {
21+
b = c
22+
} else {
23+
a = c
24+
}
25+
}
26+
return c
27+
}

maths/decimal_convert.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @function decimalConvert
3+
* @description Convert the binary to decimal.
4+
* @param {string} binary - The input binary
5+
* @return {number} - Decimal of binary.
6+
* @see [DecimalConvert](https://www.programiz.com/javascript/examples/binary-decimal)
7+
* @example decimalConvert(1100) = 12
8+
* @example decimalConvert(1110) = 14
9+
*/
10+
11+
export const decimalConvert = (binary: string): number => {
12+
let decimal = 0
13+
let binaryArr = binary.split('').reverse()
14+
15+
for (let i = 0; i < binaryArr.length; i++) {
16+
decimal += parseInt(binaryArr[i]) * Math.pow(2, i)
17+
}
18+
19+
return decimal
20+
}

maths/euler_method.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @function eulerMethod
3+
* @description Euler's method is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value.
4+
* @param {number} x0 - The initial value of x
5+
* @param {number} y0 - The initial value of y
6+
* @param {number} h - The step size
7+
* @param {number} n - The number of iterations
8+
* @param {Function} f - The function
9+
* @return {number} - The value of y at x
10+
* @see [EulerMethod](https://en.wikipedia.org/wiki/Euler_method)
11+
* @example eulerMethod(0, 1, 0.1, 10, (x, y) => x + y) = 2.5937424601
12+
* @example eulerMethod(0, 1, 0.1, 10, (x, y) => x * y) = 1.7715614317
13+
*/
14+
15+
export const eulerMethod = (x0: number, y0: number, h: number, n: number, f: Function): number => {
16+
let x = x0
17+
let y = y0
18+
19+
for (let i = 1; i <= n; i++) {
20+
y = y + h * f(x, y)
21+
x = x + h
22+
}
23+
24+
return y
25+
}

0 commit comments

Comments
 (0)