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 f_t(t):
#result = np.sin(x0-direction*t)
result = np.exp(-t**2.)
return result
def amplitude_x(x,t0):
#result = np.sin(x-direction*t0)
result = np.exp(-(x-t0)**2.)
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(-1.5*pi, 1.5*pi, 1000)
t0=-pi
#############################
#plot1
y_max = 2.
line_4, = ax[0].plot(t, f_t(t),
'r-', lw=2)
line_5, = ax[0].plot([t0,t0], [-y_max,y_max],
'b--', lw=1)
line_6, = ax[0].plot(t0, f_t(t0),
'bo', lw=1, label='$f(t_0)$')
#plot params
ax[0].set_title('$f(t)$', fontsize=16)
ax[0].set_xlabel('$t$', fontsize=16)
ax[0].set_xlim(min(t),max(t))
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_1, = ax[1].plot(x, amplitude_x(x,t0),
'b-', lw=2)
line_3, = ax[1].plot(0, amplitude_x(0,t0),
'ro', lw=1, label='$\psi(0,t_0)$')
#plot params
ax[1].set_title('$\psi(x,t=t_0)$', fontsize=16)
ax[1].set_xlabel('$x$', fontsize=16)
ax[1].set_xlim(min(x),max(x))
ax[1].set_ylim(-y_max,y_max)
ax[1].grid(True)
ax[0].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_4.set_ydata(f_t(t))
line_5.set_xdata([t0,t0])
line_6.set_xdata(t0)
line_6.set_ydata(f_t(t0))
#plot2
#############################
line_1.set_ydata(amplitude_x(x,t0))
line_3.set_ydata(amplitude_x(0,t0))
fig.canvas.draw_idle()
return
#Define control elements
###############################################
###############################################
s1=widgets.FloatSlider(
min=-1.5*pi,
max=3.*pi,
step=0.2,
value=-pi,
layout=Layout(width='500px'),
description='$t_0$',
style = {'description_width': 'initial'})
#Connect controls to plot
###############################################
###############################################
out = interactive_output(update, {'t0': s1})
#Set layout
###############################################
###############################################
box_layout = Layout(display='flex', flex_flow='column', justify_content='space-between', align_items='center')
#Display output
###############################################
###############################################
display(VBox([s1], layout=box_layout))
VBox(children=(FloatSlider(value=-3.141592653589793, description='$t_0$', layout=Layout(width='500px'), max=9.…
These plots show the relation between $f(t)$, which is the position of the end of a rope as a funciton of time, and $\psi(x,t)$, which is the amplitude of the wave generated by $f(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>''')