I wante a typed python script which numericaly solves using scipy odesolver a elastic…
- User
I wante a typed python script which numericaly solves using scipy odesolver a elastic multibody system of 4 bodies in 2d each body is connected by spring. keep the code short and precise, no bloat or noob comments
Assistant
import numpy as np from scipy.integrate import solve_ivp def multibody_system(t, y, kx, ky): m1, m2, m3, m4 = y[0], y[1], y[2], y[3] x1, y1, x2, y2, x3, y3, x4, y4 = y delta1 = np.array([x2 - x1, y2 - y1]) delta2 = np.array([x3 - x2, y3 - y2]) delta3 = np.array([x4 - x3, y4 - y3]) k1 = kx[0] k2 = kx[1] k3 = kx[2] k4 = kx[3] fx1 = -k1 * delta1 fx2 = -k2 * delta2 fx3 = -k3 * delta3 fx4 = -k4 * delta3 ax1 = fx1 / m1 ay1 = fx1 / m1 ax2 = fx2 / m2 ay2 = fx2 / m2 ax3 = fx3 / m3 ay3 = fx3 / m3 ax4 = fx4 / m4 ay4 = fx4 / m4 return [ [x1, y1, ax1, ay1], [x2, y2, ax2, ay2], [x3, y3, ax3, ay3], [x4, y4, ax4, ay4] ] if __name__ == '__main__': m1, m2, m3, m4 = 1.0, 1.0, 1.0, 1.0 kx = [10.0, 10.0, 10.0, 10.0] y0 = [0.0, 0.0, 0.0, 0.0] t_span = [0, 10] t_eval = np.linspace(t_span[0], t_span[1], 200) sol = solve_ivp(multibody_system, t_span, y0, args=(kx, ky), dense_output=True, t_eval=t_eval) x1, y1, x2, y2, x3, y3, x4, y4 = sol.y.T import matplotlib.pyplot as plt plt.plot(x1, y1, label='Body 1') plt.plot(x2, y2, label='Body 2') plt.plot(x3, y3, label='Body 3') plt.plot(x4, y4, label='Body 4') plt.xlabel('X') plt.ylabel('Y') plt.legend() plt.grid(True) plt.show()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.