%This function finds the number of satellites up to the input value with the
%lowest condition number and forward error. This is to determine the most 
%accurate number of GPS satellites.

%This function requires the other functions:
    %SatelliteConditionNumber
    %SatelliteEMF
    %MVNewtonsInputs
    %FCreator
    %DCreator
    
%The input to this function is the higest number of GPS satellites you want to
%have data for.

function Result=BestSatelliteNumber(MaxSatellites)
%The following line creates an empty matrix that will hold the data.
SatStats=[];

%The followiong loop collects data on the condition number and forward
%error for all satellite amounts up to the specified maximum amount.
for i=1:MaxSatellites-3
    %The data for each satellite amount is stored as a row in SatStats matrix
    SatStats=[SatStats;i+3,SatelliteConditionNumber(i+3)];
end

%The following two lines find the minimum values for the condition number and 
%the forward error and the minimum median values, and it finds the amount of 
%satellites that correspond to those values.
[MinimumValues, MinimumIndices]=min(SatStats);
[MaximumValues, MaximumIndices]=max(SatStats);

%The following 8 lines output the minimum values for the condition number, the 
%forward error, the median condition number, and the median forward error in 
%that order and it outputs the satellite amounts that correspond to those values
disp('The satellite amount with minimum condition number is:');
MinimumIndices(2)+3
disp('With condition number:');
MinimumValues(2)
disp('The satellite amount with the lowest median EMF is:');
MinimumIndices(4)+3
disp('With median:');
MinimumValues(4)
disp('The satellite amount with smallest maximum forward error is:');
MinimumIndices(3)+3
disp('With forward error number:');
MinimumValues(3)
disp('The satellite amount with the lowest median forward error is:');
MinimumIndices(5)+3
disp('With median:');
MinimumValues(5)

%The function outputs a matrix where the columns are the condition number, the 
%forward error, the median condition number, and the median forward error in 
%that order and the rows are those values for each satellite amount.
Result=SatStats;