-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlorenz.py
57 lines (50 loc) · 1.58 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
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
class Lorenz:
def __init__(self, beta):
self.sigma = 10.0
self.rho = 28.0
self.beta = beta
self.x0 = 0.1
self.y0 = 0.0
self.z0 = 0.0
self.xyz0 = [0.1, 0.0, 0.0]
self.t_span = (0, 100)
self.t_eval = np.linspace(0, 100, 10000)
def lorenzBuilder(self, t, xyz):
x, y, z = xyz
dxdt = self.sigma * (y - x)
dydt = x * (self.rho - z) - y
dzdt = x * y - self.beta * z
return [dxdt, dydt, dzdt]
def solve_lorenz(self):
sol = solve_ivp(self.lorenzBuilder, self.t_span, self.xyz0, t_eval=self.t_eval)
x, y, z = sol.y
return x, y, z
def print_first_3_solutions(self):
x, y, z = self.solve_lorenz()
print("First 3 solutions:")
print("x:", x[70])
print("y:", y[:3])
print("z:", z[:3])
def draw_attractor(self):
x, y, z = self.solve_lorenz()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z, linewidth=0.5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Atractor de Lorenz')
plt.show()
def getPosition(self, axis, position):
x , y, z = self.solve_lorenz()
if axis == "x":
return round(x[position],2)
elif axis == "y":
return round(y[position],2)
elif axis == "z":
return round(z[position],2)
else:
return "Invalid axis"