-
Notifications
You must be signed in to change notification settings - Fork 0
/
lorenz.py
121 lines (98 loc) · 2.21 KB
/
lorenz.py
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
import sys
from turtle import Turtle, Screen
options = {
1: "graph u-v",
2: "graph u-w",
3: "graph v-w",
4: "exit"
}
def askForOption():
print("Lorenz attractor simulator")
print("==========================")
print("Select option:\n")
for key,val in options.items():
print("\t", key, " = ", val)
print("")
opt = 0
while opt < 1 or opt > 4:
opt = int(input("option [1-4]: "))
if opt < 1 or opt > 4:
print("Invalid option")
return opt
# Begin
opt = askForOption()
if opt == 4:
print("bye")
sys.exit(0)
print("Selected option: ", options.get(opt, "Invalid option"))
max_value = 4
# Init turtle
screen = Screen()
screen.setworldcoordinates(-max_value * 100, -max_value * 100, max_value * 100, max_value * 100)
turtle = Turtle()
turtle.speed('fastest')
turtle.penup()
# Draw axis
turtle.pencolor('#dddddd')
turtle.penup()
turtle.goto(-max_value * 100, 0)
turtle.pendown()
turtle.forward(max_value * 2 * 100)
turtle.penup()
turtle.goto(0, max_value * 100)
turtle.right(90)
turtle.pendown()
turtle.forward(max_value * 2 * 100)
# Draw graph title
turtle.penup()
turtle.goto(-max_value * 100 + 8, max_value * 100 - 10)
turtle.pendown()
turtle.write(options.get(opt, ""))
# Parameters
M = 1
N = 30000
u0 = 0.82
v0 = 0.63
w0 = 0.74
s = 16.0
r = 46.5
b = 4.0
h = 0.001
fast = 5
p = 0
# Draw start values
u = u0
v = v0
w = w0
turtle.pencolor('black')
turtle.penup()
if opt == 1:
turtle.goto(round(u * 100), round(v * 100))
elif opt == 2:
turtle.goto(round(v * 100), round(w * 100))
elif opt == 3:
turtle.goto(round(u * 100), round(w * 100))
turtle.pendown()
# Draw graph
us = 0
vs = 0
ws = 0
for j in range(N):
for i in range(M):
us = u + h*s*(v - u)
vs = v + h*(r*u - v - 20.0*u*w)
ws = w + h*(5.0*u*v - b*w)
u = us
v = vs
w = ws
p = p + 1
if p % fast != 0:
continue;
if opt == 1:
turtle.goto(round(u * 100), round(v * 100))
elif opt == 2:
turtle.goto(round(v * 100), round(w * 100))
elif opt == 3:
turtle.goto(round(u * 100), round(w * 100))
# Wait click to exit
screen.exitonclick()