-
Notifications
You must be signed in to change notification settings - Fork 114
/
01.3D_Wave_Visualization_and_Interactive_Learning.py
129 lines (111 loc) · 4.52 KB
/
01.3D_Wave_Visualization_and_Interactive_Learning.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
122
123
124
125
126
127
128
129
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import plotly.graph_objs as go
from ipywidgets import interactive, FloatSlider, HTML
# Generate pulse data
x = np.linspace(0, 10, 500)
pulse = np.exp(-0.2 * (x - 3) ** 2) * np.sin(4 * np.pi * x)
# Create pulse animation using Matplotlib
fig, ax = plt.subplots()
line, = ax.plot(x, pulse)
def animate(t):
line.set_ydata(np.roll(pulse, int(t * 20)))
return line,
ani = animation.FuncAnimation(fig, animate, frames=np.linspace(0, 10, 200), interval=50, blit=True)
plt.close() # Close the initial figure
def sinusoidal_wave(x, t, A, k, omega, phi):
return A * np.sin(k * x - omega * t + phi)
x_values = np.linspace(0, 10, 500)
t_values = np.linspace(0, 2, 200)
A = 1.0
k = 2 * np.pi / 5
omega = 2 * np.pi / 2
phi = 0.0
# Calculate wave data
wave_data = np.array([[sinusoidal_wave(x, t, A, k, omega, phi) for x in x_values] for t in t_values])
# Create an interactive HTML element for displaying information
info_text = HTML()
info_text.value = "<b>Select different parameters to visualize the wave behavior.</b>"
def plot_wave(amplitude, wave_number, angular_frequency, initial_phase):
fig = go.Figure()
for t in t_values:
y_values = sinusoidal_wave(x_values, t, amplitude, wave_number, angular_frequency, initial_phase)
fig.add_trace(go.Scatter(x=x_values, y=y_values, mode='lines', name=f't = {t:.2f}'))
fig.update_layout(title='Interactive Sinusoidal Wave',
xaxis_title='x', yaxis_title='y',
xaxis_range=[0, 10], yaxis_range=[-1.5, 1.5],
annotations=[dict(text="Wavelength (λ)", x=4.5, y=-1.7, font_size=12, showarrow=False),
dict(text="Amplitude (A)", x=7.5, y=1.2, font_size=12, showarrow=False)])
fig.update_xaxes(title_text="x (Distance)")
fig.update_yaxes(title_text="y (Amplitude)")
info_text.value = (
f"<b>Wave Characteristics:</b><br>"
f"Amplitude (A): {amplitude}<br>"
f"Wave Number (k): {wave_number:.2f}<br>"
f"Angular Frequency (ω): {angular_frequency:.2f}<br>"
f"Initial Phase (ϕ): {initial_phase:.2f}"
)
fig.show()
# Create interactive sliders for user input
interactive_plot = interactive(plot_wave,
amplitude=FloatSlider(min=0.1, max=2, step=0.1, value=A),
wave_number=FloatSlider(min=0.5, max=5, step=0.5, value=k),
angular_frequency=FloatSlider(min=0.5, max=5, step=0.5, value=omega),
initial_phase=FloatSlider(min=0, max=2 * np.pi, step=0.1, value=phi))
# Arrange layout for interactive plot and information
output = interactive_plot.children[-1]
output.layout.height = '350px'
layout = go.Layout(
title="Wave Visualization and Interactive Learning",
grid={"rows": 1, "columns": 2},
annotations=[
dict(text="Amplitude (A)", x=0.2, y=0.7, font_size=16, showarrow=False),
dict(text="Wavelength (λ)", x=0.9, y=0.7, font_size=16, showarrow=False)
]
)
# Display the complete layout with interactive plot and information
final_layout = go.FigureWidget(layout=layout)
for t in t_values:
y_values = sinusoidal_wave(x_values, t, A, k, omega, phi)
trace = go.Scatter(x=x_values, y=y_values, mode='lines', name=f't = {t:.2f}')
final_layout.add_trace(trace)
final_layout.update_layout({'updatemenus': None})
final_layout.layout.update(updatemenus=[
{
'type': 'buttons',
'showactive': False,
'buttons': [
{
'label': 'Wave Visualization',
'method': 'animate',
'args': [None, {'frame': {'duration': 50, 'redraw': True}, 'fromcurrent': True}],
}
]
},
{
'type': 'buttons',
'showactive': False,
'buttons': [
{
'label': 'Pause',
'method': 'animate',
'args': [[None], {'frame': {'duration': 0, 'redraw': False}, 'mode': 'immediate',
'transition': {'duration': 0}}],
}
]
},
{
'type': 'buttons',
'showactive': False,
'buttons': [
{
'label': 'Wave Characteristics',
'method': 'relayout',
'args': [{'annotations': [{'text': info_text.value, 'x': 0.5, 'y': 1.2, 'showarrow': False,
'font': {'size': 12}}]}]
}
]
}
])
final_layout.show()