jpoptum commited on
Commit
04a0096
1 Parent(s): 64ee556

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -32
app.py CHANGED
@@ -1,40 +1,34 @@
1
  import streamlit as st
2
  import numpy as np
3
- import matplotlib.pyplot as plt
4
- from mpl_toolkits.mplot3d import Axes3D
5
 
6
- # Define the time variable
7
- t = np.linspace(0, 2*np.pi, 100)
8
 
9
- # Create a meshgrid for the x and y axes
10
- x, y = np.meshgrid(np.linspace(-1, 1, 100), np.linspace(-1, 1, 100))
11
-
12
- # Define the amplitudes and frequencies of the waves
13
- amp_e = st.sidebar.slider('Amplitude of Electric Field', min_value=0.0, max_value=1.0, value=0.5, step=0.1)
14
- amp_b = st.sidebar.slider('Amplitude of Magnetic Field', min_value=0.0, max_value=1.0, value=0.5, step=0.1)
15
- freq = st.sidebar.slider('Frequency of Waves', min_value=1, max_value=10, value=5, step=1)
 
 
16
 
17
  # Calculate the electric and magnetic fields
18
- e_x = amp_e * np.cos(freq*t)
19
- e_y = np.zeros_like(e_x)
20
- e_z = np.zeros_like(e_x)
21
-
22
- b_x = np.zeros_like(e_x)
23
- b_y = amp_b * np.cos(freq*t)
24
- b_z = np.zeros_like(e_x)
25
 
26
- # Plot the electric and magnetic fields
27
- fig = plt.figure(figsize=(10, 6))
28
- ax = fig.add_subplot(111, projection='3d')
29
- ax.quiver(x, y, np.zeros_like(x), e_x, e_y, e_z, length=0.1, color='r')
30
- ax.quiver(x, y, np.zeros_like(x), b_x, b_y, b_z, length=0.1, color='b')
31
- ax.set_xlim(-1, 1)
32
- ax.set_ylim(-1, 1)
33
- ax.set_zlim(-1, 1)
34
- ax.set_xlabel('X')
35
- ax.set_ylabel('Y')
36
- ax.set_zlabel('Z')
37
- ax.view_init(elev=30, azim=120)
38
 
39
- # Display the plot in Streamlit
40
- st.pyplot(fig)
 
1
  import streamlit as st
2
  import numpy as np
3
+ import plotly.graph_objs as go
 
4
 
5
+ st.title("Electromagnetic Wave Simulation")
 
6
 
7
+ # Set up initial values
8
+ c = 3e8 # Speed of light (m/s)
9
+ lambda_ = 1e-9 # Wavelength (m)
10
+ omega = 2 * np.pi * c / lambda_ # Angular frequency
11
+ k = omega / c # Wave number
12
+ E0 = 1 # Amplitude of electric field
13
+ B0 = E0 / c # Amplitude of magnetic field
14
+ t = np.linspace(0, 1e-14, 1000) # Time (s)
15
+ z = np.linspace(0, 1e-6, 100) # Position (m)
16
 
17
  # Calculate the electric and magnetic fields
18
+ E = E0 * np.sin(k * z - omega * t[:, np.newaxis])
19
+ B = B0 * np.sin(k * z - omega * t[:, np.newaxis])
 
 
 
 
 
20
 
21
+ # Create a 3D surface plot
22
+ fig = go.Figure(
23
+ go.Surface(x=z, y=t, z=E.T, colorscale='Blues', showscale=False, name="Electric Field"),
24
+ go.Surface(x=z, y=t, z=B.T, colorscale='Reds', showscale=False, name="Magnetic Field"),
25
+ )
26
+ fig.update_layout(
27
+ title="Electromagnetic Wave Simulation",
28
+ scene=dict(xaxis_title="Position (m)", yaxis_title="Time (s)", zaxis_title="Field Strength"),
29
+ margin=dict(l=0, r=0, b=0, t=40),
30
+ height=700,
31
+ )
 
32
 
33
+ # Display the plot
34
+ st.plotly_chart(fig, use_container_width=True)