MATLAB: How to calculate the area between these two lines

integralinterpolationtrapz

I am trying to find the area between these two curves that I got from XFOIL. Is there a simple way to do this? I was able to export the coordinates of the two curves and put them into matlab as X1, Y1, X2, and Y2. I'm guessing I'm gonna have to do some sort of interpolation, because though the two curves begin and end at the same XY values, the number of values is different. Sorry if this is a repeat question, but I couldn't find any problems in the forum that are similar to my case. Any advice would be greatly appreciated!

Best Answer

First, interpolate them to the same ‘x’ vector creating the ‘x’ vector with linspace and using interp1 for the interpolations, then use the trapz function to integrate each, and subtract one from the other:
X1 = load('x1.mat');
Y1 = load('y1.mat');
X2 = load('x2.mat');
Y2 = load('y2.mat');
x1 = X1.x1;
y1 = -Y1.y1;
x2 = X2.x2;
y2 = -Y2.y2;
xq = linspace(min([x1;x2]), max([x1;x2]))'; % New ‘x’ Vector For Interpolation
y1i = interp1(x1, y1, xq, 'linear','extrap'); % Interpolate To ‘xq’

y2i = interp1(x2, y2, xq, 'linear','extrap'); % Interpolate To ‘xq’
Area1 = trapz(xq, y1i);
Area2 = trapz(xq, y2i);
AreaDif = Area1 - Area2;
producing:
AreaDif =
0.495256997199081
If you want the cumulative areas, use cumtrapz instead. The rest of the code is unchanged.
EDIT — (3 Feb 2020 at 17:56)
Corrected typographical errors.