forked from cpmech/gosl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathode_hweq11.go
More file actions
118 lines (96 loc) · 3.53 KB
/
ode_hweq11.go
File metadata and controls
118 lines (96 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
package main
import (
"math"
"github.com/cpmech/gosl/io"
"github.com/cpmech/gosl/la"
"github.com/cpmech/gosl/ode"
"github.com/cpmech/gosl/plt"
"github.com/cpmech/gosl/utl"
)
func main() {
// title
io.Pf("Hairer-Wanner VII-p2 Eq.(1.1)")
// constants
λ := -50.0
dx := 1.875 / 50.0
xf := 1.5
atol := 1e-4
rtol := 1e-4
numJac := false
saveStep := true
saveDense := false
// ode function: f(x,y) = dy/dx
fcn := func(f la.Vector, dx, x float64, y la.Vector) {
f[0] = λ*y[0] - λ*math.Cos(x)
}
// Jacobian function: J = df/dy
jac := func(dfdy *la.Triplet, dx, x float64, y la.Vector) {
if dfdy.Max() == 0 {
dfdy.Init(1, 1, 1)
}
dfdy.Start()
dfdy.Put(0, 0, λ)
}
// initial values
ndim := 1
y := la.NewVector(ndim)
y[0] = 0.0
// FwEuler
io.Pf("\n------------ Forward-Euler ------------------\n")
fixedStep := true
stat1, out1 := ode.Solve("fweuler", fcn, jac, y.GetCopy(), xf, dx, atol, rtol, numJac, fixedStep, saveStep, saveDense)
stat1.Print(false)
// BwEuler
io.Pf("\n------------ Backward-Euler ------------------\n")
fixedStep = true
stat2, out2 := ode.Solve("bweuler", fcn, jac, y.GetCopy(), xf, dx, atol, rtol, numJac, fixedStep, saveStep, saveDense)
stat2.Print(false)
// MoEuler
io.Pf("\n------------ Modified-Euler ------------------\n")
fixedStep = false
stat3, out3 := ode.Solve("moeuler", fcn, jac, y.GetCopy(), xf, dx, atol, rtol, numJac, fixedStep, saveStep, saveDense)
stat3.Print(true)
// DoPri5
io.Pf("\n------------ Dormand-Prince5 -----------------\n")
fixedStep = false
stat4, out4 := ode.Solve("dopri5", fcn, jac, y.GetCopy(), xf, dx, atol, rtol, numJac, fixedStep, saveStep, saveDense)
stat4.Print(true)
// DoPri8
io.Pf("\n------------ Dormand-Prince8 -----------------\n")
fixedStep = false
stat5, out5 := ode.Solve("dopri8", fcn, jac, y.GetCopy(), xf, dx, atol, rtol, numJac, fixedStep, saveStep, saveDense)
stat5.Print(true)
// Radau5
io.Pf("\n------------ Radau5 --------------------------\n")
fixedStep = false
stat6, out6 := ode.Solve("radau5", fcn, jac, y.GetCopy(), xf, dx, atol, rtol, numJac, fixedStep, saveStep, saveDense)
stat6.Print(true)
// analytical solution
npts := 201
xx := utl.LinSpace(0, xf, npts)
yy := utl.GetMapped(xx, func(x float64) float64 {
return -λ * (math.Sin(x) - λ*math.Cos(x) + λ*math.Exp(λ*x)) / (λ*λ + 1.0)
})
// plot
plt.Reset(true, &plt.A{WidthPt: 500, Prop: 1.7})
plt.Subplot(3, 1, 1)
plt.Plot(xx, yy, &plt.A{C: "grey", Ls: "-", Lw: 5, L: "analytical", NoClip: true})
plt.Plot(out1.GetStepX(), out1.GetStepY(0), &plt.A{L: "fweuler", C: "k", M: ".", Ls: ":"})
plt.Plot(out2.GetStepX(), out2.GetStepY(0), &plt.A{L: "bweuler", C: "r", M: ".", Ls: "--"})
plt.Gll("$x$", "$y$", nil)
plt.Subplot(3, 1, 2)
plt.Plot(xx, yy, &plt.A{C: "grey", Ls: "-", Lw: 5, L: "analytical", NoClip: true})
plt.Plot(out3.GetStepX(), out3.GetStepY(0), &plt.A{L: "moeuler", C: "k", M: ".", Ls: ":"})
plt.Plot(out4.GetStepX(), out4.GetStepY(0), &plt.A{L: "dopri5", C: "r", M: ".", Ls: "none"})
plt.Gll("$x$", "$y$", nil)
plt.Subplot(3, 1, 3)
plt.Plot(xx, yy, &plt.A{C: "grey", Ls: "-", Lw: 5, L: "analytical", NoClip: true})
plt.Plot(out5.GetStepX(), out5.GetStepY(0), &plt.A{L: "dopri8", C: "k", M: "o", Ls: ":"})
plt.Plot(out6.GetStepX(), out6.GetStepY(0), &plt.A{L: "radau5", C: "r", M: "o", Ls: "-", Void: true})
plt.Gll("$x$", "$y$", nil)
plt.Save("/tmp/gosl", "ode_hweq11")
}