function h = pendulum_elastic_conserved ( y )

%*****************************************************************************80
%
%% pendulum_elastic_conserved(): conserved quantity for pendulum_elastic_ode().
%
%  Discussion:
%
%    In the formula for energy, the reference ambiguously writes
%    ... ( nu + m g / k l )...  which must be ( nu + m g / k / l )!
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    23 November 2020
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Paul D Williams,
%    The RAW Filter: An Improvement to the Robert–Asselin Filter 
%    in Semi-Implicit Integrations,
%    Monthly Weather Review,
%    Volume 139, pages 1996-2007, June 2011.
%
%  Input:
%
%    real Y(:,4): the current solution.
%
%  Output:
%
%    real H(:): the value of the conserved quantity.
%
  [ g, k, l, m, t0, y0, tstop ] = pendulum_elastic_parameters ( );

  th = y(:,1);
  thdot = y(:,2);
  nu = y(:,3);
  nudot = y(:,4);

  h = 0.5 * m * l^2 * ( nudot.^2 + ( 1.0 + nu ).^2 .* thdot.^2 ) ...
    - m * g * l * ( 1.0 + nu ) .* cos ( th ) ...
    + 0.5 * k * l^2 * ( nu + m * g / k / l ).^2 ...
    + m * g * l ...
    - 0.5 * k * ( y0(3) * l )^2;

  return
end

