AUC or trapz for irregular shape that intersects itself
1 view (last 30 days)
Show older comments
I am trying to figure out how to calculate area under the curve (AUC) for an irregular shape that intersects itself several times. The data is the length and force of a muscle that creates a thing called "work loops". The area inside of the loops are considered negative (clockwise directionality) and positive (counter-clockwise direcrtinality) work. If I could know both negative and positive work, that would be great. Although I will settle for overall work done or area inside.
I have used trapz, but that seems to not give me a consistent postive or negative number. Additionally, I cannot subset the data because the indexes needed to subset are not consecutive in some areas.
WL_1546Fa = readmatrix('~/Desktop/NAU LAB/Data/Rat/Experiments/1546/1546_WL.csv','Range','C23:C1427'); %this is where it is on my computer and hwat I need selected
WL_1546La = readmatrix('~/Desktop/NAU LAB/Data/Rat/Experiments/1546/1546_WL.csv', 'Range', 'F23:F1427'); %same here
plot(WL_1546La, WL_1546Fa); %plotting to see
trapz(WL_1546La, WL_1546Fa)
(The graph goes from left to the loop on the right and then turns back on itself to cross around 0.6 and create the other loops)
Accepted Answer
Bruno Luong
on 3 Aug 2022
Edited: Bruno Luong
on 4 Aug 2022
I use data you provided on other thread (the excel is horrible to read, if you could provide the data in mat file format it's better for everyone);
The carea function returns positive when the area is counter clockwise, and vice versa. If the curve makes different loops with different orientations, it will sum them correctly as showed here
x=[0 3 3 1 1 0 0];
y=[0 0 2 2 -1 -1 0];
plot(x,y,'r');
axis([-1 4 -2 3])
axis equal
carea(x,y) % 3 = 4-1, because the right square is ccw = +4 the left square is cw= -1
Applied on your data
data = readtable('data.csv');
xy=table2array(data);
x=xy(:,1);
y=xy(:,2);
carea(x,y) % attached function
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!