function h = schroedinger_linear_conserved ( x, u )

%*****************************************************************************80
%
%% schroedinger_linear_conserved() evaluates the conserved quantity.
%
%  Discussion:
%
%    The integral of |U|^2 over the infinite interval should be conserved.
%
%    For the problem of interest, involving two solitons, conservation
%    should aproximately hold over the finite interval, as long as the
%    predominant mass of both solitons stays within that finite region.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    19 April 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Willem Hundsdorfer, Jan Verwer,
%    Numerical solution of time-dependent advection-diffusion-reaction equations,
%    Springer, 2003
%    ISBN: 978-3-662-09017-6
%
%  Input:
%
%    real X(1,NX), the coordinates of the nodes.
%
%    complex U(NT,NX), the solution values on a time and space grid.
%
%  Output:
%
%    real H(NT,1), the conserved quantity at each time.
%
  nt = size ( u, 1 );
  nx = size ( u, 2 );

  dx = ( max ( x ) - min ( x ) ) / ( nx - 1 );
  y = ( abs ( u ) ).^2;
%
%  Apply trapezoid rule to estimate integral.
%
  h(:,1) = ( - 0.5 * y(:,1) + sum ( y, 2 ) - 0.5 * y(:,nx) ) * dx;

  return
end

