function ratio = convergence ( ratio, x, x_old )

%*****************************************************************************80
%
%% convergence() updates a set of convergence ratios.
%
%  Discussion:
%
%    The numbers x and x_old will be said to have converged if
%      | x - xold | / |x| <= delta
%    where delta is a small positive value.  Here, we have delta = 0.001.
%
%    To avoid problems when |x| is zero or nearly so, we can rewrite
%    this check as
%      | x - xold | <= delta * | x |
%    or
%      0.0 <= delta * | x | - | x - xold |
%
%    If x and xold are vectors, than we write
%      0.0 <= delta * || x || - || x - xold ||
%    where, here, we use the l1 norm.
%
%    If we have a sequence of vectors, say x, u, and lambda, then
%    we want all three items to satisfy the convergence test, so
%    we let "ratio" be the minimum of the convergence ratios of
%    all these quantities.
%
%    This requires the user to initialize "ratio" to Inf before
%    the first call.  After all the variables have been submitted
%    to compute the minimum ratio, convergence can be declared 
%    if the final value of ratio is nonnegative.
%    
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    02 October 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real ratio: the current minimum convergence ratio. 
%    If this is the first call, set ratio = Inf.
%
%    real x(*), x_old(*): the current and previous values of x.
%
%  Output:
%
%    real ratio: the minimum convergence ratio, updated
%    to include the convergence of variable x.
%
  delta = 0.001;

  ratio = min ( ratio, ...
    delta * sum ( abs ( x ) ) - sum ( abs ( x - x_old ) ) );

  return
end

