<jupyter_start><jupyter_text>Solution for double pendulum<jupyter_code>import numpy as np
from scipy.integrate import odeint
g = 10.
beta = 0.1 #  some frition in the hinges; beta = 0. -> no friction
def pendulum(phi, t):
    phi1, phi2, phi3, phi4 = phi
    A = np.array([[4/3, .5*np.cos(phi1-phi2)],
                  [.5*np.cos(phi1-phi2), 1/3]])
    b = np.array([-.5*phi4**2*np.sin(phi1-phi2)-3/2*g*np.sin(phi1),
                  .5*phi3**2*np.sin(phi1-phi2)-.5*g*np.sin(phi2)])
    dphi = np.linalg.solve(A, b)
    return np.array([phi3, phi4, dphi[0]- beta*phi3, dphi[1]- beta*phi4])
phi0 = np.array([np.pi/2, 0, 0., 0.])
t = np.linspace(0, 30., 3000)

phi_t = odeint(pendulum, phi0, t)
%matplotlib inline
import matplotlib.pyplot as plt

ax = plt.subplot(1,2,1)
ax.plot(t, phi_t[:,0])
ax2 = plt.subplot(1,2,2)
ax2.plot(t, phi_t[:,1])
plt.show()
def compute_xy(phi_t):
    x1 = np.sin(phi_t[:,0])
    y1 = -np.cos(phi_t[:,0])
    x2 = x1 + np.sin(phi_t[:,1])
    y2 = y1 - np.cos(phi_t[:,1])
    return x1, y1, x2, y2

x1, y1, x2, y2 = compute_xy(phi_t)
%matplotlib notebook
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig = plt.figure(); ax = plt.subplot(111, aspect="equal")
ln1, ln2 = ax.plot([],[]) + ax.plot([],[])
ln3, = ax.plot([0,x1[0],x2[0]], [0,y1[0],y2[0]], 'ko-', lw=5)

def init():
    ax.set_xlim(-2.1, 2.1)
    ax.set_ylim(-2.1, 1.1)
    return ln1, ln2, ln3

def update(frame):
    ln1.set_data(x1[:frame+1], y1[:frame+1])
    ln2.set_data(x2[:frame+1], y2[:frame+1])
    ln3.set_data([0,x1[frame],x2[frame]], [0,y1[frame],y2[frame]])
    return ln1, ln2, ln3
    
ani = FuncAnimation(fig, update, frames=range(len(x1)), cache_frame_data=False,
                    init_func=init, interval=3, blit=True)

plt.show(ani)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20741150442477876, 'fraction_numerical': 0.07245575221238938, 'mean_word_length': 3.626598465473146, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '310916', 'license': 'MIT', 'n_tokens_mistral': 886, 'n_tokens_neox': 857, 'n_words': 155, 'path': 'projects/winter-school-numerics/SolutionDoublePendulum.ipynb', 'repo_name': 'alayoalex/data-science-ml-portfolio', 'size': '1808'}