File tree Expand file tree Collapse file tree 4 files changed +47
-0
lines changed
contents/forward_euler_method Expand file tree Collapse file tree 4 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,10 @@ indent_size = 4
150150indent_style = space
151151indent_size = 4
152152
153+ # V
154+ [* .v ]
155+ indent_style = tab
156+
153157# Whitespace
154158[* .ws ]
155159indent_style = space
Original file line number Diff line number Diff line change 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 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:
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" %}
You can’t perform that action at this time.
0 commit comments