## how to calculate the area under a curve?

### Davide Cerra (view profile)

on 9 Jul 2019
Latest activity Answered by Star Strider

### Star Strider (view profile)

on 9 Jul 2019
x=[0:100];
y=30-60*cos(2*pi/100*x);
plot (y);
Hello! how con i calculate the area under the curve above? i would also like to calculate portions of that area.
thanks

Jan

### Jan (view profile)

on 9 Jul 2019
I assume that #£ is a typo.
By the way, this is not twitter. No # before the tags. Thanks.

on 9 Jul 2019
Edited by Jan

### Jan (view profile)

on 9 Jul 2019

The area between a curve and the X axis is determined by the integral. So use trapz:
x = 0:100; % Square brackets waste time here only
y = 30 - 60 * cos(2 * pi / 100 * x);
A = trapz(x, y)
You can obtain the integral by hand also here:
30 * (x - 100*sin(pi * x / 50) / pi) + const.
Now insert the limits 0 and 100 to get 3000 as solution.

### Star Strider (view profile)

on 9 Jul 2019

I would also like to calculate portions of that area.
Use cumtrapz, and then subtract the values of the limits:
x=[0:100];
y=30-60*cos(2*pi/100*x);
Int = cumtrapz(x,y);
Intv = @(a,b) max(Int(x<=b)) - min(Int(x>=a));
SegmentArea = Intv(25, 75)
SegmentArea =
3409.2309572264
Checking:
SegmentArea = integral(@(x)30-60*cos(2*pi/100*x), 25, 75)
SegmentArea =
3409.85931710274