import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%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%20%3Ca%20href%3D%22https%3A%2F%2Fcolab.research.google.com%2Fgithub%2Fapplejxd%2Fcolaboratory%2Fblob%2Fmaster%2Fphysics%2Fvariation.ipynb%22%20target%3D%22_parent%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcolab.research.google.com%2Fassets%2Fcolab-badge.svg%22%20alt%3D%22Open%20In%20Colab%22%2F%3E%3C%2Fa%3E%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%20%E5%A4%89%E5%88%86%E5%8E%9F%E7%90%86%E3%81%AE%E3%83%87%E3%83%A2%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%E8%AA%BF%E5%92%8C%E6%8C%AF%E5%8B%95%E5%AD%90%E3%81%AE%E4%BD%9C%E7%94%A8%EF%BC%88%E6%B1%8E%E9%96%A2%E6%95%B0%EF%BC%89%0A%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20S%5Bq%5D%0A%20%20%20%20%20%20%20%20%3D%5Cint%20dt%20%5Cleft(%5Cfrac%7B1%7D%7B2%7Dm%5Cdot%7Bq%7D(t)%5E2-%5Cfrac%7B1%7D%7B2%7Dkq(t)%5E2%5Cright)%0A%20%20%20%20%24%24%0A%20%20%20%20%E3%81%AB%E5%AF%BE%E3%81%99%E3%82%8B%E5%A4%89%E5%88%86%E5%8E%9F%E7%90%86%E3%82%92%E6%9C%80%E9%81%A9%E5%8C%96%E5%95%8F%E9%A1%8C%E3%81%A8%E3%81%97%E3%81%A6%E8%A7%A3%E3%81%8F%E3%80%82%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20t_list%20%3D%20np.arange(0%2C%2010%2C%200.01)%0A%20%20%20%20dt%20%3D%20t_list%5B1%5D%20-%20t_list%5B0%5D%0A%20%20%20%20print(f%22len(t_list)%3D%7Blen(t_list)%7D%2C%20dt%20%3D%20%7Bdt%7D%22)%0A%20%20%20%20q_first%2C%20q_last%20%3D%20-5%2C%205%0A%20%20%20%20q_list%20%3D%20np.linspace(q_first%2C%20q_last%2C%20len(t_list)-2)%0A%0A%20%20%20%20plt.plot(t_list%2C%20np.hstack(%5Bq_first%2C%20q_list%2C%20q_last%5D))%0A%20%20%20%20plt.xlabel(%22t%22)%0A%20%20%20%20plt.ylabel(%22q(t)%22)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20dt%2C%20np%2C%20plt%2C%20q_first%2C%20q_last%2C%20q_list%2C%20t_list%0A%0A%0A%40app.cell%0Adef%20_(dt%2C%20np%2C%20plt%2C%20q_first%2C%20q_last%2C%20q_list%2C%20t_list)%3A%0A%20%20%20%20def%20dq_list(q_list%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20q_list_all%20%3D%20np.hstack(%5Bq_first%2C%20q_list%2C%20q_last%5D)%0A%20%20%20%20%20%20%20%20result%20%3D%20np.array(%5B(q_list_all%5Bidx%2B1%5D%20-%20q_list_all%5Bidx-1%5D)%20%2F%20(2%20*%20dt)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20idx%20in%20range(1%2C%20len(q_list_all)-1)%5D)%0A%20%20%20%20%20%20%20%20return%20result%0A%0A%20%20%20%20print(f%22len(dq_list)%3D%7Blen(dq_list(q_list))%7D%22)%0A%20%20%20%20plt.plot(t_list%5B1%3A-1%5D%2C%20dq_list(q_list))%0A%20%20%20%20plt.xlabel(%22t%22)%0A%20%20%20%20plt.ylabel(%22dq%2Fdt%22)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20(dq_list%2C)%0A%0A%0A%40app.cell%0Adef%20_(dq_list%2C%20dt%2C%20np%2C%20q_list)%3A%0A%20%20%20%20m%2C%20k%20%3D%201%2C%201%0A%0A%20%20%20%20def%20action(q_list%3A%20np.ndarray)%20-%3E%20float%3A%0A%20%20%20%20%20%20%20%20kinetic_energy%20%3D%200.5%20*%20m%20*%20np.sum(dq_list(q_list)**2)%20*%20dt%0A%20%20%20%20%20%20%20%20potential_energy%20%3D%200.5%20*%20k%20*%20np.sum(q_list**2)%20*%20dt%0A%20%20%20%20%20%20%20%20return%20kinetic_energy%20-%20potential_energy%0A%0A%20%20%20%20print(f%22action%20%3D%20%7Baction(q_list)%7D%22)%0A%20%20%20%20return%20action%2C%20k%2C%20m%0A%0A%0A%40app.cell%0Adef%20_(dt%2C%20k%2C%20m%2C%20np%2C%20plt%2C%20q_first%2C%20q_last%2C%20q_list%2C%20t_list)%3A%0A%20%20%20%20def%20ddq_list(q_list%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20q_list_all%20%3D%20np.hstack(%5Bq_first%2C%20q_list%2C%20q_last%5D)%0A%20%20%20%20%20%20%20%20result%20%3D%20np.array(%5B(q_list_all%5Bidx%2B1%5D%20-%202*q_list_all%5Bidx%5D%20%2B%20q_list_all%5Bidx-1%5D)%20%2F%20dt%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20idx%20in%20range(1%2C%20len(q_list_all)-1)%5D)%0A%20%20%20%20%20%20%20%20return%20result%0A%0A%20%20%20%20def%20eom(q_list%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20return%20m%20*%20ddq_list(q_list)%20-%20k%20*%20q_list%0A%0A%20%20%20%20plt.plot(t_list%5B1%3A-1%5D%2C%20eom(q_list))%0A%20%20%20%20plt.xlabel(%22t%22)%0A%20%20%20%20plt.ylabel(%22eom%22)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20(eom%2C)%0A%0A%0A%40app.cell%0Adef%20_(action%2C%20eom%2C%20plt%2C%20q_list%2C%20t_list)%3A%0A%20%20%20%20from%20scipy.optimize%20import%20minimize%0A%0A%20%20%20%20def%20minimize_callback(xk)%20-%3E%20bool%3A%0A%20%20%20%20%20%20%20%20print(f%22action%3D%7Baction(xk)%7D%22)%0A%0A%20%20%20%20res%20%3D%20minimize(action%2C%20q_list%2C%20method%3D%22CG%22%2C%20callback%3Dminimize_callback)%0A%20%20%20%20plt.plot(t_list%5B1%3A-1%5D%2C%20eom(res.x))%0A%20%20%20%20plt.xlabel(%22t%22)%0A%20%20%20%20plt.ylabel(%22eom%22)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20(res%2C)%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt%2C%20q_first%2C%20q_last%2C%20res%2C%20t_list)%3A%0A%20%20%20%20plt.plot(t_list%2C%20np.hstack(%5Bq_first%2C%20res.x%2C%20q_last%5D))%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
045326a472c2069a2ed4b1e741278a0d