function [Pop,Vop,EtaAircraft,Pre,Ve,ifig]=DesignAircraft(W,rho,S,CD0,A,e,V,ifig)
% function [Pop,Vop,EtaAircraft,Pre,Ve]=DesignAircraft(W,rho,S,CD0,A,e,V)
% OUPUTS
% Pop is operating power of the aircraft (ft-lbf/sec)
% Vop is the operating velocity of the aircraft (ft/sec)
% Pre is the minimum poewr required (ft-lbf/sec)
% Ve is the  velocity of the aircraft for minimum power required (ft/sec)
%
% INPUTS
% CD0= drag coefficient when CL=0.
% A= aspect ratio span squared divided by reference area
% e= Oswalds efficiency factor
% V= velocity vector in ft/sec
% rho= air density in slugs/ft^3
% W= aircraft weight in pounds (lbf)
% S= wing area
% ifig is the figure number of the plot

k=1/(pi*A*e); % k from the drag polar CD=CD0+k*CL^2
CL=2*W./(rho*S*V.*V); % Lift cofficient as a function of velocity
CD=CD0+k*CL.*CL; % Drag coefficient as a function of velocity
Pr=.5*rho*S*V.^3.*CD; % Power required ft/lbf/sec
ifig=ifig+1; figure(ifig); plot(V,Pr)
xlabel('Velocity (ft/sec)')
ylabel('Power required (ft-lbf/sec)')
title('Aircraft Power required versus trim speed')
CLe=sqrt(3*CD0/k); % Lift coefficient for maximum endurance (minimum power required)
CDe=4*CD0; % Drag coefficient for maximum endurance (minimum power required)
Ve= sqrt(2*W/(rho*S*CLe));   % Speed for maximum endurance (minimum power required)
Pre=interp1(V,Pr,Ve);         % YI = INTERP1(X,Y,XI) % minimum power required
hold on
plot([Ve,Ve],[0,Pre],':'); plot([0,Ve],[Pre,Pre],':')
string1=['Minimum power of ',num2str(Pre),' ft-lbf/sec is achieved at a speed of ',num2str(Ve),' ft/sec.'];
disp(' '); disp(string1);
text(5,Pre-1,string1)
hold off 
VopInput=input('At what speed would you like to operate the aircraft? Default=Ve (speed for min. power required) >>>>');
if isempty(VopInput)
	Vop=Ve;
	Pop=Pre;
else
	Vop=VopInput;
	CLop=2*W./(rho*S*Vop*Vop); % Lift cofficient as a function of velocity
	CDop=CD0+k*CLop.*CLop; % Drag coefficient as a function of velocity
	Pop=.5*rho*S*Vop^3.*CDop; % Power required ft/lbf/sec
	hold on
	plot([Vop,Vop],[0,Pop],':'); plot([0,Vop],[Pop,Pop],':');
	string3=['Operating power of ',num2str(Pop),' ft-lbf/sec is required at an operating speed of ',num2str(Vop),' ft/sec'];
	text(5,Pop+1,string3)
	hold off
end

disp(' '); disp('AIRCRAFT SUMMARY');
string2=['  You are asking the Propeller to deliver ',num2str(Pop),' ft-lbf/sec of power at a speed of ',num2str(Vop),' ft/sec.'];
EtaAircraft=Pre/Pop;
string4=['The operating efficiency of your aircraft is ',num2str(EtaAircraft)];
disp(string2); disp(string4); disp(' ');
hold on; text(5,Pop+3,string4); hold off
