%% Aeronautical Engine Comparative Analysis (MATLAB Code)
% This code imports engine data, sorts it by thrust, and plots four key
% performance and design metrics: T vs. Dry Weight, T vs. TSFC, T vs. Diameter, 
% and T vs. Year (Technology Trend).

% Clear workspace and command window
clc;
clear;

%% 1. Import Data from the Final Table
% The data structure is: [Number, Length(m), Diameter(m), Thrust(kN), DryWeight(Kg), TSFC(g/kN.s), Year]
Data = [
    1, 3.25, 1.32, 61.3, 1090, 19.0, 2005; % CF34-8C5B1
    2, 3.25, 1.32, 64.5, 1113, 19.3, 2006; % CF34-8C5
    3, 2.41, 1.12, 61.6, 1501, 17.2, 1987; % Tay 620
    4, 2.41, 1.14, 67.2, 1595, 17.2, 1987; % Tay 650
    5, 3.07, 1.35, 64.5, 1176, 19.3, 2004; % CF34-8E
    6, 3.42, 1.22, 77.0, 1715, 17.8, 2011; % SaM146 1s17
    7, 3.42, 1.22, 79.2, 1765, 17.8, 2011; % SaM146 1s18
    8, 3.03, 1.38, 74.0, 1450, 17.3, 1987; % D-436-T1
    9, 4.28, 1.37, 93.4, 2250, 19.5, 1964; % JT8D-200
    10, 3.68, 1.45, 90.5, 1678, 18.1, 2005; % CF34-10E
    11, 2.29, 1.45, 78.5, 1678, 18.4, 2005; % CF34-10A
    12, 3.74, 1.47, 93.4, 2250, 18.0, 1999; % BR715
    13, 2.40, 1.83, 97.9, 2041, 16.5, 1979; % CFM56-2C1
    14, 3.65, 1.83, 111.2, 2350, 16.2, 1987; % CFM56-5A1
    15, 2.49, 1.70, 97.9, 1968, 17.0, 1984; % CFM56-3B2
    16, 3.68, 1.84, 120.1, 2404, 16.2, 2000; % CFM56-5B7
    17, 3.96, 2.02, 151.3, 2585, 16.4, 1994; % CFM56-5C4
    18, 3.65, 1.86, 88.9, 2386, 16.4, 1998; % CFM56-7B20
    19, 2.52, 1.76, 98.1, 2386, 16.8, 1994; % CFM56-5B4 (Weight is missing/nan)
    20, 3.68, 1.84, 104.5, 2404, 16.2, 1996; % CFM56-5B6
    21, 3.55, 1.85, 103.6, 2500, 14.5, 2016; % PW1500G
    22, 3.25, 1.68, 110.0, 2327, 17.6, 1994; % V2500-A5
    23, 3.20, 1.85, 110.0, 2560, 14.5, 2018; % PW1900G
    24, 3.68, 1.84, 120.1, 2404, 16.2, 1994; % CFM56-5B7 (Duplicate, using its data)
];

% Engine names for plotting labels
EngineNames = {
    'CF34-8C5B1', 'CF34-8C5', 'Tay 620', 'Tay 650', 'CF34-8E', 'SaM146 1s17', 'SaM146 1s18', 'D-436-T1', 'JT8D-200', 'CF34-10E', 'CF34-10A', 'BR715', ...
    'CFM56-2C1', 'CFM56-5A1', 'CFM56-3B2', 'CFM56-5B7', 'CFM56-5C4', 'CFM56-7B20', 'CFM56-5B4', 'CFM56-5B6', 'PW1500G', 'V2500-A5', 'PW1900G', 'CFM56-5B7'
};

% Create a MATLAB table for structured display
T = table(EngineNames', Data(:,2), Data(:,3), Data(:,4), Data(:,5), Data(:,6), Data(:,7), ...
    'VariableNames', {'Engine', 'Length_m', 'Diameter_m', 'Thrust_kN', 'DryWeight_Kg', 'TSFC_g_per_kNs', 'Year'});


%% 2. Sort Data by Thrust (Ascending)
% Find the indices that sort the data based on the 'Thrust_kN' column (column 4)
[~, sortIdx] = sort(T.Thrust_kN);
TSorted = T(sortIdx, :);

% Display the sorted table
disp(' ');
disp('--------------------------------------------------------------------------------------------------------------------------------');
disp('Final Engine Table (Sorted by Thrust, Low to High)');
disp('--------------------------------------------------------------------------------------------------------------------------------');
disp(TSorted);
disp('--------------------------------------------------------------------------------------------------------------------------------');


%% 3. Plotting the Comparison Charts

% Extract sorted data for plotting (use original unsorted data for consistency in labeling)
Thrust_kN = T.Thrust_kN;
DryWeight_Kg = T.DryWeight_Kg;
TSFC = T.TSFC_g_per_kNs;
Diameter_m = T.Diameter_m;
Names = T.Engine;
Years = T.Year;


% --- Plot 1: Thrust vs. Dry Weight (T/W Ratio) ---
figure('Name', 'Thrust vs. Dry Weight (T/W Ratio)');
scatter(DryWeight_Kg, Thrust_kN, 80, Years, 'filled', 'MarkerEdgeColor', [0 .5 .5]);
hold on;

% Add labels to each point
validIdx = ~isnan(DryWeight_Kg); % Only label points with valid weight data
text(DryWeight_Kg(validIdx), Thrust_kN(validIdx), Names(validIdx), 'FontSize', 10, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');

% Add axis labels and title
xlabel('Dry Weight [Kg]');
ylabel('Thrust [kN]');
title('Thrust vs. Dry Weight (Primary Performance Metric) - T/W Ratio');
grid on;

% Fix for the colorbar error: Capture handle and set label separately
h_cbar = colorbar('Ticks', unique(Years));
h_cbar.Label.String = 'Year of Manufacture';

set(gca, 'XDir', 'reverse'); % Invert X-axis: Best performance (High T/W) is toward the top-right corner.
legend('Engines', 'Location', 'NorthWest');


% --- Plot 2: Thrust vs. TSFC (Fuel Efficiency) ---
figure('Name', 'Thrust vs. TSFC (Fuel Efficiency)');
scatter(TSFC, Thrust_kN, 80, Years, 'filled', 'MarkerEdgeColor', [0 .5 .5]);
hold on;

% Add labels to each point
text(TSFC, Thrust_kN, Names, 'FontSize', 10, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');

% Add axis labels and title
xlabel('TSFC [g/kN.s]');
ylabel('Thrust [kN]');
title('Thrust vs. TSFC (Fuel Efficiency Metric)');
grid on;

% Fix for the colorbar error
h_cbar = colorbar('Ticks', unique(Years));
h_cbar.Label.String = 'Year of Manufacture';

set(gca, 'XDir', 'reverse'); % Invert X-axis: Best efficiency (High T / Low TSFC) is toward the top-right corner.
legend('Engines', 'Location', 'SouthWest');


% --- Plot 3: Thrust vs. Diameter (Size Constraint) ---
figure('Name', 'Thrust vs. Fan Diameter (Installation Constraint)');
scatter(Diameter_m, Thrust_kN, 80, Years, 'filled', 'MarkerEdgeColor', [0 .5 .5]);
hold on;

% Add labels to each point
text(Diameter_m, Thrust_kN, Names, 'FontSize', 10, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');

% Add axis labels and title
xlabel('Diameter [m]');
ylabel('Thrust [kN]');
title('Thrust vs. Diameter (Installation Constraint)');
grid on;

% Fix for the colorbar error
h_cbar = colorbar('Ticks', unique(Years));
h_cbar.Label.String = 'Year of Manufacture';

legend('Engines', 'Location', 'SouthEast');


% --- Plot 4: Thrust vs. Year (Technology Trend) ---
figure('Name', 'Thrust vs. Year of Manufacture (Technology Trend)');

% Sort the data by Year for clear trend visualization
[YearsSorted, YearSortIdx] = sort(Years);
ThrustSorted = Thrust_kN(YearSortIdx);
NamesSorted = Names(YearSortIdx);

scatter(YearsSorted, ThrustSorted, 80, YearsSorted, 'filled', 'MarkerEdgeColor', [0 .5 .5]);
hold on;

% Add trend line (optional, but useful)
P = polyfit(YearsSorted, ThrustSorted, 1); % Fit a 1st degree polynomial (linear)
Y_fit = polyval(P, YearsSorted);
plot(YearsSorted, Y_fit, 'r--', 'LineWidth', 2, 'DisplayName', 'Linear Trend');

% Add labels to each point
text(YearsSorted, ThrustSorted, NamesSorted, 'FontSize', 10, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');

% Add axis labels and title
xlabel('Year of Entry into Service');
ylabel('Thrust [kN]');
title('Thrust vs. Year of Manufacture (Technology Trend)');
grid on;

% Fix for the colorbar error
h_cbar = colorbar('Ticks', unique(YearsSorted));
h_cbar.Label.String = 'Year of Manufacture';

legend('Engines', 'Linear Trend', 'Location', 'SouthEast');

%% 4. Final Conclusion
disp(' ');
disp('Analysis: Quick evaluation of the best engines in each chart:');
disp('1. Thrust vs. Weight: Engines located at the upper-right corner (high thrust, low weight).');
disp('2. Thrust vs. TSFC: Engines located at the upper-right corner (high thrust, low TSFC).');
disp('3. Thrust vs. Diameter: Engines located at the upper-left corner (high thrust, small diameter).');
disp('4. Thrust vs. Year: This plot shows the general trend of increasing thrust in commercial engines over the decades.');
