function y = spring_exact ( t )

%*****************************************************************************80
%
%% spring_exact() returns the exact solution for the spring ODE.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 December 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real T(:): the current time.
%
%  Output:
%
%    real Y(2): the exact solution.
%
  [ m, b, k, t0, y0, tstop ] = spring_parameters ( );

  u0 = y0(1);
  v0 = y0(2);

  r = sqrt ( ( 4.0 * m * k - b^2 ) ) / 2.0 / m;
  c1 = u0;
  c2 = ( v0 + k * u0 / 2.0 / m ) / r;

  u = c1 * exp ( - b * ( t - t0 ) / 2.0 / m ) .* cos ( r * ( t - t0 ) ) ...
    + c2 * exp ( - b * ( t - t0 ) / 2.0 / m ) .* sin ( r * ( t - t0 ) );

  v = c1 * ( - b / 2.0 / m ) * exp ( - b * ( t - t0 ) / 2.0 / m ) ...
                            .* cos ( r * ( t - t0 ) ) ...
    - c1 * r                 * exp ( - b * ( t - t0 ) / 2.0 / m ) ...
                            .* sin ( r * ( t - t0 ) ) ...
    + c2 * ( - b / 2.0 / m ) * exp ( - b * ( t - t0 ) / 2.0 / m ) ...
                            .* sin ( r * ( t - t0 ) ) ...
    + c2 * r                 * exp ( - b * ( t - t0 ) / 2.0 / m ) ...
                            .* cos ( r * ( t - t0 ) );

  y = [ u, v ];

  return
end

