-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added implementations of Runga kutta method along with test cases for Hacktoberfest contribution #1710
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
6
commits into
TheAlgorithms:master
Choose a base branch
from
mapcrafter2048:algo/runga-kutta
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
Added implementations of Runga kutta method along with test cases for Hacktoberfest contribution #1710
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e78ee7f
Added implementations of Runga kutta method along with test cases
mapcrafter2048 6f24a12
fixed some error regarding the path
mapcrafter2048 2d75811
changes the precision for the tests
mapcrafter2048 01b8478
changes the precision for the tests
mapcrafter2048 fbdbe25
fixed some potential edges cases
mapcrafter2048 7214c0a
Fix code style issues in RungaKutta files
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,80 @@ | ||
/** | ||
* @function rungeKuttaStep | ||
* @description Runge-Kutta step function to calculate the next y-value based on the current x-value, y-value, step size and differential equation. | ||
* @param {number} xCurrent - The current x-value | ||
* @param {number} stepSize - The step size | ||
* @param {number} yCurrent - The current y-value | ||
* @param {function} differentialEquation - The differential equation to solve | ||
* @returns {number} - The next y-value | ||
* @example rungeKuttaStep(0, 0.1, 1, function (x, y) { return Math.sin(x) + y; }); // returns 1.10517 | ||
* @example rungeKuttaStep(0.5, 0.1, 1, function (x, y) { return Math.exp(x) - y; }); // returns 1.15233 | ||
*/ | ||
export function rungeKuttaStep( | ||
xCurrent, | ||
stepSize, | ||
yCurrent, | ||
differentialEquation | ||
) { | ||
// Calculate the four slopes: k1, k2, k3, k4 | ||
const k1 = stepSize * differentialEquation(xCurrent, yCurrent) | ||
const k2 = | ||
stepSize * differentialEquation(xCurrent + stepSize / 2, yCurrent + k1 / 2) | ||
const k3 = | ||
stepSize * differentialEquation(xCurrent + stepSize / 2, yCurrent + k2 / 2) | ||
const k4 = stepSize * differentialEquation(xCurrent + stepSize, yCurrent + k3) | ||
|
||
// Calculate the next y-value using the weighted average of the four slopes | ||
return yCurrent + (1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4) | ||
} | ||
|
||
/** | ||
* @description Runge-Kutta method for solving ordinary differential equations (ODEs) with a given initial value. It is a numerical procedure for solving ODEs. The method proceeds in a series of steps. At each step the y-value is calculated by evaluating the differential equation at the previous step, multiplying the result with the step-size and adding it to the last y-value. | ||
* @param {number} xStart - The starting x-value | ||
* @param {number} xEnd - The ending x-value | ||
* @param {number} stepSize - The step size | ||
* @param {number} yStart - The starting y-value | ||
* @param {function} differentialEquation - The differential equation to solve | ||
* @returns {Array} - An array of points (x, y) for the complete iteration from xStart to xEnd | ||
* @see https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods | ||
* @example rungeKuttaFull(0, 1, 0.2, 1, function (x, y) { return Math.sin(x) + y; }); | ||
* [{ x: 0, y: 1 }, | ||
* { x: 0.2, y: 1.22140 }, | ||
* { x: 0.4, y: 1.53659 }, | ||
* { x: 0.6, y: 1.95837 }, | ||
* { x: 0.8, y: 2.50487 }, | ||
* { x: 1.0, y: 3.20155 }] | ||
*/ | ||
export function rungeKuttaFull( | ||
xStart, | ||
xEnd, | ||
stepSize, | ||
yStart, | ||
differentialEquation | ||
) { | ||
// Collect all the points (x, y) for the complete iteration from xStart to xEnd | ||
const points = [{ x: xStart, y: yStart }] | ||
let yCurrent = yStart | ||
let xCurrent = xStart | ||
|
||
while (xCurrent < xEnd) { | ||
// Runge-Kutta method for the next step | ||
|
||
// Check if the next step will exceed xEnd and adjust the stepSize accordingly | ||
if (xCurrent + stepSize > xEnd) { | ||
stepSize = xEnd - xCurrent | ||
} | ||
|
||
yCurrent = rungeKuttaStep( | ||
xCurrent, | ||
stepSize, | ||
yCurrent, | ||
differentialEquation | ||
) | ||
xCurrent += stepSize | ||
|
||
// Push the new point to the points array | ||
points.push({ x: xCurrent, y: yCurrent }) | ||
} | ||
|
||
return points | ||
} |
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,51 @@ | ||
import { rungeKuttaStep, rungeKuttaFull } from '../RungaKutta' | ||
|
||
describe('rungeKuttaStep', () => { | ||
it('should calculate the next y value correctly for simple linear function f(x, y) = x + y', () => { | ||
const yNext = rungeKuttaStep(0, 0.1, 1, (x, y) => x + y) | ||
expect(yNext).toBeCloseTo(1.1103, 2) // Adjusted expected value and precision | ||
}) | ||
|
||
it('should calculate the next y value correctly for simple product function f(x, y) = x * y', () => { | ||
const yNext = rungeKuttaStep(1, 0.1, 2, (x, y) => x * y) | ||
expect(yNext).toBeCloseTo(2.22, 2) // Adjusted expected value and precision | ||
}) | ||
}) | ||
|
||
describe('rungeKuttaFull', () => { | ||
it('should return all the points found for simple linear function f(x, y) = x + y', () => { | ||
const actual = rungeKuttaFull(0, 0.5, 0.1, 1, (x, y) => x + y) | ||
|
||
const expected = [ | ||
{ x: 0, y: 1 }, | ||
{ x: 0.1, y: 1.11034 }, | ||
{ x: 0.2, y: 1.23272 }, | ||
{ x: 0.3, y: 1.36862 }, | ||
{ x: 0.4, y: 1.58356 }, | ||
{ x: 0.5, y: 1.78944 } | ||
] | ||
|
||
for (let i = 0; i < actual.length; i++) { | ||
expect(actual[i].x).toBeCloseTo(expected[i].x, 1) | ||
expect(actual[i].y).toBeCloseTo(expected[i].y, 1) | ||
} | ||
}) | ||
|
||
it('should return all the points found for simple product function f(x, y) = x * y', () => { | ||
const actual = rungeKuttaFull(1, 1.5, 0.1, 1, (x, y) => x * y) | ||
|
||
const expected = [ | ||
{ x: 1, y: 1 }, | ||
{ x: 1.1, y: 1.11111 }, | ||
{ x: 1.2, y: 1.24691 }, | ||
{ x: 1.3, y: 1.40925 }, | ||
{ x: 1.4, y: 1.60073 }, | ||
{ x: 1.5, y: 1.82421 } | ||
] | ||
|
||
for (let i = 0; i < actual.length; i++) { | ||
expect(actual[i].x).toBeCloseTo(expected[i].x, 1) | ||
expect(actual[i].y).toBeCloseTo(expected[i].y, 1) | ||
} | ||
}) | ||
}) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating new variables here is unnecessary as
xStart
andyStart
are never used later. Renaming the variables tox
andy
and just using them would be better. This would also allow you to use the object shorthand syntax: