How to calculate Area under a curve (negatif side)
1 view (last 30 days)
Show older comments
Ceren Memis
on 24 Jan 2022
Commented: Star Strider
on 26 Jan 2022
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
0 Comments
Accepted Answer
Star Strider
on 24 Jan 2022
Edited: Star Strider
on 24 Jan 2022
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]
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')
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
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!