Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the implementation for bijection method binary to decimal and Euler method #251

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 39 additions & 0 deletions maths/bisection_method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @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 => {
if (e <= 0) {
throw new Error('Error threshold must be positive')
}

if (f(a) * f(b) >= 0) {
throw new Error('f(a) and f(b) should have opposite signs')
}

let c = a
while (Math.abs(b - a) / 2 >= e) {
c = (a + b) / 2
if (Math.abs(f(c)) < 1e-9) {
break
} else if (f(c) * f(a) < 0) {
b = c
} else {
a = c
}
}
return c
}
18 changes: 18 additions & 0 deletions maths/decimal_convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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
*/

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
}
38 changes: 38 additions & 0 deletions maths/euler_method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @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 => {
if (typeof f !== 'function') {
throw new Error('f must be a function')
}

if (n < 0) {
throw new Error('Number of iterations must be non-negative')
}

let x = x0
let y = y0

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

return y
}
28 changes: 28 additions & 0 deletions maths/test/bisection_method.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { bisectionMethod } from '../bisection_method'

describe('bisectionMethod', () => {
it('should find the root of f(x) = x^2 - 3 between [1, 2]', () => {
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 2 - 3)
expect(result).toBeCloseTo(1.732, 3)
})

it('should find the root of f(x) = x^3 - x - 2 between [1, 2]', () => {
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 3 - x - 2)
expect(result).toBeCloseTo(1.521, 3)
})

it('should find the root of f(x) = x^2 + x - 6 between [1, 3]', () => {
const result = bisectionMethod(1, 3, 0.001, (x: number) => x ** 2 + x - 6)
expect(result).toBeCloseTo(2, 3)
})

it('should find the root of f(x) = cos(x) - x between [0, 1]', () => {
const result = bisectionMethod(0, 1, 0.001, (x: number) => Math.cos(x) - x)
expect(result).toBeCloseTo(0.739, 2)
})

it('should find the root of f(x) = e^x - 3 between [0, 2]', () => {
const result = bisectionMethod(0, 2, 0.001, (x: number) => Math.exp(x) - 3)
expect(result).toBeCloseTo(1.099, 2)
})
})
31 changes: 31 additions & 0 deletions maths/test/decimal_convert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { decimalConvert } from '../decimal_convert'

describe('decimalConvert', () => {
it('should convert "1100" to 12', () => {
expect(decimalConvert('1100')).toBe(12)
})

it('should convert "1110" to 14', () => {
expect(decimalConvert('1110')).toBe(14)
})

it('should convert "0" to 0', () => {
expect(decimalConvert('0')).toBe(0)
})

it('should convert "1" to 1', () => {
expect(decimalConvert('1')).toBe(1)
})

it('should convert "101" to 5', () => {
expect(decimalConvert('101')).toBe(5)
})

it('should handle an empty string by returning 0', () => {
expect(decimalConvert('')).toBe(0)
})

it('should convert a binary string with leading zeros "0001" to 1', () => {
expect(decimalConvert('0001')).toBe(1)
})
})
42 changes: 42 additions & 0 deletions maths/test/euler_method.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { eulerMethod } from '../euler_method'

describe('eulerMethod', () => {
it('should compute y for dy/dx = y with y(0) = 1 at x = 1', () => {
const result = eulerMethod(0, 1, 0.1, 10, (x: number, y: number) => y)
expect(result).toBeCloseTo(2.59374, 5)
})

it('should compute y for dy/dx = -2y with y(0) = 1 at x = 1', () => {
const result = eulerMethod(0, 1, 0.1, 10, (x: number, y: number) => -2 * y)
const expectedResult = 1 * Math.pow(0.8, 10)
expect(result).toBeCloseTo(expectedResult, 5)
})

it('should compute y for dy/dx = x with y(0) = 0 at x = 1', () => {
const result = eulerMethod(0, 0, 0.1, 10, (x: number, y: number) => x)
expect(result).toBeCloseTo(0.45, 2)
})

it('should compute y for dy/dx = x + y with y(0) = 1 at x = 0.5', () => {
const h = 0.1
const n = 5
const result = eulerMethod(0, 1, h, n, (x: number, y: number) => x + y)
expect(result).toBeCloseTo(1.72102, 5)
})

it('should compute y for dy/dx = x^2 with y(0) = 0 at x = 1', () => {
const result = eulerMethod(0, 0, 0.2, 5, (x: number, y: number) => x ** 2)
expect(result).toBeCloseTo(0.24, 3)
})

it('should handle negative step size for dy/dx = y with y(1) = e', () => {
const result = eulerMethod(
1,
Math.E,
-0.001,
1000,
(x: number, y: number) => y
)
expect(result).toBeCloseTo(1, 2)
})
})
Loading