TheAlgorithms/Java · error · IllegalArgumentException

Time step must be positive

Error message

Time step must be positive

What it means

Thrown by SimplePendulumRK4.stepRK4 when dt <= 0. The RK4 integration advances state by dt; a non-positive step produces no forward progress (dt = 0 returns the same state) or reverses time (dt < 0), corrupting a forward-marching simulation. This is the second guard, checked after the state-array validation.

Source

Thrown at src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java:62

        double omega = state[1];
        double dtheta = omega;
        double domega = -(g / length) * Math.sin(theta);
        return new double[] {dtheta, domega};
    }

    /**
     * Performs one time step using the RK4 method.
     *
     * @param state the current state [theta, omega]
     * @param dt the time step size
     * @return the new state after time dt
     */
    public double[] stepRK4(double[] state, double dt) {
        if (state == null || state.length != 2) {
            throw new IllegalArgumentException("State must be array of length 2");
        }
        if (dt <= 0) {
            throw new IllegalArgumentException("Time step must be positive");
        }

        double[] k1 = derivatives(state);
        double[] s2 = new double[] {state[0] + 0.5 * dt * k1[0], state[1] + 0.5 * dt * k1[1]};

        double[] k2 = derivatives(s2);
        double[] s3 = new double[] {state[0] + 0.5 * dt * k2[0], state[1] + 0.5 * dt * k2[1]};

        double[] k3 = derivatives(s3);
        double[] s4 = new double[] {state[0] + dt * k3[0], state[1] + dt * k3[1]};

        double[] k4 = derivatives(s4);

        double thetaNext = state[0] + dt / 6.0 * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0]);
        double omegaNext = state[1] + dt / 6.0 * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1]);

        return new double[] {thetaNext, omegaNext};
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a strictly positive dt; for RK4 a moderately small step (e.g. 1e-3) is usually sufficient given RK4's accuracy.
  2. In the marching loop, clamp the last step: double step = Math.min(dt, tEnd - t); break when step <= 0.
  3. Validate dt where the integration schedule is configured.

Example fix

// before
double[] next = p.stepRK4(state, tEnd - t);
// after
double step = tEnd - t;
if (step <= 0) break;
double[] next = p.stepRK4(state, step);
Defensive patterns

Strategy: validation

Validate before calling

double step = dt;
if (step <= 0) {
    throw new IllegalStateException("RK4 step resolved to non-positive dt");
}
double[] next = p.stepRK4(state, step);

Prevention

When it happens

Trigger: Calling stepRK4(state, 0) or stepRK4(state, negativeValue). Typically arises when a time-marching loop computes a final step as tEnd - t that resolves to zero or negative in the last iteration.

Common situations: Adaptive or fixed-step loop overshooting the end time, a step counter running one iteration too far, or a config default of 0.

Related errors


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