%% Matlab solution for EX04 - 1
clear
close all

load('F18Hornet.mat')

%% Airplane parameters:
S = 38; % wing area [m^2]
b = 12.3; % wing span [m]
d = 17.1; % length of the airplane [m]

%% Calculate new graphs:
CD_alpha = [CL_alpha(:,1) interp1(CL_CD(:,2), CL_CD(:,1), CL_alpha(:,2), 'linear', 'extrap')];
AE_alpha = [CL_alpha(:,1) (CL_alpha(:,2) ./ CD_alpha(:,2))];
CN_alpha = [CL_alpha(:,1) ...
    (CL_alpha(:,2) .* cosd(CL_alpha(:,1)) + CD_alpha(:,2) .* sind(CL_alpha(:,1)))];
xCP_alpha = [CL_alpha(:,1) (0.6 * d - ...
    (S * interp1(CM_alpha(:,1), CM_alpha(:,2), CN_alpha(:,1), 'linear', 'extrap')) ./ (b * CN_alpha(:,2)))];

CD_maxl = interp1(CD_alpha(:,1), CD_alpha(:,2), 37.84);

%% Plot graphs:
figure; grid on;
plot(CL_alpha(:,1), CL_alpha(:,2), 'Marker', 'o')
xlabel('\alpha'); ylabel('C_L');

figure; grid on;
plot(CL_CD(:,1), CL_CD(:,2), 'Marker', 'o')
xlabel('C_D'); ylabel('C_L');

figure; grid on;
plot(CM_alpha(:,1), CM_alpha(:,2), 'Marker', 'o')
xlabel('\alpha'); ylabel('C_M');

figure; grid on;
plot(CD_alpha(:,1), CD_alpha(:,2), 'Marker', 'o')
xlabel('\alpha'); ylabel('C_D');

figure; grid on;
plot(AE_alpha(:,1), AE_alpha(:,2), 'Marker', 'o')
xlabel('\alpha'); ylabel('AE');

figure; grid on;
plot(xCP_alpha(:,1), (xCP_alpha(:,2) / d), 'Marker', 'o')
xlabel('\alpha'); ylabel('x_{CP} / d (%)');