TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException

should be greater than

Error message

{nameof(xEnd)} should be greater than {nameof(xStart)}

What it means

ClassicRungeKuttaMethod integrates an ODE from xStart to xEnd using RK4; the interval must have positive length, so it throws ArgumentOutOfRangeException for xEnd when xStart >= xEnd, naming xEnd as the offending parameter. An equal end/start is also rejected since no integration steps would occur.

Solutions

  1. Ensure xEnd > xStart; if integrating backward, negate the step and swap endpoints or use a solver that supports negative direction.
  2. Validate the interval at the caller before invoking.
  3. Check where xEnd is computed from config/duration; fix the source value.

Example fix

// before
solver.ClassicRungeKuttaMethod(t0, t0, y0, f); // tEnd == tStart
// after
var tEnd = t0 + duration; // duration > 0 validated
var result = solver.ClassicRungeKuttaMethod(t0, tEnd, y0, f);
Defensive patterns

Strategy: validation

Validate before calling

if (xEnd <= xStart) throw new ArgumentOutOfRangeException(nameof(xEnd), "xEnd must be greater than xStart");
var points = ClassicRungeKuttaMethod(xStart, xEnd, stepSize, yStart, f);

Try / catch

try { var pts = ClassicRungeKuttaMethod(x0, x1, h, y0, f); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "xEnd") { /* fix interval or use a backward-capable integrator */ }

Prevention

When it happens

Trigger: Calling ClassicRungeKuttaMethod with xEnd == xStart or xEnd < xStart, e.g. integrating backwards in time.

Common situations: Computing xEnd from a duration that ended up 0 or negative (bad config), or attempting backward integration which this API does not support.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/cd0c152145979d45. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/RungeKuttaMethod.cs:30

    ///     Loops through all the steps until xEnd is reached, adds a point for each step and then
    ///     returns all the points.
    /// </summary>
    /// <param name="xStart">Initial conditions x-value.</param>
    /// <param name="xEnd">Last x-value.</param>
    /// <param name="stepSize">Step-size on the x-axis.</param>
    /// <param name="yStart">Initial conditions y-value.</param>
    /// <param name="function">The right hand side of the differential equation.</param>
    /// <returns>The solution of the Cauchy problem.</returns>
    public static List<double[]> ClassicRungeKuttaMethod(
        double xStart,
        double xEnd,
        double stepSize,
        double yStart,
        Func<double, double, double> function)
    {
        if (xStart >= xEnd)
        {
            throw new ArgumentOutOfRangeException(
                nameof(xEnd),
                $"{nameof(xEnd)} should be greater than {nameof(xStart)}");
        }

        if (stepSize <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(stepSize),
                $"{nameof(stepSize)} should be greater than zero");
        }

        List<double[]> points = [];
        double[] firstPoint = [xStart, yStart];
        points.Add(firstPoint);

        var yCurrent = yStart;
        var xCurrent = xStart;

View on GitHub (pinned to 96e2905cab)