% NACA four digit airfoil generation code.
% Input four digit from command line, eg. "2412"
clear
s=input('\nInput four digit mumber of the NACA airfoil:\n?','s');
t=str2num(s(length(s)-1))/10 str2num(s(length(s)))/100; % thickness
p=str2num(s(length(s)-2))/10; % max camber position
m=str2num(s(length(s)-3))/100; % max camber
if p==0 % To avoid p=0
p=0.0001;
end
x=0:0.001:1;
for i=1:length(x)
yt(i)=(t/0.2)*(0.2969*x(i)^0.5-0.1260*x(i)-0.3516*x(i)^2 0.2843*x(i)^3-0.1015*x(i)^4);
% thickness distribution
if x(i)<=p
yc(i)=m/p^2*(2*p*x(i)-x(i)^2); % center line (front half)
else
yc(i)=m/(1-p)^2*((1-2*p) 2*p*x(i)-x(i)^2); % center line (rear half)
end
if i==1
theta=pi/2;
else
theta=atan((yc(i)-yc(i-1))/(x(i)-x(i-1)));
end
xu(i)=x(i)-yt(i)*sin(theta); % upper surface
yu(i)=yc(i) yt(i)*cos(theta);
xl(i)=x(i) yt(i)*sin(theta); % lower surface
yl(i)=yc(i)-yt(i)*cos(theta);
end
% plot airfoil
plot(xu,yu,'b-',xl,yl,'b-',x,yc,'k-.');
grid on;
axis equal;
% output airfoil data
filename=['NACA',s,'.dat'];
fid=fopen(filename,'w');
fprintf(fid,' x_u y_u x_l y_l\n');
for i=1:length(x)
fprintf(fid,'%8.4f %8.4f %8.4f %8.4f\n',xu(i),yu(i),xl(i),yl(i));
end
fclose(fid);