@@ -0,0 +1,1654 @@
+#!/usr/bin/env python3%0A# -*- coding: utf-8 -*-%0A%0A# Copyright (c) 2015 J%C3%A9r%C3%A9mie DECOCK (http://www.jdhp.org)%0A%0Aimport numpy as np%0A%0A%0Aclass State:%0A    def __init__(self, ndim):%0A        self.ndim = ndim%0A        self.position = np.zeros(self.ndim)%0A        self.velocity = np.zeros(self.ndim)%0A%0A%0Aclass Model:%0A%0A    def __init__(self, mass=1., stiffness=1.):%0A%0A        self.mass = mass%0A        self.stiffness = stiffness%0A%0A%0A    def compute_acceleration(self, state):%0A%0A        # F = -k.x%0A        self.stretch = state.position%5B0%5D  # 0 is the %22rest%22 point%0A        total_external_force = -1. * self.stiffness * self.stretch%0A%0A        # a = f/m%0A        acceleration = total_external_force / self.mass%0A%0A        return acceleration%0A%0A%0Adef compute_new_state(state, acceleration, delta_time):%0A    %22Compute the forward kinematics with finite difference method.%22%0A%0A    new_state = State(state.ndim)%0A    %0A    # Velocity (m/s) at time_n+1%0A    new_state.velocity = state.velocity + acceleration * delta_time%0A%0A    # Position (m) at time_n+1%0A    new_state.position = state.position + state.velocity * delta_time%0A%0A    return new_state%0A%0A%0Adef main():%0A%0A    state = State(ndim=1)%0A    state.position%5B0%5D = 1.  # initial stiffness (0 is the %22rest%22 point)%0A%0A    time = 0.%0A    delta_time = 0.01%0A%0A    model = Model()%0A%0A    print(%22#time  acceleration  velocity  position%22)%0A%0A    while time %3C 12:%0A%0A        time = time + delta_time%0A%0A        # Update state (physics)%0A        acceleration = model.compute_acceleration(state)%0A        state = compute_new_state(state, acceleration, delta_time)%0A%0A        print(time, acceleration, state.velocity%5B0%5D, state.position%5B0%5D)%0A%0A%0Aif __name__ == %22__main__%22:%0A    main()%0A%0A

Add a snippet (Python physics).