<jupyter_start><jupyter_text>Fill in any place that says ` YOUR CODE HERE` or YOUR ANSWER HERE, as well as your name and collaborators below.Grading for pre-lecture assignments is all or nothing. Partial credit is available for in-class assignments and checkpoints, but **only when code is commented**.<jupyter_code>NAME = ""
COLLABORATORS = ""<jupyter_output><empty_output><jupyter_text>---<jupyter_code>import grading_helper as _test
# Space for imports, utility functions, etc.

# YOUR CODE HERE<jupyter_output><empty_output><jupyter_text>EigenfrequenciesConsider a chain of four identical masses connected by five ideal springs. We will neglect the effects of gravity for simplicity. Your task is to make an animation that shows the chain vibrating horizontally at each of its four *eigenfrequencies*.Let $x_0$, $x_1$, $x_2$, and $x_3$ be the horizontal displacement of each mass from equilibrium, numbered from left to right. (For example, in the figure above, $x_0 = x_1 = x_2 = x_3 = 0$. Verify that the potential energy of the chain is$$U = \tfrac12 k x_0^2 + \tfrac12 k(x_1-x_0)^2 + \tfrac12 k(x_2-x_1)^2 + \tfrac12 k(x_3-x_2)^2 + \tfrac12 k x_3^2\,,$$where we have one term for each of the five springs.Using$$F_j=-\frac{\partial U}{\partial x_j} = m\ddot{x_j}\,,$$write down the four equations of motion that govern the four masses. To find the eigenfrequencies $\omega$ of the system, we exploit the fact that the solution to a simple harmonic oscillator takes the form $\ddot{x_i} = -\omega^2 x_i$.Put the equations you wrote above into the form$$\mathbf{Mx}=\frac{\omega^2}{\omega_0^2}\mathbf{x}\,,$$where $\mathbf{M}$ is a matrix with the coefficients from the equation of motion and $\omega_0=\sqrt{k/m}$ is the natural frequency of each spring. We'll choose units where $\omega_0 = 1$. We are thus left with an eigenvalue problem!$$\mathbf{Mx}=\omega^2\mathbf{x}\,$$where $\omega$ are the angular *eigenfrequencies* (be careful, they are the square root of the eigenvalues and they should all be real).But what is the significance of these eigenfrequencies? Recall that the time-dependent solution to a simple harmonic oscillator is the real part of$$x_j(t) = A_j e^{\mathrm{i}\omega t}\,,$$where $A_j$ is the complex amplitude. (The complex nature of $A_j$ allows for different phase&mdash;the magnitude of $A_j$ is what we normally call "amplitude".) In other words, when the chain is driven at an arbitrary frequency $\omega$, all four masses move with that same frequency, but different amplitudes and phases. In general, the values of these amplitudes and phases are not obvious.However, if the system is driven at an eigenfrequency, the resulting amplitudes are all real, and the amplitudes of the four masses are exactly the elements of the associated eigenvector.Make an animation that shows the chain moving at each of its four eigenfrequencies (this would be a good place to use `subplot`). Your animation doesn't need to loop smoothly, but should be long enough that the motions of the masses are clear. It should also be clear that the frequencies for the four cases are not all the same.> Not only does the result look cool, but *any* motion of these four masses can be described as a linear combination of the four motions from your animation. In effect, these are the four unique ways the masses can move.<jupyter_code># To help you with the visualization, here is some example code that draws 
# a spring using matplotlib. Feel free to use/modify this code as you see fit.

import numpy as np
import matplotlib.pyplot as plt


def draw_spring(left, right):
    """
    Draws a horizontal spring.
    Inputs: left  = x-coord of left side
            right = x-coord of right side
    """
    dy = 0.5
    x = np.linspace(left, right, 23)
    y = np.array([0]*4 + [dy, 0, -dy, 0]*4 + [0]*3) # zig-zag shape for spring
    spring = plt.Line2D(x, y, linestyle="-", color="gray", zorder=-1)
    ax = plt.gca()                      #                        ^
    ax.add_line(spring)                 # draw behind everything else


def draw_chain(x_offsets):
    """
    Draw a chain of five springs and four masses.
    Input: x_offsets = list of displacements from equilibrium
    """
    x = np.arange(2, 10, 2) + x_offsets
    
    # draw springs
    draw_spring(0, x[0],)
    draw_spring(x[0], x[1])
    draw_spring(x[1], x[2])
    draw_spring(x[2], x[3])
    draw_spring(x[3], 10)
    
    # draw masses
    plt.plot(x, [0, 0, 0, 0], "ko", ms=8)
    
    # draw vertical bars at edges
    plt.vlines([0, 10], -1, 1, "k", lw=5)
    
    
# example of an unstretched chain
draw_chain([0, 0, 0, 0])
plt.axis("scaled")
plt.axis("off")
plt.show()
%%graded # 20 points

# YOUR CODE HERE<jupyter_output><empty_output>