## area under and over the curve

### jackie (view profile)

on 12 Aug 2018
Latest activity Edited by jackie

on 13 Aug 2018

### Image Analyst (view profile)

hello I hope you can help me!
as per diagram below, I have a curve (blue) and a straight line (pink) say y=1.
I will need to find the unit area under the curve (yellow area) and "over" the curve (green area).
also the curve dont have many data points, I hope this wouldn't be a huge problem!

### Image Analyst (view profile)

on 12 Aug 2018

Try this:
xLast = find(y > 1, 1, 'last');
areaUnderCurve = sum(abs(y(1:xLast) - 1))
This is basically assuming the digitized y is a bar chart and you're summing the heights of the rectangular bars. Of use trapz() if you want trapezoids instead of rectangular bars.

jackie

### jackie (view profile)

on 12 Aug 2018
hey Image Analyst, thank you for the prompt reply!
if possible , Can you quickly explain the 2 line of code please if possible?
and also how to use trapz()?
thanks!
Image Analyst

### Image Analyst (view profile)

on 12 Aug 2018
y starts below 1, then rises above 1, then finally drops below 1 again. It's this x coordinate, where it drops below 1 that we need to find because that defines the rightmost place where you want to integrate.
If you do y-1, that will give you the area when y > 1, but that won't work on the left because it will give you negative areas. You'd want 1-y there. But 1-y is simply abs(y-1) so that now allows us to use the same formula everywhere because that will also work for the right area because abs(a positive number) is equal to the positive number.
trapz() is explained in the documentation. Do you really want me to just repeat what's already in the documentation? Basically instead of assuming the areas of each point are rectangular bars, it draws a line from one data point to the adjacent one and this gives you bars with sloping tops rather than flat tops. It's just a different definition and way of doing it. Use whichever you want.

on 13 Aug 2018
Edited by jackie

### jackie (view profile)

on 13 Aug 2018

ok I figured it out, thanks for you help!
x=[0,5,10,20,50,100,150,250,350,450];
b=[4.01E-01,8.30E-01,9.49E-01,1.08E+00,1.21E+00,1.23E+00
,1.20E+00,1.12E+00,1.11E+00,9.69E-01];
line=[1,1,1,1,1,1,1,1,1,1];
x_fine=0:10:450
b_fine = interp1(x,b,x_fine,'spline');
figure
plot(x,b,'o',x_fine,b_fine,'r', x,line,'b');
xFirst = find(b_fine > 1, 1, 'first');
xLast = find(b_fine > 1, 1, 'last');
area_total = trapz(x_fine,b_fine-1)
area_Loss = trapz(x_fine(1:xFirst),b_fine(1:xFirst)-1)
area_Gain = trapz(x_fine(xFirst:xLast),b_fine(xFirst:xLast)-1)