jpoptum's picture
Update app.py
259f356
raw
history blame
2.64 kB
import streamlit as st
import numpy as np
import plotly.graph_objs as go
st.title('Electromagnetic Wave Simulation with Plotly')
# Set the default wavelength and amplitude values
wavelength_default = 1
amplitude_default = 1
# Create sliders for adjusting wavelength and amplitude
wavelength = st.slider('Wavelength', min_value=0.1, max_value=10.0, value=wavelength_default, step=0.1)
amplitude = st.slider('Amplitude', min_value=0.1, max_value=5.0, value=amplitude_default, step=0.1)
# Define the wave function for the electric field
def electric_field(x, t):
return amplitude * np.sin(2*np.pi*x/wavelength - 2*np.pi*t)
# Define the wave function for the magnetic field
def magnetic_field(x, t):
return amplitude * np.sin(2*np.pi*x/wavelength - 2*np.pi*t + np.pi/2)
# Create a plotly figure for the electric field
fig_electric = go.Figure()
# Add a line for the electric field
fig_electric.add_trace(
go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), 0),
mode='lines', name='Electric Field')
)
# Create a plotly figure for the magnetic field
fig_magnetic = go.Figure()
# Add a line for the magnetic field
fig_magnetic.add_trace(
go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1, 1000), 0),
mode='lines', name='Magnetic Field')
)
# Create a plotly figure for the wave
fig_wave = go.Figure()
# Add a line for the electric field
fig_wave.add_trace(
go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), 0),
mode='lines', name='Electric Field')
)
# Add a line for the magnetic field
fig_wave.add_trace(
go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1, 1000), 0),
mode='lines', name='Magnetic Field')
)
# Set the title and axis labels for the wave figure
fig_wave.update_layout(
title='Electromagnetic Wave',
xaxis_title='Position',
yaxis_title='Field Strength'
)
# Add a slider for adjusting time
t_slider = fig_wave.add_slider(
dict(steps=[dict(method='animate', args=[None, {'frame': {'duration': 50, 'redraw': True}}])],
transition={'duration': 0},
x=0, y=0,
len=1.0,
currentvalue=dict(visible=True, xanchor='left'),
font=dict(size=10, color='#666')
),
)
# Define the animation frames
frames = [go.Frame(data=[go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), t),
mode='lines', name='Electric Field'),
go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1,