function [SolutionToAlgebraicEquations,Output] = MGSolveLinearEqns
%===========================================================================
% File: MGSolveLinearEqns.m created Jul 20 2024 by MotionGenesis 6.4.
% Portions copyright (c) 2009-2024 Motion Genesis LLC. are licensed under
% the 3-clause BSD license.  https://opensource.org/licenses/BSD-3-Clause.
% This copyright notice must appear in all copies and distributions.
% MotionGenesis Professional Licensee: Motion Genesis LLC.
%===========================================================================
x=0; y=0;


%-------------------------------+--------------------------+-------------------+-----------------
% Quantity                      | Value                    | Units             | Description
%-------------------------------|--------------------------|-------------------|-----------------
shouldPrintToScreen             =  1;                      % NoUnits             0 or 1
shouldPrintToFile               =  1;                      % NoUnits             0 or 1
%-------------------------------+--------------------------+-------------------+-----------------


% Begin for-loop(s)
for( t = 0:  1:  4 ),

SolutionToAlgebraicEquations = DoCalculations;
Output = CalculateOutput;
OutputToScreenOrFile( Output, shouldPrintToScreen, shouldPrintToFile );

end   % End for-loop

OutputToScreenOrFile( [], 0, 0 );   % Close output files.


%===========================================================================
function SolutionToAlgebraicEquations = DoCalculations
%===========================================================================
COEF = zeros( 2, 2 );
COEF(1,1) = 2;
COEF(1,2) = 3;
COEF(2,1) = 4;
COEF(2,2) = 5;
RHS = zeros( 1, 2 );
RHS(1) = sin(t);
RHS(2) = cos(t);
SolutionToAlgebraicEquations = COEF \ transpose(RHS);

% Update variables after uncoupling equations
x = SolutionToAlgebraicEquations(1);
y = SolutionToAlgebraicEquations(2);


end


%===========================================================================
function Output = CalculateOutput
%===========================================================================
Output = zeros( 1, 3 );
Output(1) = t;
Output(2) = x;
Output(3) = y;
end


%===========================================================================
function OutputToScreenOrFile( Output, shouldPrintToScreen, shouldPrintToFile )
%===========================================================================
persistent FileIdentifier hasHeaderInformationBeenWritten;

if( isempty(Output) ),
   if( ~isempty(FileIdentifier) ),
      fclose( FileIdentifier(1) );
      clear FileIdentifier;
      fprintf( 1, '\n Output is in the file MGSolveLinearEqns.1\n' );
      fprintf( 1, '\n Note: To automate plotting, issue the command OutputPlot in MotionGenesis.\n' );
      fprintf( 1, '\n To load and plot columns 1 and 2 with a solid line and columns 1 and 3 with a dashed line, enter:\n' );
      fprintf( 1, '    someName = load( ''MGSolveLinearEqns.1'' );\n' );
      fprintf( 1, '    plot( someName(:,1), someName(:,2), ''-'', someName(:,1), someName(:,3), ''--'' )\n' );
   end
   if( shouldPrintToScreen ),
      fprintf( 1, '\n Output returned to the calling function:\n' );
      fprintf( 1, ' x (UNITS)    y (UNITS) ... \n\n' );
   end
   clear hasHeaderInformationBeenWritten;
   return;
end

if( isempty(hasHeaderInformationBeenWritten) ),
   if( shouldPrintToScreen ),
      fprintf( 1,                '%%       t              x              y\n' );
      fprintf( 1,                '%%     (sec)         (meter)        (meter)\n\n' );
   end
   if( shouldPrintToFile && isempty(FileIdentifier) ),
      FileIdentifier(1) = fopen('MGSolveLinearEqns.1', 'wt');   if( FileIdentifier(1) == -1 ), error('Error: unable to open file MGSolveLinearEqns.1'); end
      fprintf(FileIdentifier(1), '%% FILE: MGSolveLinearEqns.1\n%%\n' );
      fprintf(FileIdentifier(1), '%%       t              x              y\n' );
      fprintf(FileIdentifier(1), '%%     (sec)         (meter)        (meter)\n\n' );
   end
   hasHeaderInformationBeenWritten = 1;
end

if( shouldPrintToScreen ), WriteNumericalData( 1,                 Output(1:3) );  end
if( shouldPrintToFile ),   WriteNumericalData( FileIdentifier(1), Output(1:3) );  end
end


%===========================================================================
function WriteNumericalData( fileIdentifier, Output )
%===========================================================================
numberOfOutputQuantities = length( Output );
if( numberOfOutputQuantities > 0 ),
   for( i = 1 : numberOfOutputQuantities ),
      fprintf( fileIdentifier, ' %- 14.6E', Output(i) );
   end
   fprintf( fileIdentifier, '\n' );
end
end


%==========================================
end    % End of function MGSolveLinearEqns
%==========================================
