Skip to content

Commit 9e13c88

Browse files
Julianberquist
Julian
authored andcommittedOct 25, 2019
V forward euler (algorithm-archivists#633)
1 parent 4632697 commit 9e13c88

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed
 

‎.editorconfig

+4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ indent_size = 4
150150
indent_style = space
151151
indent_size = 4
152152

153+
# V
154+
[*.v]
155+
indent_style = tab
156+
153157
# Whitespace
154158
[*.ws]
155159
indent_style = space

‎book.json

+4
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@
193193
"lang": "ss",
194194
"name": "Scheme"
195195
},
196+
{
197+
"lang": "v",
198+
"name": "Vlang"
199+
},
196200
{
197201
"lang": "coffee",
198202
"name": "CoffeeScript"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

‎contents/forward_euler_method/forward_euler_method.md

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ Full code for the visualization follows:
136136
[import, lang:"fortran"](code/fortran/euler.f90)
137137
{% sample lang="go" %}
138138
[import, lang:"go"](code/golang/euler.go)
139+
{% sample lang="v" %}
140+
[import, lang:"v"](code/v/euler.v)
139141
{% sample lang="asm-x64" %}
140142
[import, lang:"asm-x64"](code/asm-x64/euler.s)
141143
{% sample lang="java" %}

0 commit comments

Comments
 (0)
Please sign in to comment.