{"record":{"id":"927d6afdab80e9fe","repo":"TheAlgorithms/Java","slug":"natural-frequency-must-be-positive","errorCode":null,"errorMessage":"Natural frequency must be positive.","messagePattern":"Natural frequency must be positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/DampedOscillator.java","lineNumber":50,"sourceCode":"    private final double omega0;\n\n    /** Damping coefficient (s⁻¹). */\n    private final double gamma;\n\n    private DampedOscillator() {\n        throw new AssertionError(\"No instances.\");\n    }\n\n    /**\n     * Constructs a damped oscillator model.\n     *\n     * @param omega0 the natural frequency (rad/s), must be positive\n     * @param gamma  the damping coefficient (s⁻¹), must be non-negative\n     * @throws IllegalArgumentException if parameters are invalid\n     */\n    public DampedOscillator(double omega0, double gamma) {\n        if (omega0 <= 0) {\n            throw new IllegalArgumentException(\"Natural frequency must be positive.\");\n        }\n        if (gamma < 0) {\n            throw new IllegalArgumentException(\"Damping coefficient must be non-negative.\");\n        }\n        this.omega0 = omega0;\n        this.gamma = gamma;\n    }\n\n    /**\n     * Computes the analytical displacement of an underdamped oscillator.\n     * Formula: x(t) = A * exp(-γt) * cos(ω_d t + φ)\n     *\n     * @param amplitude the initial amplitude A\n     * @param phase     the initial phase φ (radians)\n     * @param time      the time t (seconds)\n     * @return the displacement x(t)\n     */\n    public double displacementAnalytical(double amplitude, double phase, double time) {","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/DampedOscillator.java#L32-L68","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass a strictly positive omega0 in rad/s (e.g. sqrt(stiffness/mass) for a spring).","Validate omega0 at the boundary where physical constants are loaded or computed, rejecting non-positive results upstream.","If omega0 may be NaN from upstream arithmetic, guard with !Double.isNaN(omega0) && omega0 > 0 before constructing."],"exampleFix":"// before\nDampedOscillator osc = new DampedOscillator(0, 0.5);\n// after\nDampedOscillator osc = new DampedOscillator(2 * Math.PI, 0.5);","handlingStrategy":"validation","validationCode":"if (Double.isNaN(omega0) || omega0 <= 0) {\n    throw new IllegalArgumentException(\"omega0 must be > 0\");\n}\nDampedOscillator osc = new DampedOscillator(omega0, gamma);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute omega0 = Math.sqrt(k / m) only after validating k > 0 and m > 0.","Treat oscillator parameters as config validated at load time.","Note the constructor does not reject NaN — add your own NaN guard."],"tags":["java","physics","parameter-validation","oscillator","constructor","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}