%22%22%22Classical%20Mechanics%20with%20Derive%3A%20Lagrangian%20and%20Hamiltonian%20Formalism%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.19.4%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20from%20derive%20import%20(%0A%20%20%20%20%20%20%20%20Symbol%2C%20symbols%2C%20Function%2C%20Matrix%2C%20Rational%2C%20Sqrt%2C%20R%2C%0A%20%20%20%20%20%20%20%20D%2C%20Integrate%2C%20Simplify%2C%20Expand%2C%20Solve%2C%20Sin%2C%20Cos%2C%20Pi%2C%0A%20%20%20%20%20%20%20%20Series%2C%20Limit%2C%20Eq%2C%0A%20%20%20%20%20%20%20%20Eigenvalues%2C%20Eigenvectors%2C%20Det%2C%20Tr%2C%0A%20%20%20%20%20%20%20%20Nest%2C%20NestList%2C%20FixedPoint%2C%20FixedPointList%2C%0A%20%20%20%20%20%20%20%20Table%2C%20Map%2C%20Total%2C%0A%20%20%20%20)%0A%20%20%20%20from%20derive.calculus%20import%20EulerLagrangeEquation%0A%20%20%20%20from%20derive.ode%20import%20DSolve%2C%20NDSolve%0A%20%20%20%20from%20derive.plotting%20import%20Plot%2C%20ListPlot%2C%20ListLinePlot%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20Cos%2C%0A%20%20%20%20%20%20%20%20D%2C%0A%20%20%20%20%20%20%20%20DSolve%2C%0A%20%20%20%20%20%20%20%20Eigenvalues%2C%0A%20%20%20%20%20%20%20%20Eq%2C%0A%20%20%20%20%20%20%20%20EulerLagrangeEquation%2C%0A%20%20%20%20%20%20%20%20Function%2C%0A%20%20%20%20%20%20%20%20ListLinePlot%2C%0A%20%20%20%20%20%20%20%20ListPlot%2C%0A%20%20%20%20%20%20%20%20Matrix%2C%0A%20%20%20%20%20%20%20%20NDSolve%2C%0A%20%20%20%20%20%20%20%20NestList%2C%0A%20%20%20%20%20%20%20%20Plot%2C%0A%20%20%20%20%20%20%20%20R%2C%0A%20%20%20%20%20%20%20%20Series%2C%0A%20%20%20%20%20%20%20%20Simplify%2C%0A%20%20%20%20%20%20%20%20Sin%2C%0A%20%20%20%20%20%20%20%20Sqrt%2C%0A%20%20%20%20%20%20%20%20Symbol%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20symbols%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Classical%20Mechanics%20with%20Derive%0A%0A%20%20%20%20This%20notebook%20demonstrates%20Lagrangian%20and%20Hamiltonian%20mechanics%20using%20symderive's%0A%20%20%20%20full%20suite%20of%20symbolic%20tools.%0A%0A%20%20%20%20%23%23%20Overview%0A%0A%20%20%20%20We%20showcase%20symderive's%20capabilities%3A%0A%20%20%20%201.%20**EulerLagrangeEquation**%3A%20Derive%20equations%20of%20motion%20from%20Lagrangians%0A%20%20%20%202.%20**DSolve**%3A%20Analytically%20solve%20equations%20of%20motion%0A%20%20%20%203.%20**NDSolve**%3A%20Numerically%20integrate%20trajectories%0A%20%20%20%204.%20**Eigenvalues**%3A%20Find%20normal%20mode%20frequencies%0A%20%20%20%205.%20**Series**%3A%20Small-angle%20approximations%0A%20%20%20%206.%20**Plot**%3A%20Phase%20portraits%20and%20trajectories%0A%20%20%20%207.%20**NestList**%3A%20Discrete%20dynamical%20systems%20(Poincar%C3%A9%20maps)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.%20The%20Lagrangian%20Formalism%0A%0A%20%20%20%20The%20Lagrangian%20is%20defined%20as%20%24L%20%3D%20T%20-%20V%24%20where%20%24T%24%20is%20kinetic%20and%20%24V%24%20is%20potential%20energy.%0A%0A%20%20%20%20The%20equations%20of%20motion%20follow%20from%20the%20Euler-Lagrange%20equation%3A%0A%0A%20%20%20%20%24%24%5Cfrac%7Bd%7D%7Bdt%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5Cdot%7Bq%7D%7D%20-%20%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20q%7D%20%3D%200%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Symbol)%3A%0A%20%20%20%20%23%20Time%20and%20fundamental%20symbols%0A%20%20%20%20t%20%3D%20Symbol('t'%2C%20real%3DTrue)%0A%20%20%20%20m%20%3D%20Symbol('m'%2C%20positive%3DTrue)%0A%20%20%20%20g%20%3D%20Symbol('g'%2C%20positive%3DTrue)%0A%20%20%20%20return%20g%2C%20m%2C%20t%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%201%3A%20Simple%20Harmonic%20Oscillator%0A%0A%20%20%20%20For%20a%20mass%20on%20a%20spring%3A%20%24L%20%3D%20%5Cfrac%7B1%7D%7B2%7Dm%5Cdot%7Bx%7D%5E2%20-%20%5Cfrac%7B1%7D%7B2%7Dkx%5E2%24%0A%0A%20%20%20%20Let's%20derive%20the%20equation%20of%20motion%20and%20**solve%20it%20with%20DSolve**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(D%2C%20EulerLagrangeEquation%2C%20Function%2C%20R%2C%20Simplify%2C%20Symbol%2C%20m%2C%20t)%3A%0A%20%20%20%20k%20%3D%20Symbol('k'%2C%20positive%3DTrue)%0A%20%20%20%20x_sho%20%3D%20Function('x')(t)%0A%20%20%20%20v_sho%20%3D%20D(x_sho%2C%20t)%0A%0A%20%20%20%20L_SHO%20%3D%20R(1%2C%202)%20*%20m%20*%20v_sho**2%20-%20R(1%2C%202)%20*%20k%20*%20x_sho**2%0A%0A%20%20%20%20%23%20Euler-Lagrange%20equation%0A%20%20%20%20eq_SHO%20%3D%20EulerLagrangeEquation(L_SHO%2C%20x_sho%2C%20%5Bt%5D)%0A%20%20%20%20eq_motion%20%3D%20Simplify(eq_SHO)%0A%20%20%20%20eq_motion%0A%20%20%20%20return%20eq_motion%2C%20k%2C%20x_sho%0A%0A%0A%40app.cell%0Adef%20_(DSolve%2C%20Eq%2C%20eq_motion%2C%20t%2C%20x_sho)%3A%0A%20%20%20%20%23%20Solve%20the%20equation%20of%20motion%20using%20DSolve%0A%20%20%20%20%23%20eq_motion%20%3D%200%20is%20the%20ODE%0A%20%20%20%20sho_solution%20%3D%20DSolve(Eq(eq_motion%2C%200)%2C%20x_sho%2C%20t)%0A%20%20%20%20sho_solution%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20solution%20%24x(t)%20%3D%20C_1%20%5Ccos(%5Comega%20t)%20%2B%20C_2%20%5Csin(%5Comega%20t)%24%20where%20%24%5Comega%20%3D%20%5Csqrt%7Bk%2Fm%7D%24%0A%20%20%20%20represents%20oscillatory%20motion.%0A%0A%20%20%20%20%23%23%23%20Plotting%20the%20Solution%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Cos%2C%20Plot%2C%20Sin%2C%20Symbol)%3A%0A%20%20%20%20%23%20Plot%20harmonic%20oscillator%20solutions%0A%20%20%20%20t_plot%20%3D%20Symbol('t'%2C%20real%3DTrue)%0A%20%20%20%20omega_plot%20%3D%20Symbol('omega'%2C%20positive%3DTrue)%0A%0A%20%20%20%20%23%20Different%20initial%20conditions%0A%20%20%20%20x1%20%3D%20Cos(t_plot)%20%20%23%20x(0)%3D1%2C%20v(0)%3D0%0A%20%20%20%20x2%20%3D%20Sin(t_plot)%20%20%23%20x(0)%3D0%2C%20v(0)%3D1%0A%20%20%20%20x3%20%3D%20Cos(t_plot)%20%2B%20Sin(t_plot)%20%20%23%20Superposition%0A%0A%20%20%20%20Plot(%0A%20%20%20%20%20%20%20%20%5Bx1%2C%20x2%2C%20x3%5D%2C%0A%20%20%20%20%20%20%20%20(t_plot%2C%200%2C%204%20*%203.14159)%2C%0A%20%20%20%20%20%20%20%20PlotLabel%3D%22Harmonic%20Oscillator%20Solutions%22%2C%0A%20%20%20%20%20%20%20%20AxesLabel%3D%5B%22t%22%2C%20%22x(t)%22%5D%2C%0A%20%20%20%20%20%20%20%20PlotLegends%3D%5B%22cos(t)%22%2C%20%22sin(t)%22%2C%20%22cos(t)%2Bsin(t)%22%5D%2C%0A%20%20%20%20%20%20%20%20GridLines%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.%20The%20Simple%20Pendulum%0A%0A%20%20%20%20For%20a%20pendulum%20of%20length%20%24%5Cell%24%3A%0A%0A%20%20%20%20%24%24L%20%3D%20%5Cfrac%7B1%7D%7B2%7Dm%5Cell%5E2%5Cdot%7B%5Ctheta%7D%5E2%20%2B%20mg%5Cell%5Ccos%5Ctheta%24%24%0A%0A%20%20%20%20The%20equation%20of%20motion%20is%20nonlinear%3A%20%24%5Cddot%7B%5Ctheta%7D%20%2B%20%5Cfrac%7Bg%7D%7B%5Cell%7D%5Csin%5Ctheta%20%3D%200%24%0A%0A%20%20%20%20%23%23%23%20Small-Angle%20Approximation%20via%20Series%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Cos%2C%20D%2C%20EulerLagrangeEquation%2C%20Function%2C%20R%2C%20Simplify%2C%20Symbol%2C%20g%2C%20m%2C%20t)%3A%0A%20%20%20%20ell%20%3D%20Symbol('ell'%2C%20positive%3DTrue)%0A%20%20%20%20theta%20%3D%20Function('theta')(t)%0A%20%20%20%20theta_dot%20%3D%20D(theta%2C%20t)%0A%0A%20%20%20%20%23%20Pendulum%20Lagrangian%0A%20%20%20%20T_pend%20%3D%20R(1%2C%202)%20*%20m%20*%20ell**2%20*%20theta_dot**2%0A%20%20%20%20V_pend%20%3D%20-m%20*%20g%20*%20ell%20*%20Cos(theta)%0A%20%20%20%20L_pend%20%3D%20T_pend%20-%20V_pend%0A%0A%20%20%20%20%23%20Equation%20of%20motion%0A%20%20%20%20eq_pend%20%3D%20Simplify(EulerLagrangeEquation(L_pend%2C%20theta%2C%20%5Bt%5D))%0A%20%20%20%20eq_pend%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Series%2C%20Sin%2C%20Symbol)%3A%0A%20%20%20%20%23%20Small-angle%20approximation%3A%20sin(theta)%20%E2%89%88%20theta%20-%20theta%5E3%2F6%20%2B%20...%0A%20%20%20%20theta_small%20%3D%20Symbol('theta'%2C%20real%3DTrue)%0A%0A%20%20%20%20sin_expansion%20%3D%20Series(Sin(theta_small)%2C%20(theta_small%2C%200%2C%205))%0A%20%20%20%20sin_expansion%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20series%20shows%20%24%5Csin%5Ctheta%20%5Capprox%20%5Ctheta%20-%20%5Cfrac%7B%5Ctheta%5E3%7D%7B6%7D%20%2B%20O(%5Ctheta%5E5)%24.%0A%0A%20%20%20%20For%20small%20angles%2C%20the%20pendulum%20becomes%20a%20harmonic%20oscillator%20with%20%24%5Comega%20%3D%20%5Csqrt%7Bg%2F%5Cell%7D%24.%0A%0A%20%20%20%20%23%23%23%20Numerical%20Solution%20with%20NDSolve%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ListLinePlot%2C%20NDSolve%2C%20t)%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20%23%20Pendulum%20ODE%3A%20theta''%20%2B%20(g%2Fl)*sin(theta)%20%3D%200%0A%20%20%20%20%23%20As%20first-order%20system%3A%20y%20%3D%20%5Btheta%2C%20omega%5D%0A%20%20%20%20%23%20y'%20%3D%20%5Bomega%2C%20-(g%2Fl)*sin(theta)%5D%0A%0A%20%20%20%20g_val%20%3D%209.8%0A%20%20%20%20ell_val%20%3D%201.0%0A%0A%20%20%20%20def%20pendulum_ode(t_val%2C%20y)%3A%0A%20%20%20%20%20%20%20%20theta_val%2C%20omega_val%20%3D%20y%0A%20%20%20%20%20%20%20%20return%20%5Bomega_val%2C%20-(g_val%2Fell_val)%20*%20np.sin(theta_val)%5D%0A%0A%20%20%20%20%23%20Solve%20for%20initial%20angle%20%3D%200.5%20rad%0A%20%20%20%20sol_small%20%3D%20NDSolve(pendulum_ode%2C%20%5B0.5%2C%200%5D%2C%20(t%2C%200%2C%2010))%0A%0A%20%20%20%20%23%20Solve%20for%20initial%20angle%20%3D%202.5%20rad%20(large%20amplitude)%0A%20%20%20%20sol_large%20%3D%20NDSolve(pendulum_ode%2C%20%5B2.5%2C%200%5D%2C%20(t%2C%200%2C%2010))%0A%0A%20%20%20%20%23%20Plot%20comparison%20(single%20dataset)%0A%20%20%20%20small_data%20%3D%20list(zip(sol_small.t.tolist()%2C%20sol_small.y%5B0%5D.tolist()))%0A%0A%20%20%20%20ListLinePlot(%0A%20%20%20%20%20%20%20%20small_data%2C%0A%20%20%20%20%20%20%20%20PlotLabel%3D%22Pendulum%3A%20Small%20Amplitude%20(%CE%B8%E2%82%80%3D0.5%20rad)%22%2C%0A%20%20%20%20%20%20%20%20AxesLabel%3D%5B%22t%22%2C%20%22%CE%B8(t)%22%5D%2C%0A%20%20%20%20%20%20%20%20GridLines%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(np%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20large-amplitude%20solution%20is%20**anharmonic**%20-%20notice%20the%20period%20is%20longer%0A%20%20%20%20and%20the%20shape%20is%20not%20a%20pure%20sinusoid.%0A%0A%20%20%20%20%23%23%203.%20Coupled%20Oscillators%20and%20Normal%20Modes%0A%0A%20%20%20%20Two%20identical%20masses%20coupled%20by%20springs%20demonstrate%20**normal%20modes**.%0A%0A%20%20%20%20The%20eigenvalues%20of%20the%20dynamical%20matrix%20give%20the%20squared%20frequencies%20%24%5Comega%5E2%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(D%2C%20EulerLagrangeEquation%2C%20Function%2C%20R%2C%20Simplify%2C%20m%2C%20symbols%2C%20t)%3A%0A%20%20%20%20%23%20Two%20coupled%20oscillators%0A%20%20%20%20k1%2C%20k2%2C%20k_c%20%3D%20symbols('k_1%20k_2%20k_c'%2C%20positive%3DTrue)%0A%20%20%20%20x1_c%20%3D%20Function('x_1')(t)%0A%20%20%20%20x2_c%20%3D%20Function('x_2')(t)%0A%20%20%20%20v1_c%20%3D%20D(x1_c%2C%20t)%0A%20%20%20%20v2_c%20%3D%20D(x2_c%2C%20t)%0A%0A%20%20%20%20%23%20Lagrangian%0A%20%20%20%20T_coupled%20%3D%20R(1%2C%202)%20*%20m%20*%20(v1_c**2%20%2B%20v2_c**2)%0A%20%20%20%20V_coupled%20%3D%20(R(1%2C%202)%20*%20k1%20*%20x1_c**2%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20R(1%2C%202)%20*%20k2%20*%20x2_c**2%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20R(1%2C%202)%20*%20k_c%20*%20(x2_c%20-%20x1_c)**2)%0A%20%20%20%20L_coupled%20%3D%20T_coupled%20-%20V_coupled%0A%0A%20%20%20%20%23%20Equations%20of%20motion%0A%20%20%20%20eq1%20%3D%20Simplify(EulerLagrangeEquation(L_coupled%2C%20x1_c%2C%20%5Bt%5D))%0A%20%20%20%20eq2%20%3D%20Simplify(EulerLagrangeEquation(L_coupled%2C%20x2_c%2C%20%5Bt%5D))%0A%20%20%20%20(eq1%2C%20eq2)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Eigenvalues%2C%20Matrix%2C%20Symbol)%3A%0A%20%20%20%20%23%20For%20identical%20springs%20(k1%20%3D%20k2%20%3D%20k)%2C%20find%20normal%20mode%20frequencies%0A%20%20%20%20k_sym%20%3D%20Symbol('k'%2C%20positive%3DTrue)%0A%20%20%20%20k_coupling%20%3D%20Symbol('k_c'%2C%20positive%3DTrue)%0A%20%20%20%20m_sym%20%3D%20Symbol('m'%2C%20positive%3DTrue)%0A%0A%20%20%20%20%23%20Dynamical%20matrix%20(from%20-d%C2%B2x%2Fdt%C2%B2%20%3D%20A%C2%B7x)%0A%20%20%20%20%23%20With%20V%20%3D%20(1%2F2)k(x1%C2%B2%20%2B%20x2%C2%B2)%20%2B%20(1%2F2)k_c(x2-x1)%C2%B2%0A%20%20%20%20%23%20A%20%3D%20%5B%5Bk%20%2B%20k_c%2C%20-k_c%5D%2C%20%5B-k_c%2C%20k%20%2B%20k_c%5D%5D%20%2F%20m%0A%20%20%20%20A_matrix%20%3D%20Matrix(%5B%0A%20%20%20%20%20%20%20%20%5Bk_sym%20%2B%20k_coupling%2C%20-k_coupling%5D%2C%0A%20%20%20%20%20%20%20%20%5B-k_coupling%2C%20k_sym%20%2B%20k_coupling%5D%0A%20%20%20%20%5D)%20%2F%20m_sym%0A%0A%20%20%20%20%23%20Eigenvalues%20are%20%CF%89%C2%B2%0A%20%20%20%20omega_squared%20%3D%20Eigenvalues(A_matrix)%0A%20%20%20%20omega_squared%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20normal%20modes%20are%3A%0A%20%20%20%20-%20**Symmetric%20mode**%3A%20%24%5Comega_1%5E2%20%3D%20k%2Fm%24%20(masses%20move%20together)%0A%20%20%20%20-%20**Antisymmetric%20mode**%3A%20%24%5Comega_2%5E2%20%3D%20(k%20%2B%202k_c)%2Fm%24%20(masses%20move%20opposite)%0A%0A%20%20%20%20%23%23%204.%20The%20Hamiltonian%20Formalism%0A%0A%20%20%20%20The%20Hamiltonian%20is%20the%20Legendre%20transform%20of%20the%20Lagrangian%3A%0A%0A%20%20%20%20%24%24H%20%3D%20%5Csum_i%20p_i%20%5Cdot%7Bq%7D_i%20-%20L%24%24%0A%0A%20%20%20%20Hamilton's%20equations%20are%3A%0A%20%20%20%20%24%24%5Cdot%7Bq%7D_i%20%3D%20%5Cfrac%7B%5Cpartial%20H%7D%7B%5Cpartial%20p_i%7D%2C%20%5Cquad%20%5Cdot%7Bp%7D_i%20%3D%20-%5Cfrac%7B%5Cpartial%20H%7D%7B%5Cpartial%20q_i%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(D%2C%20R%2C%20Symbol%2C%20k%2C%20m)%3A%0A%20%20%20%20%23%20Harmonic%20oscillator%20in%20Hamiltonian%20form%0A%20%20%20%20x_h%20%3D%20Symbol('x'%2C%20real%3DTrue)%0A%20%20%20%20p_h%20%3D%20Symbol('p'%2C%20real%3DTrue)%0A%0A%20%20%20%20%23%20Hamiltonian%3A%20H%20%3D%20p%C2%B2%2F(2m)%20%2B%20kx%C2%B2%2F2%0A%20%20%20%20H_SHO%20%3D%20p_h**2%20%2F%20(2*m)%20%2B%20R(1%2C%202)%20*%20k%20*%20x_h**2%0A%0A%20%20%20%20%23%20Hamilton's%20equations%0A%20%20%20%20x_dot%20%3D%20D(H_SHO%2C%20p_h)%20%20%23%20dx%2Fdt%20%3D%20%E2%88%82H%2F%E2%88%82p%20%3D%20p%2Fm%0A%20%20%20%20p_dot%20%3D%20-D(H_SHO%2C%20x_h)%20%20%23%20dp%2Fdt%20%3D%20-%E2%88%82H%2F%E2%88%82x%20%3D%20-kx%0A%0A%20%20%20%20(H_SHO%2C%20x_dot%2C%20p_dot)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Phase%20Space%20Portrait%0A%0A%20%20%20%20In%20phase%20space%20%24(x%2C%20p)%24%2C%20trajectories%20are%20curves%20of%20constant%20energy.%0A%20%20%20%20For%20the%20harmonic%20oscillator%2C%20these%20are%20ellipses.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Plot%2C%20Sqrt%2C%20Symbol)%3A%0A%20%20%20%20%23%20Phase%20space%20trajectories%20(parametric%20plot%20would%20be%20ideal)%0A%20%20%20%20%23%20x(t)%20%3D%20A*cos(%CF%89t)%2C%20p(t)%20%3D%20-m%CF%89A*sin(%CF%89t)%0A%20%20%20%20%23%20Eliminating%20t%3A%20(x%2FA)%C2%B2%20%2B%20(p%2F(m%CF%89A))%C2%B2%20%3D%201%20(ellipse)%0A%0A%20%20%20%20%23%20Plot%20energy%20contours%3A%20H%20%3D%20p%C2%B2%2F2%20%2B%20x%C2%B2%2F2%20%3D%20E%20(with%20m%3Dk%3D1)%0A%20%20%20%20x_phase%20%3D%20Symbol('x'%2C%20real%3DTrue)%0A%0A%20%20%20%20%23%20p%20as%20function%20of%20x%20at%20constant%20E%0A%20%20%20%20E_vals%20%3D%20%5B0.5%2C%201.0%2C%202.0%5D%0A%0A%20%20%20%20%23%20Plot%20upper%20branches%3A%20p%20%3D%20sqrt(2E%20-%20x%C2%B2)%0A%20%20%20%20p_1%20%3D%20Sqrt(2*0.5%20-%20x_phase**2)%0A%20%20%20%20p_2%20%3D%20Sqrt(2*1.0%20-%20x_phase**2)%0A%20%20%20%20p_3%20%3D%20Sqrt(2*2.0%20-%20x_phase**2)%0A%0A%20%20%20%20Plot(%0A%20%20%20%20%20%20%20%20%5Bp_1%2C%20p_2%2C%20p_3%5D%2C%0A%20%20%20%20%20%20%20%20(x_phase%2C%20-2%2C%202)%2C%0A%20%20%20%20%20%20%20%20PlotLabel%3D%22Phase%20Space%20(upper%20half%2C%20m%3Dk%3D1)%22%2C%0A%20%20%20%20%20%20%20%20AxesLabel%3D%5B%22x%22%2C%20%22p%22%5D%2C%0A%20%20%20%20%20%20%20%20PlotLegends%3D%5B%22E%3D0.5%22%2C%20%22E%3D1.0%22%2C%20%22E%3D2.0%22%5D%2C%0A%20%20%20%20%20%20%20%20GridLines%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%205.%20Noether's%20Theorem%0A%0A%20%20%20%20Every%20continuous%20symmetry%20of%20the%20Lagrangian%20corresponds%20to%20a%20conserved%20quantity.%0A%0A%20%20%20%20%7C%20Symmetry%20%7C%20Conserved%20Quantity%20%7C%0A%20%20%20%20%7C----------%7C-------------------%7C%0A%20%20%20%20%7C%20Time%20translation%20%7C%20Energy%20%7C%0A%20%20%20%20%7C%20Space%20translation%20%7C%20Linear%20momentum%20%7C%0A%20%20%20%20%7C%20Rotation%20%7C%20Angular%20momentum%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(D%2C%20Function%2C%20R%2C%20m%2C%20t)%3A%0A%20%20%20%20%23%20Central%20force%20in%20polar%20coordinates%0A%20%20%20%20r_c%20%3D%20Function('r')(t)%0A%20%20%20%20phi_c%20%3D%20Function('phi')(t)%0A%20%20%20%20r_dot%20%3D%20D(r_c%2C%20t)%0A%20%20%20%20phi_dot%20%3D%20D(phi_c%2C%20t)%0A%0A%20%20%20%20%23%20Kinetic%20energy%20in%20polar%20coordinates%0A%20%20%20%20T_central%20%3D%20R(1%2C%202)%20*%20m%20*%20(r_dot**2%20%2B%20r_c**2%20*%20phi_dot**2)%0A%0A%20%20%20%20%23%20Generic%20central%20potential%20V(r)%0A%20%20%20%20V_r%20%3D%20Function('V')(r_c)%0A%0A%20%20%20%20L_central%20%3D%20T_central%20-%20V_r%0A%0A%20%20%20%20%23%20Angular%20momentum%3A%20p_phi%20%3D%20%E2%88%82L%2F%E2%88%82(phi_dot)%20%3D%20m*r%C2%B2*phi_dot%0A%20%20%20%20p_phi%20%3D%20D(L_central%2C%20phi_dot)%0A%20%20%20%20p_phi%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Since%20%24%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5Cphi%7D%20%3D%200%24%20(rotational%20symmetry)%2C%0A%20%20%20%20angular%20momentum%20%24p_%5Cphi%20%3D%20mr%5E2%5Cdot%7B%5Cphi%7D%24%20is%20conserved.%0A%0A%20%20%20%20%23%23%206.%20Discrete%20Dynamics%3A%20The%20Standard%20Map%0A%0A%20%20%20%20The%20**standard%20map**%20(Chirikov%20map)%20is%20a%20model%20of%20chaos%3A%0A%0A%20%20%20%20%24%24p_%7Bn%2B1%7D%20%3D%20p_n%20%2B%20K%20%5Csin(%5Ctheta_n)%24%24%0A%0A%20%20%20%20%24%24%5Ctheta_%7Bn%2B1%7D%20%3D%20%5Ctheta_n%20%2B%20p_%7Bn%2B1%7D%24%24%0A%0A%20%20%20%20We%20use%20**NestList**%20to%20iterate%20this%20map.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(NestList%2C%20np)%3A%0A%20%20%20%20%23%20Standard%20map%20with%20chaos%20parameter%20K%0A%20%20%20%20K%20%3D%200.9%20%20%23%20Below%20critical%20value%0A%0A%20%20%20%20def%20standard_map(state)%3A%0A%20%20%20%20%20%20%20%20theta%2C%20p%20%3D%20state%0A%20%20%20%20%20%20%20%20p_new%20%3D%20p%20%2B%20K%20*%20np.sin(theta)%0A%20%20%20%20%20%20%20%20theta_new%20%3D%20(theta%20%2B%20p_new)%20%25%20(2%20*%20np.pi)%0A%20%20%20%20%20%20%20%20return%20(theta_new%2C%20p_new%20%25%20(2%20*%20np.pi))%0A%0A%20%20%20%20%23%20Iterate%20from%20initial%20condition%0A%20%20%20%20trajectory%20%3D%20NestList(standard_map%2C%20(1.0%2C%200.5)%2C%201000)%0A%0A%20%20%20%20%23%20Extract%20for%20plotting%0A%20%20%20%20theta_vals%20%3D%20%5Bs%5B0%5D%20for%20s%20in%20trajectory%5D%0A%20%20%20%20p_vals%20%3D%20%5Bs%5B1%5D%20for%20s%20in%20trajectory%5D%0A%20%20%20%20return%20p_vals%2C%20theta_vals%0A%0A%0A%40app.cell%0Adef%20_(ListPlot%2C%20p_vals%2C%20theta_vals)%3A%0A%20%20%20%20%23%20Phase%20space%20portrait%20(Poincar%C3%A9%20section)%0A%20%20%20%20poincare_data%20%3D%20list(zip(theta_vals%2C%20p_vals))%0A%0A%20%20%20%20ListPlot(%0A%20%20%20%20%20%20%20%20poincare_data%2C%0A%20%20%20%20%20%20%20%20PlotLabel%3D%22Standard%20Map%20(K%3D0.9)%22%2C%0A%20%20%20%20%20%20%20%20AxesLabel%3D%5B%22%CE%B8%22%2C%20%22p%22%5D%2C%0A%20%20%20%20%20%20%20%20PlotStyle%3D%22Blue%22%2C%0A%20%20%20%20%20%20%20%20GridLines%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20trajectory%20shows%20**quasi-periodic**%20motion%20(for%20K%20%3C%201).%0A%20%20%20%20Above%20the%20critical%20value%20K%20%E2%89%88%200.97%2C%20the%20map%20becomes%20chaotic.%0A%0A%20%20%20%20%23%23%207.%20The%20Kepler%20Problem%0A%0A%20%20%20%20For%20a%20gravitational%20potential%20%24V(r)%20%3D%20-GMm%2Fr%24%2C%20trajectories%20are%20conic%20sections.%0A%0A%20%20%20%20The%20effective%20potential%20method%20reduces%20the%202D%20problem%20to%201D.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Symbol)%3A%0A%20%20%20%20%23%20Effective%20potential%20for%20Kepler%20problem%0A%20%20%20%20r_eff%20%3D%20Symbol('r'%2C%20positive%3DTrue)%0A%20%20%20%20L_ang%20%3D%20Symbol('L'%2C%20positive%3DTrue)%20%20%23%20Angular%20momentum%0A%20%20%20%20m_kepler%20%3D%20Symbol('m'%2C%20positive%3DTrue)%0A%20%20%20%20GM%20%3D%20Symbol('GM'%2C%20positive%3DTrue)%0A%0A%20%20%20%20%23%20V_eff%20%3D%20L%C2%B2%2F(2mr%C2%B2)%20-%20GMm%2Fr%0A%20%20%20%20V_eff%20%3D%20L_ang**2%20%2F%20(2%20*%20m_kepler%20*%20r_eff**2)%20-%20GM%20*%20m_kepler%20%2F%20r_eff%0A%0A%20%20%20%20V_eff%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Plot%2C%20R%2C%20Symbol)%3A%0A%20%20%20%20%23%20Plot%20effective%20potential%20(dimensionless)%0A%20%20%20%20r_plot%20%3D%20Symbol('r'%2C%20positive%3DTrue)%0A%0A%20%20%20%20%23%20V_eff%20%2F%20(GMm%2Fr_0)%20where%20r_0%20%3D%20L%C2%B2%2F(GMm%C2%B2)%0A%20%20%20%20%23%20In%20these%20units%3A%20V_eff%20%3D%201%2F(2r%C2%B2)%20-%201%2Fr%0A%20%20%20%20V_eff_plot%20%3D%20R(1%2C%202)%20%2F%20r_plot**2%20-%201%20%2F%20r_plot%0A%0A%20%20%20%20Plot(%0A%20%20%20%20%20%20%20%20V_eff_plot%2C%0A%20%20%20%20%20%20%20%20(r_plot%2C%200.5%2C%205)%2C%0A%20%20%20%20%20%20%20%20PlotLabel%3D%22Effective%20Potential%20(Kepler%20Problem)%22%2C%0A%20%20%20%20%20%20%20%20AxesLabel%3D%5B%22r%2Fr%E2%82%80%22%2C%20%22V_eff%22%5D%2C%0A%20%20%20%20%20%20%20%20GridLines%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20minimum%20of%20the%20effective%20potential%20gives%20the%20circular%20orbit%20radius.%0A%20%20%20%20Bound%20orbits%20(E%20%3C%200)%20oscillate%20between%20turning%20points.%0A%0A%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20We%20demonstrated%20symderive's%20classical%20mechanics%20capabilities%3A%0A%0A%20%20%20%20%7C%20Feature%20%7C%20Usage%20%7C%0A%20%20%20%20%7C---------%7C-------%7C%0A%20%20%20%20%7C%20**EulerLagrangeEquation**%20%7C%20Derive%20equations%20of%20motion%20%7C%0A%20%20%20%20%7C%20**DSolve**%20%7C%20Analytical%20solutions%20(harmonic%20oscillator)%20%7C%0A%20%20%20%20%7C%20**NDSolve**%20%7C%20Numerical%20trajectories%20(pendulum)%20%7C%0A%20%20%20%20%7C%20**Eigenvalues**%20%7C%20Normal%20mode%20frequencies%20%7C%0A%20%20%20%20%7C%20**Series**%20%7C%20Small-angle%20approximations%20%7C%0A%20%20%20%20%7C%20**Plot**%20%7C%20Phase%20portraits%2C%20potentials%20%7C%0A%20%20%20%20%7C%20**NestList**%20%7C%20Discrete%20maps%20(standard%20map)%20%7C%0A%20%20%20%20%7C%20**D**%20%7C%20Hamiltonian%20equations%20%7C%0A%0A%20%20%20%20Derive%20provides%20comprehensive%20tools%20for%20analytical%20mechanics!%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
e3cbf746503aef2d2c40dfe31edb3717