function dydt = gyroscope_deriv ( t, y )

%*****************************************************************************80
%
%% gyroscope_deriv() returns the right hand side of gyroscope_ode().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    18 August 2024
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Forest Ray Moulton,
%    "The Damped Gyroscope",
%    Differential Equations,
%    Dover, 1958.
%
%  Input:
%
%    real T, the current time.
%
%    real Y(6), the current state values.
%
%  Output:
%
%    real DYDT(6), the time derivatives of the current state values.
%
  [ A1, A2, A3, m, t0, y0, tstop ] = gyroscope_parameters ( );

  psi    = y(1);
  theta  = y(2);
  phi    = y(3);
  omega1 = y(4);
  omega2 = y(5);
  omega3 = y(6);

  M1 = - m * A1 * sin ( theta ) * cos ( phi );
  M2 =   m * A2 * sin ( theta ) * sin ( phi );
  M3 = 0.0;

  dpsidt = ( omega1 * sin ( phi ) + omega2 * cos ( phi ) ) / sin ( theta );
  dthetadt = omega1 * cos ( phi ) - omega2 * sin ( phi );
  dphidt = omega3 - cos ( theta ) * dpsidt;

  domega1dt = ( ( A2 - A3 ) * omega2 * omega3 + M1 ) / A1;
  domega2dt = ( ( A3 - A1 ) * omega3 * omega1 + M2 ) / A2;
  domega3dt = ( ( A1 - A2 ) * omega1 * omega2 + M3 ) / A3;

  dydt = [ dpsidt; dthetadt; dphidt; domega1dt; domega2dt; domega3dt ];

  return
end

