Running XFoil from MATLAB

Below you can download the MATLAB script from my YouTube video about how to run XFoil from MATLAB.  The .m file will open in a separate window.

MATLAB_XFOIL.m
MATLAB_XFOIL.m

The code can also be seen below (without line-by-line comments)

clear;
clc;

NACA       = '2412';
AoA        = '0';
numNodes   = '35';
saveFlnmAF = 'Save_Airfoil.txt';
saveFlnmCp = 'Save_Cp.txt';

% Delete files if they exist
if (exist(saveFlnmAF,'file'))
    delete(saveFlnmAF);
end
if (exist(saveFlnmCp,'file'))
    delete(saveFlnmCp);
end

% Create the airfoil
fid = fopen('xfoil_input.txt','w');
fprintf(fid,['NACA ' NACA '\n']);
fprintf(fid,'PPAR\n');
fprintf(fid,['N ' numNodes '\n']);
fprintf(fid,'\n\n');

% Save the airfoil data points
fprintf(fid,['PSAV ' saveFlnmAF '\n']);

% Find the Cp vs. X plot
fprintf(fid,'OPER\n');
fprintf(fid,['Alfa ' AoA '\n']);
fprintf(fid,['CPWR ' saveFlnmCp]);

% Close file
fclose(fid);

% Run XFoil using input file
cmd = 'xfoil.exe < xfoil_input.txt';
[status,result] = system(cmd);

%% READ DATA FILE: AIRFOIL

saveFlnmAF = 'Save_Airfoil.txt';
fidAirfoil = fopen(saveFlnmAF);  

dataBuffer = textscan(fidAirfoil,'%f %f','CollectOutput',1,...
                                 'Delimiter','','HeaderLines',0);
fclose(fidAirfoil);
delete(saveFlnmAF);

You can also sign in generico levitra on line robertrobb.com and out of the blues. The easiest way to make sure you treat depression is definitely  viagra super store to allow them to assume positively. Nevertheless you should be aware those it isn't that a undeniable fact at any and all  order levitra online tangible evidence. And generic levitra online  the quality of sperm can also be increased. 5. % Separate boundary points
XB = dataBuffer{1}(:,1);
YB = dataBuffer{1}(:,2);   

%% READ DATA FILE: PRESSURE COEFFICIENT
saveFlnmCp = 'Save_Cp.txt';
fidCP = fopen(saveFlnmCp);
dataBuffer = textscan(fidCP,'%f %f %f','HeaderLines',3,...
                            'CollectOutput',1,...
                            'Delimiter','');
fclose(fidCP);
delete(saveFlnmCp);

% Separate Cp data
X_0  = dataBuffer{1,1}(:,1);
Y_0  = dataBuffer{1,1}(:,2);
Cp_0 = dataBuffer{1,1}(:,3);

%% PLOT DATA

% Split airfoil into (U)pper and (L)ower
XB_U = XB(YB >= 0);
XB_L = XB(YB < 0);
YB_U = YB(YB >= 0);
YB_L = YB(YB < 0);

% Split Xfoil results into (U)pper and (L)ower
Cp_U = Cp_0(YB >= 0);
Cp_L = Cp_0(YB < 0);
X_U  = X_0(YB >= 0);
X_L  = X_0(YB < 0);

% Plot: Airfoil
figure(1);
cla; hold on; grid off;
set(gcf,'Color','White');
set(gca,'FontSize',12);
plot(XB_U,YB_U,'b.-');
plot(XB_L,YB_L,'r.-');
xlabel('X Coordinate');
ylabel('Y Coordinate');
axis equal;

% Plot: Pressure coefficient
figure(2);
cla; hold on; grid on;
set(gcf,'Color','White');
set(gca,'FontSize',12);
plot(X_U,Cp_U,'bo-','LineWidth',2);
plot(X_L,Cp_L,'ro-','LineWidth',2);
xlabel('X Coordinate');
ylabel('Cp');
ylim('auto');
set(gca,'Ydir','reverse')

19 Replies to “Running XFoil from MATLAB”

  1. thanks for sharing this very benfict program, actually i was looking for a start point to use Xfoil with Matlab, It’s a very simple way what y’ve presented here, thank you again.

  2. getting this error

    The logical indices contain a true value outside of the array bounds.

    Error in runxfoil (line 87)
    Cp_L = Cp_0(YB < 0);

    • That’s probably happening because you’re trying to load an airfoil where none of the boundary point Y-coordinates are less than zero. It’s not generally the case, but sometimes with more extreme airfoil shapes you’ll get this issue. I only separate out the lower and upper points for plotting purposes, but you can just plot all the points without separating into lower/upper. That should fix this issue.

  3. Hi
    how can I determine C_l and C_d for this airfoil in a specified AOA & Reynolds number?
    what modifications to this code should I make?
    In this code you have not considered any value for Reynolds Number
    is there any preset value for that?(for example if you don’t enter any value for reynolds 1e6 will be considered automatically)?

  4. Thank you very much Josh. I am your big fan, you are an amazing teacher. I need your help on how to put the output value of xfoil in Genetic algorithm in Matlab as the output of an objective function. I have made a mathematical equation with coefficients which plot the airfoil, the change of coefficients changes the Airfoil shape. I want xfoil to calculate the cl/cd and put that value back to GA in Matlab for optimization. Can you please help it’s very urgent, I have been trying for many days.

    • Rahul, I have been working on the same issue and I think it would be great if we have a discussion on that to come up with the best solution. Even I am using genetic algorithm for airfoil optimization. I am more concerned about high lifting airfoils which are highly chambered. My email id abi13766@gmail.com feel free to contact and have a discussion, will be helpful for both of us.

  5. Hey man, i keep running into a problem when the data is split into upper and lower, the upper matrix always has 2 more data points than the lower (with your code) and so i can’t really manipulate the data

    • Hi Mohamed. I only split the data into two arrays so I can show the upper and lower data with different colors. You should just ignore those lines of code, and keep all the airfoil data together in a single array.

  6. Hey I keep running into this problem where when the data is split up into an upper and lower set, the lower set has 78 numbers in it whereas the upper set has 82. Because of this I can’t really manipulate the data because the matrices are not of the same size. Any suggestions?

  7. Hi man! This is amazing. I was looking for something like it a couple of months ago. Its a shame I’ve only found out about this now! Keep up the great work!

Leave a Reply

Your email address will not be published.

*