# Math Help - Help:Tiny code snippet for area under the curve

1. ## Help:Tiny code snippet for area under the curve

Hello everyone,
I'm new to this forum and I have an issue that I could use some help me. I have written a tiny function to calculate area between two curves.
I have 3-d, cartesian (x,y,z) data from a person performing a movement which is being recorded by IR-cameras.
Now, the function takes in 3 arrays from two separate files(actual data and projected data) namely their x,y and z co-ordinates and then calculates area between two curves separately for the XY plane (think side view) and XZ plane (think top view).
The first curve is data collected from the person. The second curve is data projected by a model.

I need to know how to normalize the output from the function. Since the data starts and stops at different co-ordinates the output area is meaningless until it can be normalized.

Attached is the code. it is quite simple. Im using "cumtrapz".

2. Originally Posted by lonedesi
Hello everyone,
I'm new to this forum and I have an issue that I could use some help me. I have written a tiny function to calculate area between two curves.
I have 3-d, cartesian (x,y,z) data from a person performing a movement which is being recorded by IR-cameras.
Now, the function takes in 3 arrays from two separate files(actual data and projected data) namely their x,y and z co-ordinates and then calculates area between two curves separately for the XY plane (think side view) and XZ plane (think top view).
The first curve is data collected from the person. The second curve is data projected by a model.

I need to know how to normalize the output from the function. Since the data starts and stops at different co-ordinates the output area is meaningless until it can be normalized.

Attached is the code. it is quite simple. Im using "cumtrapz".
You need to be clear about what you are trying to calculate. What you are doing in your code snippet does not calculate the area between curves, at best it calculates the difference in the areas under the curves.

If you are really interested in the area between the curves; that is:

$A=\int_{x=x_0}^{x_1} |y_1(x) - y_2(x)| \; dx$

then one approach is to interpolate the curves onto uniform set of $x$'s, then numericaly integrate the absolute value of difference in the $y$'s at corresponding $x$'s.

If the curves are realy functions of time it might be better to interpolate them onto a set of uniformly spaced time points then you would be interested in something like:

$A=\int_{t=t_0}^{t_1} |(y_1(t) - y_2(t))| \left|\frac{dx}{dt}\right| dt$

RonL

3. ## Hey Captain Black

Hi Captain Black,
Thank you so much for replying. What you say makes sense. However as I said first I am extremely poor with MATLAB programming. Is there some way you could perhaps help me with the code? Or is that too much to ask?
Even a rough sample would suffice. Each input file contains 3 columns of data namely the x,y and z co-ordinates. If need be then I could send you a sample file.
Is it possible?
Once again...thanks so much for replying.

4. Originally Posted by lonedesi
Hi Captain Black,
Thank you so much for replying. What you say makes sense. However as I said first I am extremely poor with MATLAB programming. Is there some way you could perhaps help me with the code? Or is that too much to ask?
Even a rough sample would suffice. Each input file contains 3 columns of data namely the x,y and z co-ordinates. If need be then I could send you a sample file.
Is it possible?
Once again...thanks so much for replying.
If you are not in a hurry yes.

RonL

5. Originally Posted by CaptainBlack
If you are not in a hurry yes.

RonL
Also think about what you really want, if you are trying to measure how well one trajectory models the other you might well be interested in the integral of the absolute value of the 3-D seperation over time, rather than the seperation of the projections on to the x-y and x-z planes.

RonL

6. Im not in a rush. This could wait a day or two. Is that ok?
Thank you thank you thank you !!!
Im going to send a sample file now. Two actually. In the first file called scrap the 2nd,3rd and 4th columns are x,y and z data. In the second file called model the first second and third rows are x,y and z data.

7. The goal here is to see how well the model predicts data so yeah im trying to compare the two data sets. since previous comparisons were done separately in the xy and xz planes i was thinking of moving forward with that. but if you could help me with the code for 2-d then i can definitely try and modify it for 3-d. thanks captain black. believe it or not ive been stuck on this for almost a month now.

8. Originally Posted by lonedesi
The goal here is to see how well the model predicts data so yeah im trying to compare the two data sets. since previous comparisons were done separately in the xy and xz planes i was thinking of moving forward with that. but if you could help me with the code for 2-d then i can definitely try and modify it for 3-d. thanks captain black. believe it or not ive been stuck on this for almost a month now.
Is the first colum in scrap.xls a time, and are the points in the second spreadsheed for the same times?

RonL

9. Yes, the first column in scrap.xls is the time column. The second spreadsheet i.e. model.xls does nt have a time stamp because the inputs to the model are only the starting and ending x,y and z co-ordinates and the model predicts values between the starting and ending points. Unless im mistaken you dont really need to compare the time between actual and model. Ive only been using columns 2,3 and 4 in actual data and columns 1,2 and 3 in model data for my calculations.

10. Originally Posted by lonedesi
Yes, the first column in scrap.xls is the time column. The second spreadsheet i.e. model.xls does nt have a time stamp because the inputs to the model are only the starting and ending x,y and z co-ordinates and the model predicts values between the starting and ending points. Unless im mistaken you dont really need to compare the time between actual and model. Ive only been using columns 2,3 and 4 in actual data and columns 1,2 and 3 in model data for my calculations.
No, can't do this with a reasonable amount of effort because the data are not of a form that makes this easy to do (the fact that the x's in scrap are not monotonic is the first problem).

It would be doable if there were a time index in the model data file or the x's in both files were monotonic, but otherwise this not a problem that can be dealt with in a reasonable amount of time.

RonL