-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path01.3D_Dynamic_Wave_Animation_with_Visualization.py
56 lines (41 loc) · 1.56 KB
/
01.3D_Dynamic_Wave_Animation_with_Visualization.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
import numpy as np
import pandas as pd
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 a DataFrame to store wave data
wave_df = pd.DataFrame(data=wave_high_E_3D, index=t, columns=x)
# Create a 3D surface plot
fig = go.Figure()
surf = go.Surface(z=wave_df.iloc[0], x=x, y=t, 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_df.iloc[:i+1], x=x, y=t, 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()