## Motion of a mass on a spring

Context equations-of-motion
uses mechanics/dynamics
uses quantities/angular-frequency

We consider a point-like object of mass m : M attached to a spring whose mass we assume to be negligible. The other end of the spring is attached to a wall. When the particle is at position x : T→L relative to the equilibrium length l : L of the spring, the force F : T→F acting on it is proportional to x:

force: F = -(k × x)

where k : force-constant characterizes the elastic properties of the spring. (Source: Wikimedia Commons)

Newton’s equation of motion for the displacement x of the mass takes the form

newton: 𝒟(𝒟(x)) = -((k ÷ m) × x).

Additional arithmetic definitions for this context:
1. A force constant times a length is a force:
force-constant × L : F
force-constant × T→L : T→F
(k × f)[t] ⇒ k × f[t]
∀ f : T→L
∀ t : T
∀ k : force-constant

2. A force constant divided by a mass is the square of an angular frequency:
force-constant ÷ M : angular-frequency-squared

3. A squared angular frequency times a length is an acceleration:
angular-frequency-squared × L : A
angular-frequency-squared × T→L : T→A
(ω2 × f)[t] ⇒ ω2 × f[t]
∀ f : T→L
∀ t : T
∀ ω2 : angular-frequency-squared

Context analytical-solution
extends equations-of-motion
uses quantities/angular-frequency

### 1Analytical solution

Introducing ω : angular-frequency defined by ω = √(k ÷ m), the solution of =.newton can be written as

solution: x[t] = A × cos((ω × t) + δ)  ∀ t : T,

where cos(angle) : ℝ is the cosine function. The amplitude A : L and the phase δ : angle can take arbitray values.

Additional arithmetic definitions for this context:

√(angular-frequency-squared) : angular-frequency

### 2Numerical solution

Context euler-template
includes transformed quantities/function-with-finite-difference-template

For simplicity, we use one of the simplest numerical integration schemes known as the Euler method. It was developed in the era of manual computation, where simplicity was of utmost importance. There are much better integration schemes today, and therefore the Euler method should not be used in practice when using a computer.

In the Euler method, the derivative 𝒟(f) of a time-dependent quantity f is replaced by the finite difference Δ(f, h), where h is a small but non-zero integration step size. The finite difference is computed as

Δ(f, h)[t] ⇒ (f[t + h] - f[t]) ÷ h  ∀ t : SQD.

Context numerical-solution
extends equations-of-motion
includes transformed euler-template
includes transformed euler-template

Since the Euler integration scheme applies to first-order differential equations only, we must first transform =.newton into a set of two coupled first-order equations newton1.

We introduce v : T→V representing the velocity of the mass. The definition of this velocity,

newton1.x: 𝒟(x) = v,

is the first equation in our coupled set. The second one is obtained by applying the substitution subst: ⇒(newton1.x) 𝒟(x) ⇒ v to the second-order equation =.newton :

newton1.v: subst(newton) 𝒟(v) = -((k ÷ m) × x).

We can now discretize the equations newton1 by applying the substitution discretize: 𝒟(f) ⇒ Δ(f, h), where f:Q→Q is an arbitrary function of time and h : Tnz is the integration time step:

newtonΔ.x: discretize(newton1.x) Δ(x, h) = v
newtonΔ.v: discretize(newton1.v) Δ(v, h) = -((k ÷ m) × x)

Applying at-t: f → f[t] yields the equations for an explicit value of t : T:

newtonΔ-t.x: newtonΔ.xat-t(x[t + h] - x[t]) ÷ h = v[t]
newtonΔ-t.v: newtonΔ.vat-t(v[t + h] - v[t]) ÷ h = -((k ÷ m) × x[t])

These equations can be used to construct an iterative algorithm that computes time-series.x: xn = x[t0 + (n × h)] and time-series.v: vn = v[t0 + (n × h)] for any n:ℕ, given the initial values x0 and v0 at time t0 : T:

algo.x: xn ⇒ xn - 1 + (h × vn - 1)  ∀ n : ℕnz
algo.v: vn ⇒ vn - 1 + (h × ((k ÷ m) × xn - 1))  ∀ n : ℕnz

Additional arithmetic definition for this context:
• Integer indices select a time value on a grid:
T→L : L
T→V : V