-
-
Notifications
You must be signed in to change notification settings - Fork 410
Added the implementation for bijection method binary to decimal and Euler method #251
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
Open
mapcrafter2048
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
mapcrafter2048:feature/added-bijection-euler-decimal-convert-method
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9b477f1
Added the implementation for bijection method binary to decimal and e…
mapcrafter2048 813c215
Added the test cases for bisection method euler method and binary to …
mapcrafter2048 cf97cac
Update the test cases and revised the cases
mapcrafter2048 609367d
Merge branch 'TheAlgorithms:master' into feature/added-bijection-eule…
mapcrafter2048 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.