-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave.py
More file actions
128 lines (103 loc) · 4.23 KB
/
wave.py
File metadata and controls
128 lines (103 loc) · 4.23 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
119
120
121
122
123
124
125
126
127
128
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Wave parameters
amplitud = 1
longitud_onda = 2 * np.pi
velocidad = 1
frecuencia = velocidad / longitud_onda
k = 2 * np.pi / longitud_onda
omega = 2 * np.pi * frecuencia
# Compute period of the wave
T = 2 * np.pi / omega # Period of the wave
# Animation settings
fps = 30 # frames per second
cycles = 2 # number of full cycles in the animation
duration = T * cycles # total duration in seconds
total_frames = int(duration * fps) # total frames matching the period
# x-axis
x = np.linspace(0, 4 * np.pi, 1000)
# Three specific points to track (non-uniform positions)
x1 = np.pi # 180 degrees
x2 = 1.3 * np.pi # ~234 degrees
x3 = 2.2 * np.pi # ~396 degrees
# Figure and axis
fig, (ax_wave, ax_circle) = plt.subplots(1, 2, figsize=(12, 4))
# --- Wave plot ---
line, = ax_wave.plot(x, np.sin(k * x), label='Onda viajera')
point1, = ax_wave.plot([], [], 'ro', label='Punto 1')
point2, = ax_wave.plot([], [], 'go', label='Punto 2')
point3, = ax_wave.plot([], [], 'bo', label='Punto 3')
vline1 = ax_wave.axvline(x1, color='r', linestyle='--', alpha=0.5)
vline2 = ax_wave.axvline(x2, color='g', linestyle='--', alpha=0.5)
vline3 = ax_wave.axvline(x3, color='b', linestyle='--', alpha=0.5)
ax_wave.set_ylim(-1.5, 1.5)
ax_wave.set_xlim(0, 4 * np.pi)
ax_wave.set_xlabel('x')
ax_wave.set_ylabel('Amplitud')
ax_wave.set_title('Onda viajera')
ax_wave.legend()
# --- Circle plot ---
circle1 = plt.Circle((0, 0), amplitud, color='r', fill=False, linestyle='--')
circle2 = plt.Circle((0, 0), amplitud, color='g', fill=False, linestyle='--')
circle3 = plt.Circle((0, 0), amplitud, color='b', fill=False, linestyle='--')
particle1, = ax_circle.plot([], [], 'ro')
particle2, = ax_circle.plot([], [], 'go')
particle3, = ax_circle.plot([], [], 'bo')
projection1_line, = ax_circle.plot([], [], 'r--', alpha=0.5)
projection2_line, = ax_circle.plot([], [], 'g--', alpha=0.5)
projection3_line, = ax_circle.plot([], [], 'b--', alpha=0.5)
radius1_line, = ax_circle.plot([], [], 'r-', alpha=0.7)
radius2_line, = ax_circle.plot([], [], 'g-', alpha=0.7)
radius3_line, = ax_circle.plot([], [], 'b-', alpha=0.7)
ax_circle.add_patch(circle1)
ax_circle.add_patch(circle2)
ax_circle.add_patch(circle3)
ax_circle.set_xlim(-1.2, 1.2)
ax_circle.set_ylim(-1.2, 1.2)
ax_circle.set_aspect('equal')
ax_circle.set_title('Movimiento circular')
ax_circle.set_xticks([])
ax_circle.set_yticks([])
# Update function for animation
def actualizar(frame):
t = frame / fps # simulated time using correct fps
y = amplitud * np.sin(k * x - omega * t)
line.set_ydata(y)
# Update tracked points on the wave
y1 = amplitud * np.sin(k * x1 - omega * t)
y2 = amplitud * np.sin(k * x2 - omega * t)
y3 = amplitud * np.sin(k * x3 - omega * t)
point1.set_data([x1], [y1])
point2.set_data([x2], [y2])
point3.set_data([x3], [y3])
# Calculate angles for the rotating particles
theta1 = k * x1 - omega * t
theta2 = k * x2 - omega * t
theta3 = k * x3 - omega * t
# Update rotating particles
x_circ1 = amplitud * np.cos(theta1)
y_circ1 = amplitud * np.sin(theta1)
x_circ2 = amplitud * np.cos(theta2)
y_circ2 = amplitud * np.sin(theta2)
x_circ3 = amplitud * np.cos(theta3)
y_circ3 = amplitud * np.sin(theta3)
particle1.set_data([x_circ1], [y_circ1])
particle2.set_data([x_circ2], [y_circ2])
particle3.set_data([x_circ3], [y_circ3])
# Vertical projection lines (from particle to x-axis)
projection1_line.set_data([x_circ1, x_circ1], [y_circ1, 0])
projection2_line.set_data([x_circ2, x_circ2], [y_circ2, 0])
projection3_line.set_data([x_circ3, x_circ3], [y_circ3, 0])
# Radius lines (from center to particle)
radius1_line.set_data([0, x_circ1], [0, y_circ1])
radius2_line.set_data([0, x_circ2], [0, y_circ2])
radius3_line.set_data([0, x_circ3], [0, y_circ3])
return (line, point1, point2, point3,
particle1, particle2, particle3,
projection1_line, projection2_line, projection3_line,
radius1_line, radius2_line, radius3_line)
# Create animation with aligned frames to period
ani = animation.FuncAnimation(fig, actualizar, frames=total_frames, interval=1000/fps, blit=True)
plt.tight_layout()
plt.show()