<jupyter_start><jupyter_text>Interact Exercise 3  Imports<jupyter_code>%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display<jupyter_output><empty_output><jupyter_text>Using interact for animation with data A [*soliton*](http://en.wikipedia.org/wiki/Soliton) is a constant velocity wave that maintains its shape as it propagates. They arise from non-linear wave equations, such has the [Korteweg–de Vries](http://en.wikipedia.org/wiki/Korteweg%E2%80%93de_Vries_equation) equation, which has the following analytical solution:$$\phi(x,t) = \frac{1}{2} c \mathrm{sech}^2 \left[ \frac{\sqrt{c}}{2} \left(x - ct - a \right) \right]$$The constant `c` is the velocity and the constant `a` is the initial location of the soliton.Define `soliton(x, t, c, a)` function that computes the value of the soliton wave for the given arguments. Your function should work when the postion `x` *or* `t` are NumPy arrays, in which case it should return a NumPy array itself.<jupyter_code>def soliton(x, t, c, a):
    """Return phi(x, t) for a soliton wave with constants c and a."""
    # YOUR CODE HERE
#     v = np.coshx-c*t-a
#     b = np.sqrt(c)/2
#     x = np.array()
#     t = np.array()
    phi_x_t = 0.5*c*(1/np.cosh(np.sqrt(c)/2*(x-c*t-a)))**2
    return phi_x_t
assert np.allclose(soliton(np.array([0]),0.0,1.0,0.0), np.array([0.5]))<jupyter_output><empty_output><jupyter_text>To create an animation of a soliton propagating in time, we are going to precompute the soliton data and store it in a 2d array. To set this up, we create the following variables and arrays:<jupyter_code>tmin = 0.0
tmax = 10.0
tpoints = 100
t = np.linspace(tmin, tmax, tpoints)

xmin = 0.0
xmax = 10.0
xpoints = 200
x = np.linspace(xmin, xmax, xpoints)

c = 1.0
a = 0.0<jupyter_output><empty_output><jupyter_text>Compute a 2d NumPy array called `phi`:* It should have a dtype of `float`.* It should have a shape of `(xpoints, tpoints)`.* `phi[i,j]` should contain the value $\phi(x[i],t[j])$.<jupyter_code># YOUR CODE HERE
phi = np.empty([xpoints, tpoints])
for i in x:
    for j in t:
        phi[i, j] = soliton(x[i], t[j], c, a)
assert phi.shape==(xpoints, tpoints)
assert phi.ndim==2
assert phi.dtype==np.dtype(float)
assert phi[0,0]==soliton(x[0],t[0],c,a)<jupyter_output><empty_output><jupyter_text>Write a `plot_soliton_data(i)` function that plots the soliton wave $\phi(x, t[i])$. Customize your plot to make it effective and beautiful.<jupyter_code>def plot_soliton_data(i=0):
    """Plot the soliton data at t[i] versus x."""
    # YOUR CODE HERE
    plt.plot(soliton(x, t[i], c, a))
f = plt.figure(figsize=(10,6))
plot_soliton_data(0)
plt.xlabel('Time')
plt.ylabel('Position')
plt.title('Soliton position vs. time')
plt.ylim(0, 0.6)
assert True # leave this for grading the plot_soliton_data function<jupyter_output><empty_output><jupyter_text>Use `interact` to animate the `plot_soliton_data` function versus time.<jupyter_code># YOUR CODE HERE
interact(plot_soliton_data, i=5);
assert True # leave this for grading the interact with plot_soliton_data cell<jupyter_output><empty_output>