forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EulerMethod.js
30 lines (27 loc) · 1.51 KB
/
EulerMethod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* In mathematics and computational science, the Euler method (also called forward Euler method) is a first-order
* numerical procedure for solving ordinary differential equations (ODEs) with a given initial value. It is the most
* basic explicit method for numerical integration of ordinary differential equations. 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: y_n+1 = y_n + stepSize * f(x_n, y_n).
*
* (description adapted from https://en.wikipedia.org/wiki/Euler_method)
* @see https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
*/
export function eulerStep (xCurrent, stepSize, yCurrent, differentialEquation) {
// calculates the next y-value based on the current value of x, y and the stepSize
return yCurrent + stepSize * differentialEquation(xCurrent, yCurrent)
}
export function eulerFull (xStart, xEnd, stepSize, yStart, differentialEquation) {
// loops through all the steps until xEnd is reached, adds a point for each step and then returns all the points
const points = [{ x: xStart, y: yStart }]
let yCurrent = yStart
let xCurrent = xStart
while (xCurrent < xEnd) {
// Euler method for next step
yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation)
xCurrent += stepSize
points.push({ x: xCurrent, y: yCurrent })
}
return points
}