Skip to content

Commit 7214c0a

Browse files
Fix code style issues in RungaKutta files
1 parent fbdbe25 commit 7214c0a

File tree

2 files changed

+108
-91
lines changed

2 files changed

+108
-91
lines changed

Maths/RungaKutta.js

+57-40
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,75 @@
66
* @param {number} yCurrent - The current y-value
77
* @param {function} differentialEquation - The differential equation to solve
88
* @returns {number} - The next y-value
9-
* @example rungeKuttaStep(0, 0.1, 1, function (x, y) { return Math.sin(x) + y; }); // returns 1.10517
9+
* @example rungeKuttaStep(0, 0.1, 1, function (x, y) { return Math.sin(x) + y; }); // returns 1.10517
1010
* @example rungeKuttaStep(0.5, 0.1, 1, function (x, y) { return Math.exp(x) - y; }); // returns 1.15233
1111
*/
12-
export function rungeKuttaStep(xCurrent, stepSize, yCurrent, differentialEquation) {
13-
// Calculate the four slopes: k1, k2, k3, k4
14-
const k1 = stepSize * differentialEquation(xCurrent, yCurrent);
15-
const k2 = stepSize * differentialEquation(xCurrent + stepSize / 2, yCurrent + k1 / 2);
16-
const k3 = stepSize * differentialEquation(xCurrent + stepSize / 2, yCurrent + k2 / 2);
17-
const k4 = stepSize * differentialEquation(xCurrent + stepSize, yCurrent + k3);
18-
19-
// Calculate the next y-value using the weighted average of the four slopes
20-
return yCurrent + (1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4);
21-
}
22-
12+
export function rungeKuttaStep(
13+
xCurrent,
14+
stepSize,
15+
yCurrent,
16+
differentialEquation
17+
) {
18+
// Calculate the four slopes: k1, k2, k3, k4
19+
const k1 = stepSize * differentialEquation(xCurrent, yCurrent)
20+
const k2 =
21+
stepSize * differentialEquation(xCurrent + stepSize / 2, yCurrent + k1 / 2)
22+
const k3 =
23+
stepSize * differentialEquation(xCurrent + stepSize / 2, yCurrent + k2 / 2)
24+
const k4 = stepSize * differentialEquation(xCurrent + stepSize, yCurrent + k3)
25+
26+
// Calculate the next y-value using the weighted average of the four slopes
27+
return yCurrent + (1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4)
28+
}
29+
2330
/**
2431
* @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.
25-
* @param {number} xStart - The starting x-value
32+
* @param {number} xStart - The starting x-value
2633
* @param {number} xEnd - The ending x-value
2734
* @param {number} stepSize - The step size
2835
* @param {number} yStart - The starting y-value
2936
* @param {function} differentialEquation - The differential equation to solve
3037
* @returns {Array} - An array of points (x, y) for the complete iteration from xStart to xEnd
3138
* @see https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
32-
* @example rungeKuttaFull(0, 1, 0.2, 1, function (x, y) { return Math.sin(x) + y; });
33-
* [{ x: 0, y: 1 },
34-
* { x: 0.2, y: 1.22140 },
35-
* { x: 0.4, y: 1.53659 },
36-
* { x: 0.6, y: 1.95837 },
37-
* { x: 0.8, y: 2.50487 },
39+
* @example rungeKuttaFull(0, 1, 0.2, 1, function (x, y) { return Math.sin(x) + y; });
40+
* [{ x: 0, y: 1 },
41+
* { x: 0.2, y: 1.22140 },
42+
* { x: 0.4, y: 1.53659 },
43+
* { x: 0.6, y: 1.95837 },
44+
* { x: 0.8, y: 2.50487 },
3845
* { x: 1.0, y: 3.20155 }]
3946
*/
40-
export function rungeKuttaFull(xStart, xEnd, stepSize, yStart, differentialEquation) {
41-
// Collect all the points (x, y) for the complete iteration from xStart to xEnd
42-
const points = [{ x: xStart, y: yStart }];
43-
let yCurrent = yStart;
44-
let xCurrent = xStart;
45-
46-
while (xCurrent < xEnd) {
47-
// Runge-Kutta method for the next step
48-
49-
// Check if the next step will exceed xEnd and adjust the stepSize accordingly
50-
if (xCurrent + stepSize > xEnd) {
51-
stepSize = xEnd - xCurrent;
52-
}
47+
export function rungeKuttaFull(
48+
xStart,
49+
xEnd,
50+
stepSize,
51+
yStart,
52+
differentialEquation
53+
) {
54+
// Collect all the points (x, y) for the complete iteration from xStart to xEnd
55+
const points = [{ x: xStart, y: yStart }]
56+
let yCurrent = yStart
57+
let xCurrent = xStart
58+
59+
while (xCurrent < xEnd) {
60+
// Runge-Kutta method for the next step
5361

54-
yCurrent = rungeKuttaStep(xCurrent, stepSize, yCurrent, differentialEquation);
55-
xCurrent += stepSize;
56-
57-
// Push the new point to the points array
58-
points.push({ x: xCurrent, y: yCurrent });
62+
// Check if the next step will exceed xEnd and adjust the stepSize accordingly
63+
if (xCurrent + stepSize > xEnd) {
64+
stepSize = xEnd - xCurrent
5965
}
60-
61-
return points;
66+
67+
yCurrent = rungeKuttaStep(
68+
xCurrent,
69+
stepSize,
70+
yCurrent,
71+
differentialEquation
72+
)
73+
xCurrent += stepSize
74+
75+
// Push the new point to the points array
76+
points.push({ x: xCurrent, y: yCurrent })
77+
}
78+
79+
return points
6280
}
63-

Maths/test/RungaKutta.test.js

+51-51
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
import { rungeKuttaStep, rungeKuttaFull } from '../RungaKutta';
2-
3-
describe("rungeKuttaStep", () => {
4-
it("should calculate the next y value correctly for simple linear function f(x, y) = x + y", () => {
5-
const yNext = rungeKuttaStep(0, 0.1, 1, (x, y) => x + y);
6-
expect(yNext).toBeCloseTo(1.1103, 2); // Adjusted expected value and precision
7-
});
8-
9-
it("should calculate the next y value correctly for simple product function f(x, y) = x * y", () => {
10-
const yNext = rungeKuttaStep(1, 0.1, 2, (x, y) => x * y);
11-
expect(yNext).toBeCloseTo(2.22, 2); // Adjusted expected value and precision
12-
});
13-
});
14-
15-
describe("rungeKuttaFull", () => {
16-
it("should return all the points found for simple linear function f(x, y) = x + y", () => {
17-
const actual = rungeKuttaFull(0, 0.5, 0.1, 1, (x, y) => x + y);
18-
19-
const expected = [
20-
{ x: 0, y: 1 },
21-
{ x: 0.1, y: 1.11034 },
22-
{ x: 0.2, y: 1.23272 },
23-
{ x: 0.3, y: 1.36862 },
24-
{ x: 0.4, y: 1.58356 },
25-
{ x: 0.5, y: 1.78944 },
26-
];
27-
28-
for (let i = 0; i < actual.length; i++) {
29-
expect(actual[i].x).toBeCloseTo(expected[i].x, 1);
30-
expect(actual[i].y).toBeCloseTo(expected[i].y, 1);
31-
}
32-
});
33-
34-
it("should return all the points found for simple product function f(x, y) = x * y", () => {
35-
const actual = rungeKuttaFull(1, 1.5, 0.1, 1, (x, y) => x * y);
36-
37-
const expected = [
38-
{ x: 1, y: 1 },
39-
{ x: 1.1, y: 1.11111 },
40-
{ x: 1.2, y: 1.24691 },
41-
{ x: 1.3, y: 1.40925 },
42-
{ x: 1.4, y: 1.60073 },
43-
{ x: 1.5, y: 1.82421 },
44-
];
45-
46-
for (let i = 0; i < actual.length; i++) {
47-
expect(actual[i].x).toBeCloseTo(expected[i].x, 1);
48-
expect(actual[i].y).toBeCloseTo(expected[i].y, 1);
49-
}
50-
});
51-
});
1+
import { rungeKuttaStep, rungeKuttaFull } from '../RungaKutta'
2+
3+
describe('rungeKuttaStep', () => {
4+
it('should calculate the next y value correctly for simple linear function f(x, y) = x + y', () => {
5+
const yNext = rungeKuttaStep(0, 0.1, 1, (x, y) => x + y)
6+
expect(yNext).toBeCloseTo(1.1103, 2) // Adjusted expected value and precision
7+
})
8+
9+
it('should calculate the next y value correctly for simple product function f(x, y) = x * y', () => {
10+
const yNext = rungeKuttaStep(1, 0.1, 2, (x, y) => x * y)
11+
expect(yNext).toBeCloseTo(2.22, 2) // Adjusted expected value and precision
12+
})
13+
})
14+
15+
describe('rungeKuttaFull', () => {
16+
it('should return all the points found for simple linear function f(x, y) = x + y', () => {
17+
const actual = rungeKuttaFull(0, 0.5, 0.1, 1, (x, y) => x + y)
18+
19+
const expected = [
20+
{ x: 0, y: 1 },
21+
{ x: 0.1, y: 1.11034 },
22+
{ x: 0.2, y: 1.23272 },
23+
{ x: 0.3, y: 1.36862 },
24+
{ x: 0.4, y: 1.58356 },
25+
{ x: 0.5, y: 1.78944 }
26+
]
27+
28+
for (let i = 0; i < actual.length; i++) {
29+
expect(actual[i].x).toBeCloseTo(expected[i].x, 1)
30+
expect(actual[i].y).toBeCloseTo(expected[i].y, 1)
31+
}
32+
})
33+
34+
it('should return all the points found for simple product function f(x, y) = x * y', () => {
35+
const actual = rungeKuttaFull(1, 1.5, 0.1, 1, (x, y) => x * y)
36+
37+
const expected = [
38+
{ x: 1, y: 1 },
39+
{ x: 1.1, y: 1.11111 },
40+
{ x: 1.2, y: 1.24691 },
41+
{ x: 1.3, y: 1.40925 },
42+
{ x: 1.4, y: 1.60073 },
43+
{ x: 1.5, y: 1.82421 }
44+
]
45+
46+
for (let i = 0; i < actual.length; i++) {
47+
expect(actual[i].x).toBeCloseTo(expected[i].x, 1)
48+
expect(actual[i].y).toBeCloseTo(expected[i].y, 1)
49+
}
50+
})
51+
})

0 commit comments

Comments
 (0)