3 views (last 30 days)

Show older comments

Sara Oliveira Pedro dos Santos
on 14 May 2021

Commented: Sara Oliveira Pedro dos Santos
on 18 May 2021

Hello all,

I want to calculate the area between points that create a curve.

I am using MATLAB to simulate particles being advected on a plane. I have the initial position of those particles and I have the final position. I want to calculate the area between the initial position and final position of the particles

The points are in the attached mat file, where px is the position x of the particles for 10 frames and py is the position y of the particles for 10 frames. I want to calculate the area (green in image) between px(:,:,1),py(:,:,1) (black line in image) and px(:,:,10),py(:,:,10) (points with red line). Below is a plot of the final position of the points. One of the approaches I tried was fitting the data with a polynomial curve but that would introduce a significant error in the calculations and I want to avoid that.

How can I calculate the area without fitting the data?

Thank you!

DGM
on 15 May 2021

Edited: DGM
on 15 May 2021

There are probably a bunch of ways to do this. Considering that the endpoint trajectories are not aligned to the reference grid, I figured I'd just do it this way, even if the error would have been negligible otherwise.

S = load('points.mat');

px0 = squeeze(S.px(:,:,1));

py0 = squeeze(S.py(:,:,1));

pxf = squeeze(S.px(:,:,end));

pyf = squeeze(S.py(:,:,end));

% get rid of nans

px0 = px0(~isnan(px0));

py0 = py0(~isnan(py0));

pxf = pxf(~isnan(pxf));

pyf = pyf(~isnan(pyf));

% order points to form a polygon

x = [px0; flipud(pxf)];

y = [py0; flipud(pyf)];

sh = polyshape(x,y);

plot(sh); grid on

axis tight

area(sh) % this is the area of the calculated polygon

Of course, the first dataset isn't as described by your image. If there's an offset or if px0, py0 are something other than the first vectors in in the arrays, then you'll have to make that change.

If you simply want to exclude the overlap area, then you can do this

% truncate overlap

x = [px0; max(flipud(pxf),interp1(py0,px0,pyf))];

sh = polyshape(x,y);

plot(sh); grid on

axis tight

area(sh)

If the overlapped regions are to be considered as some sort of negative area, then you can use a similar technique to calculate it (or just take the difference of the two methods given).

polyshape() will spit out warnings in either case, but they're just warnings. It's just telling you that the polygon intersects itself, which is easy enough to tell just by looking.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!