TheAlgorithms/Python · error · ValueError

Step size must be positive.

Error message

Step size must be positive.

What it means

Error "Step size must be positive." thrown in TheAlgorithms/Python.

Source

Thrown at maths/numerical_analysis/runge_kutta_fehlberg_45.py:67

    ...     return x + y
    >>> y = runge_kutta_fehlberg_45(f, 0, 0, 0.2, -1)
    Traceback (most recent call last):
        ...
    ValueError: The final value of x must be greater than initial value of x.
    >>> def f(x, y):
    ...     return x
    >>> y = runge_kutta_fehlberg_45(f, -1, 0, -0.2, 0)
    Traceback (most recent call last):
        ...
    ValueError: Step size must be positive.
    """
    if x_initial >= x_final:
        raise ValueError(
            "The final value of x must be greater than initial value of x."
        )

    if step_size <= 0:
        raise ValueError("Step size must be positive.")

    n = int((x_final - x_initial) / step_size)
    y = np.zeros(
        (n + 1),
    )
    x = np.zeros(n + 1)
    y[0] = y_initial
    x[0] = x_initial
    for i in range(n):
        k1 = step_size * func(x[i], y[i])
        k2 = step_size * func(x[i] + step_size / 4, y[i] + k1 / 4)
        k3 = step_size * func(
            x[i] + (3 / 8) * step_size, y[i] + (3 / 32) * k1 + (9 / 32) * k2
        )
        k4 = step_size * func(
            x[i] + (12 / 13) * step_size,
            y[i] + (1932 / 2197) * k1 - (7200 / 2197) * k2 + (7296 / 2197) * k3,
        )

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use a positive step size h > 0.

When it happens

Trigger: Thrown at maths/numerical_analysis/runge_kutta_fehlberg_45.py:67 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/ecd6c05921a6fa0d. Report an issue: GitHub.