TheAlgorithms/Python · error · ValueError

The final value of x must be greater than initial value of x

Error message

The final value of x must be greater than initial value of x.

What it means

Error "The final value of x must be greater than initial value of x." thrown in TheAlgorithms/Python.

Source

Thrown at maths/numerical_analysis/runge_kutta_fehlberg_45.py:62

    >>> y = runge_kutta_fehlberg_45(5, 0, 0, 0.1, 1)
    Traceback (most recent call last):
        ...
    TypeError: 'int' object is not callable
    >>> def f(x, y):
    ...     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

View on GitHub (pinned to f5988cc097)

Solutions

  1. Set the final x value greater than the initial x value.
  2. Swap the integration bounds if integrating in the reverse direction was intended.

When it happens

Trigger: Thrown at maths/numerical_analysis/runge_kutta_fehlberg_45.py:62 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/945dfb247a55a701. Report an issue: GitHub.