/
henon.go
160 lines (133 loc) · 2.89 KB
/
henon.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bufio"
"flag"
"fmt"
"github.com/gonum/blas/goblas"
mat "github.com/gonum/matrix/mat64"
"math"
"os"
)
func init() {
mat.Register(goblas.Blas{})
}
type fm struct {
mat.Matrix
margin int
}
func (m fm) Format(fs fmt.State, c rune) {
if c == 'v' && fs.Flag('#') {
fmt.Fprintf(fs, "%#v", m.Matrix)
return
}
mat.Format(m.Matrix, m.margin, '.', fs, c)
}
func henon(a, b, x, y float64) (xn, yn float64) {
xn = 1 - a*x*x + y
yn = b * x
return xn, yn
}
func Jcb(a, b, x float64) *mat.Dense {
jcb := mat.NewDense(2, 2, []float64{-2 * a * x, 1, b, 0})
return jcb
}
func MatrixLength(m mat.Matrix) float64 {
tmp := &mat.Dense{}
tmp.MulElem(m, m)
res := math.Sqrt(tmp.Sum())
return res
}
func PhaseGraph(a, b float64) {
para_f, _ := os.Create("henon_phase.dat")
defer para_f.Close()
para_fb := bufio.NewWriter(para_f)
num := 10000 * 20 * 10
x, y := 0.1, 0.3
for i := 0; i < 1000; i++ {
x, y = henon(a, b, x, y)
}
for i := 0; i < num; i++ {
x, y = henon(a, b, x, y)
para_fb.WriteString(fmt.Sprintf("%d %f\n", i, x))
}
para_fb.Flush()
}
func LyapunovExponents() {
xf, _ := os.Create("henon.dat")
defer xf.Close()
xfb := bufio.NewWriter(xf)
dxf, _ := os.Create("henon_ly.dat")
defer dxf.Close()
dxfb := bufio.NewWriter(dxf)
zdxf, _ := os.Create("henon_ly_zero.dat")
defer zdxf.Close()
zdxfb := bufio.NewWriter(zdxf)
// for initial condition
a, b := 0.0, 0.3
scale := 10000
// for the Lyapunov
eb := 1.0 / math.Sqrt(2)
e0 := mat.NewDense(2, 1, []float64{-eb, eb})
f0 := mat.NewDense(2, 1, []float64{eb, eb})
ze := 0.005
x0, y0 := 0.1, 0.3
fmt.Printf("x0: %f, y0: %f \n", x0, y0)
num := 200
for idx := 0; a < 1.5; idx++ {
a = a + 1.0/float64(scale)
e0_sum := 0.0
x, y := x0, y0
for i := 0; i < num; i++ {
x, y = henon(a, b, x, y)
}
for i := 0; i < num; i++ {
// for the ly
// Step 2
tmp := &mat.Dense{}
jcb := Jcb(a, b, x)
// calculate the error
e1 := &mat.Dense{}
e1.Mul(jcb, e0)
f1 := &mat.Dense{}
f1.Mul(jcb, f0)
// Step 3
e0_sum += math.Log(MatrixLength(e1))
// Step 4
tmp.Reset()
tmp.TCopy(f1)
tmp.Mul(tmp, e1)
fe := tmp.Sum()
tmp.Reset()
tmp.MulElem(e1, e1)
ee := tmp.Sum()
tmp.Reset()
tmp.Scale(fe/ee, e1)
f1.Sub(f1, tmp)
// normorlize
f0.Scale(1.0/MatrixLength(f1), f1)
e0.Scale(1.0/MatrixLength(e1), e1)
x, y = henon(a, b, x, y)
xfb.WriteString(fmt.Sprintf("%f %f\n", a, x))
}
ly := e0_sum / float64(num)
dxfb.WriteString(fmt.Sprintf("%f %f\n", a, ly))
if math.Abs(ly) < ze {
zdxfb.WriteString(fmt.Sprintf("%f %f\n", a, ly))
}
}
zdxfb.Flush()
dxfb.Flush()
xfb.Flush()
}
func PhaseMain() {
var a, b float64
flag.Float64Var(&a, "a", 0.1, "para for logistic map")
flag.Float64Var(&b, "b", 0.3, "para for logistic map")
flag.Parse()
fmt.Printf("Para a: %f, b: %f\n", a, b)
PhaseGraph(a, b)
}
func main() {
// LyapunovExponents()
PhaseMain()
}