TheAlgorithms/Python · error · ValueError

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

Error message

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

What it means

Raised by the AdamsBashforth dataclass __post_init__ in maths/numerical_analysis/adams_bashforth.py when the last initial x value is >= x_final. Adams-Bashforth is a multistep ODE solver that marches forward from the initial points to x_final; if the integration endpoint is not strictly beyond the last initial condition, there is nothing to integrate and the constructor rejects it.

Source

Thrown at maths/numerical_analysis/adams_bashforth.py:54

    Traceback (most recent call last):
        ...
    ValueError: x-values must be equally spaced according to step size.

    >>> AdamsBashforth(f,[0,0.2,0.4,0.6,0.8],[0,0,0.04,0.128,0.307],-0.2,1).step_5()
    Traceback (most recent call last):
        ...
    ValueError: Step size must be positive.
    """

    func: Callable[[float, float], float]
    x_initials: list[float]
    y_initials: list[float]
    step_size: float
    x_final: float

    def __post_init__(self) -> None:
        if self.x_initials[-1] >= self.x_final:
            raise ValueError(
                "The final value of x must be greater than the initial values of x."
            )

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

        if not all(
            round(x1 - x0, 10) == self.step_size
            for x0, x1 in zip(self.x_initials, self.x_initials[1:])
        ):
            raise ValueError("x-values must be equally spaced according to step size.")

    def step_2(self) -> np.ndarray:
        """
        >>> def f(x, y):
        ...     return x
        >>> AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1).step_2()
        array([0.  , 0.  , 0.06, 0.16, 0.3 , 0.48])

View on GitHub (pinned to f5988cc097)

Solutions

  1. Ensure x_final > x_initials[-1], e.g. x_final = 1 with initials [0, 0.2].
  2. Check keyword-arg usage: AdamsBashforth(func=f, x_initials=..., y_initials=..., step_size=..., x_final=...) to avoid positional mix-ups.
  3. Derive x_final from the initial grid plus n*step_size so ordering is guaranteed.

Example fix

# before
AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 0.2)  # nothing to integrate

# after
AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1.0)
Defensive patterns

Strategy: validation

Validate before calling

if x_final <= x_initials[-1]:
    raise ValueError(f'x_final ({x_final}) must exceed last initial x ({x_initials[-1]})')

Prevention

When it happens

Trigger: AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 0.2) (x_final equals the last initial x), or any x_final <= x_initials[-1], or accidentally swapping the x_final and step_size arguments.

Common situations: Off-by-one in choosing the integration interval, reusing an example's x_final with different initial points, or argument-order confusion since both x_final and step_size are floats.

Related errors


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