Doug’s MATLAB Video Tutorials
September 8th, 2009
Integrating to find the volume underneath a set of nonuniformly spaced data
This video covers the use of GRIDDATA, anonymous functions, and QUAD2D to integrate the volume under a set of randomly spaced data points. First an interpolation scheme must be put in place, then a numeric quadrature function is invoked.
This video shows the creation of a good synthetic data set with a known volume to test the rest of the algorithm. A good test data set like this really give confidence in the building of your algorithm.
Here is the code that was written:
% I have a dataset "A" (n by 3) of ordered triplets [x,y,z].
%I want to calculate the volume between surface defined by "A" and
%the xy plane. "A" has only positive values but is not uniformly
%spaced and not gridded. "A" cannot be described by a simple
%function. Any help?
n = 10;
randOffset = 0.1;
h = 1;
x = rand(n);
x(1:4)=[0 1 0 1]';
y = rand(n);
y(1:4)=[0 0 1 1]';
z = h + randOffset*rand(n) - randOffset/2; %make average height
plot3(x,y,z,'.')
axis equal
zlim([0 h + randOffset])
interpZ = @(xi,yi) griddata(x,y,z,xi,yi) %set up interpolation
interpZ(0.5,0.5) %test interpolation
vol = quad2d(interpZ,0,1,0,1) %volume should be close to 1
Note: there is an update to this post here:
http://blogs.mathworks.com/videos/2009/09/18/revisited-integrating-to-find-the-volume-underneath-a-set-of-non-uniformly-spaced-data/
15:27 UTC |
Posted in Format: Video, Level: Basic, Topic: Practical example |
Permalink |
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
 |
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
|
 |
|
Thanks Doug, that was helpful.
Can you explain about the difference between griddata and interp2, and why you chose griddata in this case?
-n
Naor,
Thanks for the kind words! My first instinct was to use INTERP2, but that expects the data to be ‘plaid’ but this given data was not regular enough to do that. GRIDDATA is much more forgiving.
-Doug
Hi.
Best video in a long time. Interpolation is a strange beast about whose handling I still have much to learn. I will now go and apply this new knowledge to a problem I was working on last week.
Thank you.
And reading the help file I am immediately confronted with “griddata is not recommended. Use TriScatteredInterp instead”. Is there a comment on this?
–DA
Daniel,
Thank you for the kind words. I am not sure which help you were reading that said this (I looked but could not find it). What version, and which function?
Doug
Doug,
Enter this into google (with quotes, as shown):
“Use TriScatteredInterp instead”
Google finds two hits, both link to MATLAB documentation on griddata and griddata3. This is new to me too.
@Matt,
I think you will be interested in this also:
http://blogs.mathworks.com/videos/2009/09/18/revisited-integrating-to-find-the-volume-underneath-a-set-of-non-uniformly-spaced-data/
Thanks,
Doug
Hi,
Does this code require fitting of a function to the x,y and z data? What if my data is unable to be fitted to a function, is there a code that can find the volume under this irregular surface still?
Thank you.
Cari
@cari,
To integrate the area under your set of Z points, you must make some kind of assumptions as to what happens between X,Y points that you know since there is no volume under a point!
Fitting a surface to the data does that.
Depending on your data, you could simply do a nearest neighbor interpolation, or a linear interpolation. What is the nature of your Z data, and what is the spacing of the X and Y?
-Doug