grok-4-0709· xAI
LMArena Human Preference 140K
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 # Parameters m = 1.0 # mass of each body k = 10.0 # spring constant L0 = 1.0 # rest length connections = [(0,1), (1,2), (2,3)] # springs between bodies 0-1,1-2,2-3 def spring_force(p1, p2): dx = p2 - p1 dist = np.linalg.norm(dx) if dist == 0: return np.zeros(2) unit = dx / dist return k * (dist - L0) * unit def ode(t, y): n = 4 pos = y[:2*n].reshape((n,2)) vel = y[2*n:].reshape((n,2)) acc = np.zeros((n,2)) for i, j in connections: f = spring_force(pos[i], pos[j]) acc[i] += f / m acc[j] -= f / m dydt = np.concatenate((vel.ravel(), acc.ravel())) return dydt # Initial conditions: positions in line, small perturbation y0 = np.array([0,0, 1,0, 2,0, 3,0, # x,y for 4 bodies 0,0, 0,0.1, 0,0, 0,0]) # vx,vy # Solve sol = solve_ivp(ode, [0, 10], y0, method='RK45', rtol=1e-6)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.