TheAlgorithms/Java · error · IllegalArgumentException

Natural frequency must be positive.

Error message

Natural frequency must be positive.

What it means

Thrown by the DampedOscillator constructor when omega0 <= 0. The natural angular frequency omega0 (rad/s) parameterises the undamped oscillation and appears squared inside the displacement formula; a zero or negative value is physically meaningless and would make omega_d = sqrt(omega0^2 - gamma^2) degenerate. This guard is checked before the gamma validation in the same constructor.

Source

Thrown at src/main/java/com/thealgorithms/physics/DampedOscillator.java:50

    private final double omega0;

    /** Damping coefficient (s⁻¹). */
    private final double gamma;

    private DampedOscillator() {
        throw new AssertionError("No instances.");
    }

    /**
     * Constructs a damped oscillator model.
     *
     * @param omega0 the natural frequency (rad/s), must be positive
     * @param gamma  the damping coefficient (s⁻¹), must be non-negative
     * @throws IllegalArgumentException if parameters are invalid
     */
    public DampedOscillator(double omega0, double gamma) {
        if (omega0 <= 0) {
            throw new IllegalArgumentException("Natural frequency must be positive.");
        }
        if (gamma < 0) {
            throw new IllegalArgumentException("Damping coefficient must be non-negative.");
        }
        this.omega0 = omega0;
        this.gamma = gamma;
    }

    /**
     * Computes the analytical displacement of an underdamped oscillator.
     * Formula: x(t) = A * exp(-γt) * cos(ω_d t + φ)
     *
     * @param amplitude the initial amplitude A
     * @param phase     the initial phase φ (radians)
     * @param time      the time t (seconds)
     * @return the displacement x(t)
     */
    public double displacementAnalytical(double amplitude, double phase, double time) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a strictly positive omega0 in rad/s (e.g. sqrt(stiffness/mass) for a spring).
  2. Validate omega0 at the boundary where physical constants are loaded or computed, rejecting non-positive results upstream.
  3. If omega0 may be NaN from upstream arithmetic, guard with !Double.isNaN(omega0) && omega0 > 0 before constructing.

Example fix

// before
DampedOscillator osc = new DampedOscillator(0, 0.5);
// after
DampedOscillator osc = new DampedOscillator(2 * Math.PI, 0.5);
Defensive patterns

Strategy: validation

Validate before calling

if (Double.isNaN(omega0) || omega0 <= 0) {
    throw new IllegalArgumentException("omega0 must be > 0");
}
DampedOscillator osc = new DampedOscillator(omega0, gamma);

Prevention

When it happens

Trigger: Calling new DampedOscillator(omega0, gamma) with omega0 == 0, omega0 < 0, or omega0 == Double.NaN (NaN <= 0 is false, so NaN slips through this check but will surface later — only non-positive reals trigger this message).

Common situations: Reading omega0 from config that defaults to 0, a unit-conversion error producing 0, or computing omega0 = sqrt(k/m) with k or m equal to zero.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/927d6afdab80e9fe. Report an issue: GitHub.