To initialize the visualization, you may need to click "Run all initialization cells" above (see button location in figure).
#This code was authored by Alex Shvonski, Copyright 2020 MIT All Rights Reserved.
%matplotlib notebook
import ipywidgets as widgets
from IPython.display import display, clear_output
from ipywidgets import interact, interactive, interactive_output, fixed, IntSlider, HBox, Layout, Output, VBox
import numpy as np
import matplotlib.pyplot as plt
from math import pi
#Define functions
###############################################
###############################################
def amplitude_x(x,t0):
result = np.sin(x - t0)
return result
def amplitude_t(x0,t):
result = np.sin(x0 - t)
return result
#Define plot
###############################################
###############################################
fig, ax = plt.subplots(1, 2, figsize=(9.5, 5))
plt.subplots_adjust(left=0.05, bottom=None, right=1, top=None, wspace=None, hspace=1.)
x = np.linspace(0., 3.*pi, 1000)
t = np.linspace(0., 3.*pi, 1000)
x0=0.
t0=0.
#############################
#plot1
y_max = 2.
line_1, = ax[0].plot(x, amplitude_x(x,t0),
'b-', lw=2)
line_2, = ax[0].plot([x0,x0], [-y_max,y_max],
'r--', lw=1, label='$x_0$')
line_3, = ax[0].plot(x0, amplitude_x(x0,t0),
'ro', lw=1, label='$A(x_0,t_0)$')
#plot params
ax[0].set_title('Amplitude: $A(x,t=t_0)$', fontsize=16)
ax[0].set_xlabel('$x$', fontsize=16)
ax[0].set_xlim(min(x),max(x))
ax[0].set_ylim(-y_max,y_max)
ax[0].grid(True)
ax[0].legend(loc=1, fontsize=16)
plt.setp(ax[0].get_xticklabels(), fontsize=14)
plt.setp(ax[0].get_yticklabels(), fontsize=14)
#############################
#plot2
line_4, = ax[1].plot(t, amplitude_t(x0,t),
'r-', lw=2)
line_5, = ax[1].plot([t0,t0], [-y_max,y_max],
'b--', lw=1, label='$t_0$')
line_6, = ax[1].plot(t0, amplitude_t(x0,t0),
'bo', lw=1, label='$A(x_0,t_0)$')
#plot params
ax[1].set_title('Amplitude: $A(x=x_0,t)$', fontsize=16)
ax[1].set_xlabel('$t$', fontsize=16)
ax[1].set_xlim(min(x),max(x))
ax[1].set_ylim(-y_max,y_max)
ax[1].grid(True)
ax[1].legend(loc=1, fontsize=16)
plt.setp(ax[1].get_xticklabels(), fontsize=14)
plt.setp(ax[1].get_yticklabels(), fontsize=14)
#Define plot updater
###############################################
###############################################
def update(x0=2.*pi, t0=0.):
#plot1
#############################
line_1.set_ydata(amplitude_x(x,t0))
line_2.set_xdata([x0,x0])
line_3.set_xdata(x0)
line_3.set_ydata(amplitude_x(x0,t0))
#plot2
#############################
line_4.set_ydata(amplitude_t(x0,t))
line_5.set_xdata([t0,t0])
line_6.set_xdata(t0)
line_6.set_ydata(amplitude_t(x0,t0))
fig.canvas.draw_idle()
return
#Define control elements
###############################################
###############################################
s1=widgets.FloatSlider(
min=0.,
max=3.*pi,
step=0.2,
value=0.,
layout=Layout(width='500px'),
description='$x_0$',
style = {'description_width': 'initial'})
s2=widgets.FloatSlider(
min=0.,
max=3.*pi,
step=0.2,
value=0.,
layout=Layout(width='500px'),
description='$t_0$',
style = {'description_width': 'initial'})
#Connect controls to plot
###############################################
###############################################
out = interactive_output(update, {'x0': s1, 't0': s2})
#Set layout
###############################################
###############################################
box_layout = Layout(display='flex', flex_flow='column', justify_content='space-between', align_items='center')
#Display output
###############################################
###############################################
display(VBox([s1, s2], layout=box_layout))
VBox(children=(FloatSlider(value=0.0, description='$x_0$', layout=Layout(width='500px'), max=9.42477796076938,…
These plots show traveling waves of the form $A(x,t)=A\sin(kx - \omega t)$. The left plot shows the solution at a fixed time $t=t_{0}$ (as a function of position $x$) and the right plot shows the solution at a fixed position $x=x_{0}$ (as a function of time $t$).
Consider the following questions and possible actions:
#The code in this block includes content from StackOverFlow User: harshil (CC BY-SA 4.0)
#and can be found here: https://stackoverflow.com/questions/27934885/how-to-hide-code-from-cells-in-ipython-notebook-visualized-with-nbviewer
###############################################
###############################################
#Enable hidden code
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')