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

107 views (last 30 days)
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 :)

Accepted Answer

Sean de Wolski
Sean de Wolski on 5 Apr 2012
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])
  3 Comments
Sean de Wolski
Sean de Wolski on 5 Apr 2012
Yes. You could add 0 and 5 to the ends of the x vector. However, you will have to come up with y-values to use at these points. If it's known to be zero or something then this is trivial. Otherwise you'll need to either extrapolate or use the last value.
Stephen
Stephen on 5 Apr 2012
Ah, thank you very much, makes sense to me. Truth be told that's another one where I feel a bit silly for not thinking of!
Thank you very much Sean, you've been extremly helpful :)

Sign in to comment.

More Answers (1)

Jonathan Sullivan
Jonathan Sullivan on 5 Apr 2012
x = [0.6 1.3 2 3 4 5]; % Your x values
y = [12.6 87.2 74.2 45.3 23.4 14.2]; % Your y values
Area = trapz(x,y) % The area under the curve
For more information you can type
help trapz
doc trapz
  1 Comment
Stephen
Stephen on 5 Apr 2012
Thank you very much :) I accepted Sean's answer as his was first however you're answer is just as helpful :)
I've posted a follow up question on Sean's answer which I won't repost here, but I will say once again thank you for taking the time to help me out. :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!