Spaces:
Runtime error
Runtime error
import streamlit as st | |
import numpy as np | |
import plotly.graph_objs as go | |
st.title("Electromagnetic Wave Simulation") | |
# Set up initial values | |
c = 3e8 # Speed of light (m/s) | |
lambda_ = 1e-9 # Wavelength (m) | |
omega = 2 * np.pi * c / lambda_ # Angular frequency | |
k = omega / c # Wave number | |
E0 = 1 # Amplitude of electric field | |
B0 = E0 / c # Amplitude of magnetic field | |
t = np.linspace(0, 1e-14, 1000) # Time (s) | |
z = np.linspace(0, 1e-6, 100) # Position (m) | |
# Calculate the electric and magnetic fields | |
E = E0 * np.sin(k * z - omega * t[:, np.newaxis]) | |
B = B0 * np.sin(k * z - omega * t[:, np.newaxis]) | |
# Create a 3D surface plot | |
fig = go.Figure( | |
go.Surface(x=z, y=t, z=E.T, colorscale='Blues', showscale=False, name="Electric Field"), | |
go.Surface(x=z, y=t, z=B.T, colorscale='Reds', showscale=False, name="Magnetic Field"), | |
) | |
fig.update_layout( | |
title="Electromagnetic Wave Simulation", | |
scene=dict(xaxis_title="Position (m)", yaxis_title="Time (s)", zaxis_title="Field Strength"), | |
margin=dict(l=0, r=0, b=0, t=40), | |
height=700, | |
) | |
# Display the plot | |
st.plotly_chart(fig, use_container_width=True) | |