File tree 4 files changed +47
-0
lines changed
contents/forward_euler_method
4 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,10 @@ indent_size = 4
150
150
indent_style = space
151
151
indent_size = 4
152
152
153
+ # V
154
+ [* .v ]
155
+ indent_style = tab
156
+
153
157
# Whitespace
154
158
[* .ws ]
155
159
indent_style = space
Original file line number Diff line number Diff line change 193
193
"lang" : " ss" ,
194
194
"name" : " Scheme"
195
195
},
196
+ {
197
+ "lang" : " v" ,
198
+ "name" : " Vlang"
199
+ },
196
200
{
197
201
"lang" : " coffee" ,
198
202
"name" : " CoffeeScript"
Original file line number Diff line number Diff line change
1
+ import math
2
+
3
+ fn forward_euler (timestep f64 , n int ) []f64 {
4
+ mut res := [f64 (0.0 )].repeat (n)
5
+ res[0 ] = f64 (1 )
6
+ for x := 1 ; x < n; x++ {
7
+ res[x] = res[x- 1 ] - 3.0 * res[x- 1 ]* timestep
8
+ }
9
+ return res
10
+ }
11
+
12
+ fn check (result []f64 , threshold, timestep f64 ) bool {
13
+ mut approx := true
14
+ for x := 0 ; x < result.len; x++ {
15
+ solution := math.exp (- 3.0 * f64 (x) * timestep)
16
+ if math.abs (result[x]- solution) > threshold {
17
+ tmp := result[x]
18
+ println ("There is a mismatch: abs($tmp -$solution ) > $threshold !" )
19
+ approx = false
20
+ }
21
+ }
22
+ return approx
23
+ }
24
+
25
+ fn main () {
26
+ timestep := .01
27
+ threshold := .01
28
+ n := 100
29
+
30
+ result := forward_euler (timestep, n)
31
+
32
+ if check (result, threshold, timestep) {
33
+ println ("All values within threshold" )
34
+ } else {
35
+ println ("Value(s) not within threshold" )
36
+ }
37
+ }
Original file line number Diff line number Diff line change @@ -136,6 +136,8 @@ Full code for the visualization follows:
136
136
[ import, lang:"fortran"] ( code/fortran/euler.f90 )
137
137
{% sample lang="go" %}
138
138
[ import, lang:"go"] ( code/golang/euler.go )
139
+ {% sample lang="v" %}
140
+ [ import, lang:"v"] ( code/v/euler.v )
139
141
{% sample lang="asm-x64" %}
140
142
[ import, lang:"asm-x64"] ( code/asm-x64/euler.s )
141
143
{% sample lang="java" %}
You can’t perform that action at this time.
0 commit comments