TheAlgorithms/Java · error · IllegalArgumentException
Damping coefficient must be non-negative.
Error message
Damping coefficient must be non-negative.
What it means
Thrown by the DampedOscillator constructor when gamma < 0. The damping coefficient gamma (s^-1) represents energy dissipation; a negative value would imply energy injection (negative damping), which is physically unstable and produces exponentially growing oscillations in the analytical formula. This guard runs after the omega0 check, so omega0 must already be valid.
Source
Thrown at src/main/java/com/thealgorithms/physics/DampedOscillator.java:53
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) {
double omegaD = Math.sqrt(Math.max(0.0, omega0 * omega0 - gamma * gamma));
return amplitude * Math.exp(-gamma * time) * Math.cos(omegaD * time + phase);
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass gamma >= 0; use 0 for an undamped oscillator and a small positive value for light damping.
- If gamma is derived from a measurement, apply Math.abs or validate sign at the source rather than relying on the constructor to fail.
- Re-check the formula gamma = omega0 / Q — a Q below 1 still yields positive gamma, but a sign flip elsewhere can produce a negative.
Example fix
// before DampedOscillator osc = new DampedOscillator(2 * Math.PI, -0.5); // after DampedOscillator osc = new DampedOscillator(2 * Math.PI, 0.5);
Defensive patterns
Strategy: validation
Validate before calling
if (gamma < 0) {
gamma = Math.abs(gamma); // or throw, depending on domain
}
DampedOscillator osc = new DampedOscillator(omega0, gamma); Prevention
- When deriving gamma from a Q-factor, validate the sign at the source.
- Use 0 to model the undamped case rather than a tiny negative value.
- Validate the full (omega0, gamma) pair together before constructing.
When it happens
Trigger: Calling new DampedOscillator(omega0, gamma) with a negative gamma. A gamma of exactly 0 is allowed and models the undamped case; only strictly negative values throw.
Common situations: Sign error when computing gamma from measured Q-factor (gamma = omega0/Q), passing a negative friction/viscosity constant, or feeding raw sensor data that contained a negative slope.
Related errors
- Natural frequency must be positive.
- Length must be positive
- Gravity must be positive
- Orbiting mass and radius must be positive.
- State must be a non-null array of length 2.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/b8fe2a5070c6a1e5.
Report an issue: GitHub.