calculating the area under curve using cuntrapz or any other syntax

4 views (last 30 days)
Hello everyone,
I am trying to find out the area under the curve for every 10 sec interval.I have used "cumtrapez" but
How can we calculate for every 10 sec interval separately. One can directly subtract from the consecutive values. however, the variations in data will get aggregated. So,Will you Please suggest any method for calculating each 10 sec interval separately. Please find the sample data below.
Entry Time(x-axis) 1.47 8.93 19.53 23.47 27.1 29.1 34.5 38.53 39.13 49.83 56.2 56.73 61.04 61.04 69.4 71.5 90.5 92.17 101.27
Vehicles(y-axis): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
I will be obliged by your suggestion.
With regards,

Answers (3)

Torsten
Torsten on 17 Mar 2015
First use cumtrapz as usual, then use interp1 on the output array.
Best wishes
Torsten.
  1 Comment
Ajinkya M
Ajinkya M on 17 Mar 2015
Edited: Ajinkya M on 17 Mar 2015
Dear Torsten,
"interp1" will interpolate but I have whole eight hours data. I have to calculate the area under the curve for every 10 sec interval separately.
with regards,
Ajinkya

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 17 Mar 2015
xy =[1.47 1
8.93 2
19.53 3
23.47 4
27.1 5
29.1 6
34.5 7
38.53 8
39.13 9
49.83 10
56.2 11
56.73 12
61.04 13
61.04 14
69.4 15
71.5 16
90.5 17
92.17 18
101.27 19];
x = (xy(1,1):10:xy(end,1))';
y = interp1(xy(:,1),xy(:,2),x);
out = diff(x).*conv2(y,[.5;.5],'valid');
  1 Comment
Ajinkya M
Ajinkya M on 17 Mar 2015
Dear Andrei,
I do not want to interpolate any data.Could you please suggest just to find the area under the curve for every 10 sec interval separately. If we have plotted the below mentioned points on a graph, then for every 10 sec interval what is the area under the plotted graph for every 10 sec interval separately.
1.47 8.93 19.53 23.47 27.1 29.1 34.5 38.53 39.13 49.83 56.2 56.73 61.04 61.04 69.4 71.5 90.5 92.17 101.27
Thanks for your time

Sign in to comment.


John D'Errico
John D'Errico on 17 Mar 2015
While it LOOKS like there will be an aggregation of noise as you move along the curve, this is not true once you then form the difference. Lets look at what cumtrapz does.
cumtrapz does a trapezoidal rule, so over every segment, it computes the area under the corresponding trapezoidal piece. Then cumtrapz does an accumulation.
If there is noise in the y values, then indeed, there WILL be an aggregation of noise in the total (endpoint) computed area. You can even understand the behavior of that error, in a statistical context. Suppose we assume that each y value has noise in it that is normally distributed, with mean 0 and variance sigma. Other noise distributions could apply here, but normally distributed noise is simplest to treat, and the central limit theorem will still apply, so the difference is not that important to my point here.
The idea is that trapezoidal rule is a linear operation, so additive noise on the y values can be treated separately. It is as if we compute the true integral, and then a noise integral on the side. The two integral values are still added together in the end, but the linear nature of trapezoidal rule lets us separate the two.
Each segment of the trapezoidal rule has us average the values of the noise at each endpoint of the segment, and then multiply by the segment width. For simplicity here, I'll assume the segments are of fixed width, call that interval h. If there are n points in the trapezoidal rule, so n-1 segments, then the noise will indeed accumulate along the curve. (But stick with me here...)
We have essentially the sum of n independent normally distributed random numbers, then multiplied by h. Actually, the first and last elements in that sequence are multiplied by h/2, not h. Again, this is a minor tweak that we can pretty much ignore for now.
The expected mean of the noise along the curve as it accumulates will still be zero. But the variance of that noise will grow, so that at step k, the noise variance of the integral will be approximately k*h*sigma^2. (I've been slightly sloppy here. in my back of the envelope computation, but the point remains valid.) The standard deviation will be the square root of that value.
Regardless, when you form the difference to compute the area under each trapezoidal segment, EVERYTHING that happened before a given interval gets subtracted away. There is no accumulation of error anymore when you form a difference.
So just use cumtrapz, and then form the difference with no worry about accumulated noise in the difference.

Products

Community Treasure Hunt

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

Start Hunting!