FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Maths/EulerMethod.js at master · sobitd/JavaScript · GitHub
sobitd
/
JavaScript
Public
forked from
TheAlgorithms/JavaScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Maths
/
EulerMethod.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (27 loc) · 1.51 KB
Breadcrumbs
JavaScript
/
Maths
/
EulerMethod.js
Copy path
File metadata and controls
30 lines (27 loc) · 1.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
}
Back
|
FazBrowse Home
|
New Git URL