Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,33 +2,76 @@ import streamlit as st
|
|
2 |
import numpy as np
|
3 |
import plotly.graph_objs as go
|
4 |
|
5 |
-
st.title(
|
6 |
-
|
7 |
-
# Set
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
import plotly.graph_objs as go
|
4 |
|
5 |
+
st.title('Electromagnetic Wave Simulation with Plotly')
|
6 |
+
|
7 |
+
# Set the default wavelength and amplitude values
|
8 |
+
wavelength_default = 1
|
9 |
+
amplitude_default = 1
|
10 |
+
|
11 |
+
# Create sliders for adjusting wavelength and amplitude
|
12 |
+
wavelength = st.slider('Wavelength', min_value=0.1, max_value=10.0, value=wavelength_default, step=0.1)
|
13 |
+
amplitude = st.slider('Amplitude', min_value=0.1, max_value=5.0, value=amplitude_default, step=0.1)
|
14 |
+
|
15 |
+
# Define the wave function for the electric field
|
16 |
+
def electric_field(x, t):
|
17 |
+
return amplitude * np.sin(2*np.pi*x/wavelength - 2*np.pi*t)
|
18 |
+
|
19 |
+
# Define the wave function for the magnetic field
|
20 |
+
def magnetic_field(x, t):
|
21 |
+
return amplitude * np.sin(2*np.pi*x/wavelength - 2*np.pi*t + np.pi/2)
|
22 |
+
|
23 |
+
# Create a plotly figure for the electric field
|
24 |
+
fig_electric = go.Figure()
|
25 |
+
|
26 |
+
# Add a line for the electric field
|
27 |
+
fig_electric.add_trace(
|
28 |
+
go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), 0),
|
29 |
+
mode='lines', name='Electric Field')
|
30 |
+
)
|
31 |
+
|
32 |
+
# Create a plotly figure for the magnetic field
|
33 |
+
fig_magnetic = go.Figure()
|
34 |
+
|
35 |
+
# Add a line for the magnetic field
|
36 |
+
fig_magnetic.add_trace(
|
37 |
+
go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1, 1000), 0),
|
38 |
+
mode='lines', name='Magnetic Field')
|
39 |
+
)
|
40 |
+
|
41 |
+
# Create a plotly figure for the wave
|
42 |
+
fig_wave = go.Figure()
|
43 |
+
|
44 |
+
# Add a line for the electric field
|
45 |
+
fig_wave.add_trace(
|
46 |
+
go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), 0),
|
47 |
+
mode='lines', name='Electric Field')
|
48 |
)
|
49 |
+
|
50 |
+
# Add a line for the magnetic field
|
51 |
+
fig_wave.add_trace(
|
52 |
+
go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1, 1000), 0),
|
53 |
+
mode='lines', name='Magnetic Field')
|
54 |
+
)
|
55 |
+
|
56 |
+
# Set the title and axis labels for the wave figure
|
57 |
+
fig_wave.update_layout(
|
58 |
+
title='Electromagnetic Wave',
|
59 |
+
xaxis_title='Position',
|
60 |
+
yaxis_title='Field Strength'
|
61 |
)
|
62 |
|
63 |
+
# Add a slider for adjusting time
|
64 |
+
t_slider = fig_wave.add_slider(
|
65 |
+
dict(steps=[dict(method='animate', args=[None, {'frame': {'duration': 50, 'redraw': True}}])],
|
66 |
+
transition={'duration': 0},
|
67 |
+
x=0, y=0,
|
68 |
+
len=1.0,
|
69 |
+
currentvalue=dict(visible=True, xanchor='left'),
|
70 |
+
font=dict(size=10, color='#666')
|
71 |
+
),
|
72 |
+
)
|
73 |
+
|
74 |
+
# Define the animation frames
|
75 |
+
frames = [go.Frame(data=[go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), t),
|
76 |
+
mode='lines', name='Electric Field'),
|
77 |
+
go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1,
|