MATLAB: Integrating A Vector (Or rather a series of point values)

integratingpointstrapzvector

Firstly I wish to apologise as I know there's a section of the matlab documentation / help files. However I've tried following it and am still stuck, so I was hoping someone here might be able to help me.
I have a series of data taken from (for the purposes of this question) a line. The data is not taken linearly (i.e. the points at which the data is taken along the line are not evenly distributed). I want to integrate the data taken across the length of the line. I know I can do this in matlab, but I just can't seem to work out how I do it.
(The problem below is made up and isn't my actual code, I just want to learn how to do what I want to do so that I can apply it to my actual problem)
Let's say I have a line of length 5m, and I get the following measurements;
(Distance from the start of the line – Data value)
0.6 – 12.6
1.3 – 87.2
2.0 – 74.2
3.0 – 45.3
4.0 – 23.4
5.0 – 14.2
So I want to integrate the values (on the left) along the length of the line. How would I go about doing this? As I said I know I CAN do it, but I'm really lost over how I do it. I've tried following the help files (using trapz) but I ended up getting a value far larger than I expected.
I know this isn't a lot to go on so I understand if nobody is able to help but (as always) any and all help received is greatly appreciated by this newbie to matlab who is trying to demystify it one function at a time 🙂

Best Answer

trapz([0.6 1.3 2:5],[12.6 87.2 74.2 45.3 23.4 14.2])
And to see if it makes sense, look at each trapezoid's contribution:
cumtrapz([0.6 1.3 2:5],[12.6 87.2 74.2 45.3 23.4 14.2])