function []=deltawing()
%This program computes the drag coefficient and lift
%coefficient for a delta wing.

%Plot Kp and Kv graph for delta wings
clf
writetofile=0;
subplot(1,2,1),hold on
subplot(1,2,1),plot([0 4],[0 2.5],'b--');
subplot(1,2,1),plot([0 1 2 3 4],[0 1.1 2 2.7 3],'b--');
subplot(1,2,1),plot([0 1 2 3 3.5],[0 1.3 2.25 2.8 3.1],'b--');
subplot(1,2,1),plot([0 1 2 3],[0 1.5 2.4 3],'b--');
subplot(1,2,1),plot([.8 1 1.25 1.5 4],[1.2 1.3 1.4 1.45 1.4],'r-');
subplot(1,2,1),plot([1 1.25 1.5 1.75 2 4],[1.45 1.6 1.7 1.75 1.8 1.9],'r-');
subplot(1,2,1),plot([0 4],[0 2.5],'r-');
subplot(1,2,1),plot([0 4],[0 2.5],'r-');
subplot(1,2,1),plot([0 4],[0 2.5],'r-');
subplot(1,2,1),plot([0 4],[0 2.5],'r-');
subplot(1,2,1),plot([0 4],[0 2.5],'r-');
subplot(1,2,1),xlabel('AR');
subplot(1,2,1),ylabel('K_P');
subplot(1,2,2),plot([0.5 3],[2.1 2.5],'b--');
subplot(1,2,2),xlabel('AR');
subplot(1,2,2),ylabel('K_V');

Kp=input('Enter a value for Kp as read from graph.\n?');
Kv=input('Enter a value for Kv as read from graph.\n?');
Cd0=input('Enter a value for Cd0 for the wing.\n?');

if input('\nWould you like to print the information to a file?(y,n)\n?','s')=='y'
	filename=input('Enter a file name.\n?','s');
	filename=strcat(filename,'.dat');
	fid=fopen(filename,'w');
	writetofile=1;
end

clf
Ld_max=0;
alpha_max=0;
disp('            Lift Coefficient                Drag Coefficient');
disp(' Alpha   Pot''l     Vortex    Total       Pot''l     Vortex    Total     Lift/Drag Ratio');
disp('   --- --------- --------- ---------   --------- --------- ---------   ---------');
if writetofile
	fprintf(fid,'            Lift Coefficient                Drag Coefficient\n');
	fprintf(fid,' Alpha   Pot''l     Vortex    Total       Pot''l     Vortex    Total     Lift/Drag Ratio\n');
	fprintf(fid,'   --- --------- --------- ---------   --------- --------- ---------   ---------\n');
end

for alpha=0:.25:24
	alphar=alpha*pi/180;
	Clp=Kp*sin(alphar)*cos(alphar)^2;
	Clv=Kv*sin(alphar)^2*cos(alphar);
	Cl=Clp+Clv;
	Cdp=Clp*tan(alphar);
	Cdv=Clv*tan(alphar);
	Cdi=Cdp+Cdv;
	Cd=Cd0+Cdi;
	Ld=Cl/Cd;
	if Ld>Ld_max
		Ld_max=Ld;
		alpha_max=alpha;
	end
	plot(alpha,Ld,'bx');
	hold on
	if floor(alpha/2)==alpha/2
		disp(sprintf('   %2d  %8.5f  %8.5f  %8.5f    %8.5f  %8.5f  %8.5f    %8.5f',alpha,Clp,Clv,Cl,Cdp,Cdv,Cd,Ld));
	end
	if writetofile
		fprintf(fid,'%5.2f  %8.5f  %8.5f  %8.5f    %8.5f  %8.5f  %8.5f    %8.5f\n',alpha,Clp,Clv,Cl,Cdp,Cdv,Cd,Ld);
	end
end
xlabel('Angle of Attack (degrees)');
ylabel('Lift/Drag');
title(['Lift/Drag for a delta wing with Kp = ',num2str(Kp),' and Kv =',num2str(Kv)]);
disp(sprintf('\nMaximum lift/drag ratio is %10.8f at %4.2f degrees angle of attack.',Ld_max,alpha_max));
if writetofile 	
	fprintf(fid,'\nMaximum lift/drag ratio is %10.8f at %4.2f degrees angle of attack.\n',Ld_max,alpha_max);
   fclose(fid);
	disp(['Created filename',blanks(1),filename]);
end
