Spaces:
Runtime error
Runtime error
Create simluation.py
Browse files- simluation.py +40 -0
simluation.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|