File tree 2 files changed +33
-0
lines changed
contents/forward_euler_method
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ function forwardEuler ( timeStep , n ) {
2
+ const arr = [ 1 ] ;
3
+ for ( let i = 1 ; i <= n ; i ++ ) {
4
+ arr [ i ] = arr [ i - 1 ] - 3 * arr [ i - 1 ] * timeStep ;
5
+ }
6
+ return arr ;
7
+ }
8
+
9
+ function checkEuler ( arr , timeStep , threshold ) {
10
+ let isApprox = true ;
11
+ arr . forEach ( ( _value , i ) => {
12
+ const solution = Math . exp ( - 3 * timeStep * i ) ;
13
+
14
+ if ( Math . abs ( arr [ i ] - solution ) > threshold ) {
15
+ console . log ( arr [ i ] , solution ) ;
16
+ isApprox = false ;
17
+ }
18
+ } ) ;
19
+ return isApprox ;
20
+ }
21
+
22
+ function main ( ) {
23
+ const timeStep = 0.01 ;
24
+ const threshold = 0.01 ;
25
+ const n = 100 ;
26
+ const eulerResult = forwardEuler ( timeStep , n ) ;
27
+ const checkResult = checkEuler ( eulerResult , timeStep , threshold ) ;
28
+ console . log ( checkResult ) ;
29
+ }
30
+
31
+ main ( ) ;
Original file line number Diff line number Diff line change @@ -122,6 +122,8 @@ Full code for the visualization follows:
122
122
[ import, lang:"matlab"] ( code/matlab/euler.m )
123
123
{% sample lang="swift" %}
124
124
[ import, lang:"swift"] ( code/swift/euler.swift )
125
+ {% sample lang="js" %}
126
+ [ import, lang:"javascript"] ( code/javascript/euler.js )
125
127
{% sample lang="f90" %}
126
128
[ import, lang:"fortran"] ( code/fortran/euler.f90 )
127
129
{% sample lang="go" %}
You can’t perform that action at this time.
0 commit comments