Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions maths/bisection_method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @function bisectionMethod
* @description Bisection method is a root-finding method that applies to any continuous function for which one knows two values with opposite signs.
* @param {number} a - The first value
* @param {number} b - The second value
* @param {number} e - The error value
* @param {Function} f - The function
* @return {number} - The root of the function
* @see [BisectionMethod](https://en.wikipedia.org/wiki/Bisection_method)
* @example bisectionMethod(1, 2, 0.01, (x) => x**2 - 2) = 1.4140625
* @example bisectionMethod(1, 2, 0.01, (x) => x**2 - 3) = 1.732421875
*/

export const bisectionMethod = (a: number, b: number, e: number, f: Function): number => {
let c = a
while ((b - a) >= e) {
c = (a + b) / 2
if (f(c) === 0.0) {
break
} else if (f(c) * f(a) < 0) {
b = c
} else {
a = c
}
}
return c
}
20 changes: 20 additions & 0 deletions maths/decimal_convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @function decimalConvert
* @description Convert the binary to decimal.
* @param {string} binary - The input binary
* @return {number} - Decimal of binary.
* @see [DecimalConvert](https://www.programiz.com/javascript/examples/binary-decimal)
* @example decimalConvert(1100) = 12
* @example decimalConvert(1110) = 14
*/

export const decimalConvert = (binary: string): number => {
let decimal = 0
let binaryArr = binary.split('').reverse()

for (let i = 0; i < binaryArr.length; i++) {
decimal += parseInt(binaryArr[i]) * Math.pow(2, i)
}

return decimal
}
25 changes: 25 additions & 0 deletions maths/euler_method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @function eulerMethod
* @description Euler's method is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value.
* @param {number} x0 - The initial value of x
* @param {number} y0 - The initial value of y
* @param {number} h - The step size
* @param {number} n - The number of iterations
* @param {Function} f - The function
* @return {number} - The value of y at x
* @see [EulerMethod](https://en.wikipedia.org/wiki/Euler_method)
* @example eulerMethod(0, 1, 0.1, 10, (x, y) => x + y) = 2.5937424601
* @example eulerMethod(0, 1, 0.1, 10, (x, y) => x * y) = 1.7715614317
*/

export const eulerMethod = (x0: number, y0: number, h: number, n: number, f: Function): number => {
let x = x0
let y = y0

for (let i = 1; i <= n; i++) {
y = y + h * f(x, y)
x = x + h
}

return y
}