How to calculate Area under a curve (negatif side)

1 view (last 30 days)
I have a dataset and I made it curve. I want to calculate the area (like result_2) of ​​the curve but I get a result like the picture on the result_1. Is there anyone who can help with this issue?
f = [-0.390930468901234 -0.346778353936444 -0.304499753808231 -0.264063453713497 -0.225438238849140 -0.188592894412060 -0.153496205599158 -0.120116957607333 -0.0884239356334846 -0.0583859248745134 -0.0299717105273190 -0.00315007778880116 0.0221101881441402 0.0458403020746052 0.0680714788056940 0.0888349331405067 0.108161879882144 0.126083533833705 0.142631109798290 0.157835822579000 0.171728886978934 0.184341517801193 0.195704929848877 0.205850337925086 0.214808956832921 0.222612001375480 0.229290686355865 0.234876226577176 0.239399836842512 0.242892731954974 0.245386126717662 0.246911235933676 0.247499274406116 0.247181456938083 0.245988998332677 0.243953113392996 0.241105016922143 0.237475923723217 0.233097048599318 0.227999606353545 0.222214811789001 0.215773879708783 0.208708024915994 0.201048462213732 0.192826406405098 0.184073072293192 0.174819674681114 0.165097428371965 0.154937548168844 0.144371248874851 0.133429745293087 0.122144252226652 0.110545984478646 0.0986661568521688 0.0865359841503211 0.0741866811762028 0.0616494627329141 0.0489555436235550 0.0361361386512258 0.0232224626190266 0.0102457303300567 -0.00276284341258221 -0.0157720438057912 -0.0287506560464691 -0.0416674653315173 -0.0544912568578337 -0.0671908158223202 -0.0797349274218757 -0.0920923768533994 -0.104231949313792 -0.116122429999953 -0.127732604108783 -0.139031256837181 -0.149987173382047 -0.160569138940281 -0.170745938708783 -0.180486357884452 -0.189759181664190 -0.198533195244895 -0.206777183823467 -0.214459932596806 -0.221550226761814 -0.228016851515387 -0.233828592054427 -0.238954233575834 -0.243362561276507 -0.247022360353347 -0.249902416003253 -0.251971513423126 -0.253198437809864 -0.253551974360368 -0.253000908271538 -0.251514024740273 -0.249060108963473 -0.245607946138040 -0.241126321460871 -0.235584020128867 -0.228949827338928 -0.221192528287954 -0.212280908172844]
t = linspace(0,100,100);
sinus_f = sin(f)
plot(t,sinus_f)
min_f = find(islocalmin(sinus_f));
max_f = find(islocalmax(sinus_f));
axis tight
P1_1 = gca; % gca creates a cartesian axes object
P1_1.XAxisLocation = 'origin'
hold on
basevalue_A1 = 1;
xline(t(min_f),'--b')
yline(sinus_f(min_f),'--r')
xline(t(max_f),'--b')
yline(sinus_f(max_f),'--r')
area_A1 = area(t(max_f:min_f),sinus_f(max_f:min_f),'FaceColor','g') % the area by points

Accepted Answer

Star Strider
Star Strider on 24 Jan 2022
Edited: Star Strider on 24 Jan 2022
I went with the patch function because I’m more familiar with it.
Try ths —
f = [-0.390930468901234 -0.346778353936444 -0.304499753808231 -0.264063453713497 -0.225438238849140 -0.188592894412060 -0.153496205599158 -0.120116957607333 -0.0884239356334846 -0.0583859248745134 -0.0299717105273190 -0.00315007778880116 0.0221101881441402 0.0458403020746052 0.0680714788056940 0.0888349331405067 0.108161879882144 0.126083533833705 0.142631109798290 0.157835822579000 0.171728886978934 0.184341517801193 0.195704929848877 0.205850337925086 0.214808956832921 0.222612001375480 0.229290686355865 0.234876226577176 0.239399836842512 0.242892731954974 0.245386126717662 0.246911235933676 0.247499274406116 0.247181456938083 0.245988998332677 0.243953113392996 0.241105016922143 0.237475923723217 0.233097048599318 0.227999606353545 0.222214811789001 0.215773879708783 0.208708024915994 0.201048462213732 0.192826406405098 0.184073072293192 0.174819674681114 0.165097428371965 0.154937548168844 0.144371248874851 0.133429745293087 0.122144252226652 0.110545984478646 0.0986661568521688 0.0865359841503211 0.0741866811762028 0.0616494627329141 0.0489555436235550 0.0361361386512258 0.0232224626190266 0.0102457303300567 -0.00276284341258221 -0.0157720438057912 -0.0287506560464691 -0.0416674653315173 -0.0544912568578337 -0.0671908158223202 -0.0797349274218757 -0.0920923768533994 -0.104231949313792 -0.116122429999953 -0.127732604108783 -0.139031256837181 -0.149987173382047 -0.160569138940281 -0.170745938708783 -0.180486357884452 -0.189759181664190 -0.198533195244895 -0.206777183823467 -0.214459932596806 -0.221550226761814 -0.228016851515387 -0.233828592054427 -0.238954233575834 -0.243362561276507 -0.247022360353347 -0.249902416003253 -0.251971513423126 -0.253198437809864 -0.253551974360368 -0.253000908271538 -0.251514024740273 -0.249060108963473 -0.245607946138040 -0.241126321460871 -0.235584020128867 -0.228949827338928 -0.221192528287954 -0.212280908172844]
f = 1×100
-0.3909 -0.3468 -0.3045 -0.2641 -0.2254 -0.1886 -0.1535 -0.1201 -0.0884 -0.0584 -0.0300 -0.0032 0.0221 0.0458 0.0681 0.0888 0.1082 0.1261 0.1426 0.1578 0.1717 0.1843 0.1957 0.2059 0.2148 0.2226 0.2293 0.2349 0.2394 0.2429
t = linspace(0,100,100);
sinus_f = sin(f)
sinus_f = 1×100
-0.3810 -0.3399 -0.2998 -0.2610 -0.2235 -0.1875 -0.1529 -0.1198 -0.0883 -0.0584 -0.0300 -0.0032 0.0221 0.0458 0.0680 0.0887 0.1080 0.1257 0.1421 0.1572 0.1709 0.1833 0.1945 0.2044 0.2132 0.2208 0.2273 0.2327 0.2371 0.2405
plot(t,sinus_f)
min_f = find(islocalmin(sinus_f));
max_f = find(islocalmax(sinus_f));
axis tight
P1_1 = gca; % gca creates a cartesian axes object
P1_1.XAxisLocation = 'origin'
P1_1 =
Axes with properties: XLim: [0 100] YLim: [-0.3810 0.2450] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
hold on
basevalue_A1 = 1;
xline(t(min_f),'--b')
yline(sinus_f(min_f),'--r')
xline(t(max_f),'--b')
yline(sinus_f(max_f),'--r')
Lv = t<=t(min_f) & t>=t(max_f);
AUC = trapz(t(Lv), sinus_f(Lv));
text(40, -0.1, sprintf('AUC = %.6f', AUC))
area_A1 = patch([t(Lv) flip(t(Lv))], [ones(size(t(Lv)))*sinus_f(min_f) flip(sinus_f(Lv))], 'g', 'FaceAlpha',0.25);
% area_A1 = area(t(max_f:min_f),sinus_f(max_f:min_f),'FaceColor','g') % the area by points
EDIT — (24 Jan 2022 at 18:18)
Added ‘AUC’ calculation and text display.
.
  4 Comments
Ceren Memis
Ceren Memis on 26 Jan 2022
Now I got exactly the result I wanted. Thank you so much.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!