-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path01.Animation_3D_Position_along_string.py
52 lines (38 loc) · 1.44 KB
/
01.Animation_3D_Position_along_string.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
import numpy as np
import plotly.graph_objects as go
# Given values
linear_density_high_E = 3.09e-4 # kg/m
tension_high_E = 56.40 # N
length = 1.0 # Length of the string
time_points = 100
time_steps = 50
# Calculate wave speed using the formula: v = sqrt(FT / μ)
wave_speed_high_E = np.sqrt(tension_high_E / linear_density_high_E)
# Create a grid of points along the string
x = np.linspace(0, length, time_points)
t = np.linspace(0, 2 * np.pi, time_steps)
X, T = np.meshgrid(x, t)
# Calculate the wave motion using sine function
wave_high_E_3D = np.sin(2 * np.pi * X - wave_speed_high_E * T)
# Create the 3D surface plot
fig = go.Figure()
surf = go.Surface(x=X, y=T, z=wave_high_E_3D[:, 0], colorscale='Viridis')
fig.add_trace(surf)
# Update scene layout for 3D visualization
fig.update_layout(scene=dict(
xaxis_title='Position along string',
yaxis_title='Time',
zaxis_title='Amplitude',
))
# Create animation frames
animation_frames = []
for i in range(time_steps):
frame = go.Frame(data=[go.Surface(z=wave_high_E_3D[:, :i+1], colorscale='Viridis')])
animation_frames.append(frame)
# Add frames to figure
fig.frames = animation_frames
# Set animation settings
animation_settings = [dict(label='Play', method='animate', args=[None, dict(frame=dict(duration=100, redraw=True), fromcurrent=True)])]
fig.update_layout(updatemenus=[dict(type='buttons', showactive=False, buttons=animation_settings)])
# Show interactive 3D plot
fig.show()