diff --git a/contents/forward_euler_method/code/golang/euler.go b/contents/forward_euler_method/code/golang/euler.go new file mode 100644 index 000000000..0f8ba1618 --- /dev/null +++ b/contents/forward_euler_method/code/golang/euler.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "math" +) + +func forwardEuler(timeStep float64, n int) []float64 { + result := make([]float64, n) + result[0] = 1 + for x := 1; x < n; x++ { + result[x] = result[x-1] - 3*result[x-1]*timeStep + } + return result +} + +func check(result []float64, threshold, timeStep float64) bool { + approx := true + for x := 0.; int(x) < len(result); x++ { + solution := math.Exp(-3. * x * timeStep) + if math.Abs(result[int(x)]-solution) > threshold { + fmt.Println(result[int(x)], solution) + approx = false + } + } + return approx +} + +func main() { + timeStep, threshold := .01, .01 + n := 100 + + result := forwardEuler(timeStep, n) + if check(result, threshold, timeStep) { + fmt.Println("All values within threshold") + } else { + fmt.Println("Value(s) not within threshold") + } +} diff --git a/contents/forward_euler_method/forward_euler_method.md b/contents/forward_euler_method/forward_euler_method.md index a0a08782e..af6da3184 100644 --- a/contents/forward_euler_method/forward_euler_method.md +++ b/contents/forward_euler_method/forward_euler_method.md @@ -123,6 +123,8 @@ Full code for the visualization follows: [import, lang:"swift"](code/swift/euler.swift) {% sample lang="f90" %} [import, lang:"fortran"](code/fortran/euler.f90) +{% sample lang="go" %} +[import, lang:"go"](code/golang/euler.go) {% endmethod %}