A Byte of PhysicsLogo

Simple harmonic motion as an ODE

An ideal mass-spring oscillator is an ordinary differential equation: acceleration is proportional to negative displacement. Its state is position and velocity, not an already-drawn sine wave. The sinusoid is a reference solution for a stated ideal regime, useful for validating an integrator rather than replacing the state update.

Think like a programmer

Write one pure acceleration function from position, velocity, mass, stiffness, damping/drive policy, and units. Integrate { position, velocity } at named times with a declared method. Keep the analytic frequency/phase reference, numerical state trace, total-energy ledger, and error/refinement table separate. Never derive force from the rendered spring length or replace state with a precomputed sine value once the model gains a drive or nonlinearity.

Model checklist

Inputs
Positive mass m, stiffness k, initial x0/v0, zero or declared damping/drive, start/end time, step schedule, integrator, output times, and energy/reference tolerance.
State
Timestamped x and v, acceleration, kinetic/potential/total energy, phase/frequency reference, and numerical error/refinement history.
Rule
Calculate a = −kx/m in the ideal case; advance both state values by the named integrator; derive observables from the next or documented time level.
Output
Trajectory, frequency/period, energy trace, analytic-reference error, and step/integrator metadata.
Check
At x > 0 with v = 0 acceleration is negative; zero stiffness gives zero restoring acceleration; doubling k at fixed m raises ω by √2; numerical trace approaches analytic ideal reference as step shrinks; closed ideal energy remains bounded with stated integrator behavior.
\[m\ddot{x}+kx=0\qquad\omega_0=\sqrt{\frac{k}{m}}\]

Oscillator state sampler

Change stiffness, damping, and displacement. The force is restoring; damping affects velocity once the system is moving.

Stiffness 20 N/m; damping 1; displacement 0.40 m; acceleration -8.00 m/s²; natural angular frequency 4.47 rad/s; energy 1.60 J.

For an ideal solution, initial position and velocity choose amplitude and phase while m and k choose the natural angular frequency. This separates state from configuration: changing stiffness changes derived acceleration and frequency immediately, but position changes only through an update over time.

\[m\ddot x+kx=0,\qquad \omega_0=\sqrt{\frac{k}{m}},\qquad E=\tfrac12mv^2+\tfrac12kx^2\]

Use at least three fixtures: release from positive displacement, pass equilibrium with nonzero velocity, and zero stiffness. Compare numerical x and v with the analytic solution at shared physical times. A method can have a plausible period while drifting in energy, or conserve energy reasonably while shifting phase; record both errors. The correct invariant depends on boundary and force model, so do not demand conservation after adding damping or drive without their transfer ledger.

Try this experiment

Prediction: Increasing stiffness raises the natural frequency, while changing initial displacement changes amplitude but not the ideal frequency.

Run release and equilibrium-crossing fixtures. Change k at fixed m, then change x0 at fixed k/m. Record calculated ω0, numerical period, phase error, and total energy at shared times for two step sizes. Add damping only after the undamped reference passes, and explain which energy transfer makes the closed invariant inapplicable.

Where this model breaks

Real springs have damping, finite travel, nonlinearity, mass, material loss, and possible hysteresis. An ideal oscillator is a reference model, not every physical spring. Numerical results depend on integrator/time-step choice; high frequency or long duration can make phase and energy error visible even when a short animation looks correct.

Summary

Implement oscillation as a second-order state update. Use the ideal frequency, analytic trace, and energy ledger as separate reference checks, then record integrator/refinement behavior before adding damping, drive, or nonlinear force laws.

Glossary

Self-check

  1. Which two values form oscillator state?
  2. Why is acceleration negative for positive displacement?
  3. Which parameter changes ideal frequency and which changes initial amplitude?
  4. Why log phase error as well as energy?

Sources

Model contract

Treat the lesson as a small function before treating it as a fact to memorize. Give every value a unit, keep only the state needed for the next step, and make the output easy to inspect.

\[\text{observable output} = f(\text{inputs},\,\text{state})\]
Inputs
Quantities you set or measure, with units and useful bounds.
State
Values the program must retain to reproduce the next result.
Rule
The relationship or update that turns inputs and state into a result.
Check
A known limit, unit check, invariant, or measured result that can expose a bad model.

Implement the idea as a model

For Simple Harmonic Motion as ODE, write down the quantities you can control, the values your program must retain, and the result a reader could inspect. In Oscillations, the useful program is not the drawing: it is the smallest explicit model that makes a prediction you can test.

Guided experiment

Prediction: changing one declared input while holding the others fixed should change only the outputs that the model connects to that input. Choose one input, predict the direction of change, then check a limiting case such as zero, a symmetric arrangement, or a familiar low-speed or small-change approximation.

Where this model breaks

This lesson is a teaching model, not a complete simulator. Before using it outside the stated question, check which interactions, scales, uncertainties, boundary conditions, and measurement limits it leaves out.

Summary

Treat Simple Harmonic Motion as ODE as a contract: named inputs and units enter a rule, the rule produces an observable result, and a known limit or invariant checks whether the implementation deserves trust.

Glossary

  • Input: a measured value or chosen parameter supplied to a model.
  • State: the smallest set of values needed to continue or reproduce a model.
  • Validation: comparing an output with a known result, limit, invariant, or measurement.

Self-check

  1. Which values are inputs, and which values must remain state?
  2. What observable result would tell you the model is behaving as expected?
  3. Which assumption would you test first before applying the model to a real system?

Model review: turn Simple Harmonic Motion as ODE into a test

Implement an ideal mass-spring oscillator from position, velocity, and a restoring acceleration function.

  1. Name the inputs and units that the oscillations model needs.
  2. Separate the state you must keep from values you can calculate when needed.
  3. Write one rule that maps the current state and inputs to an observable result.
  4. Choose a limiting case, unit check, invariant, or known result before trusting an output.
  5. State one assumption you would change before using this simplified model for a real decision.

Share to: