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/

9 Responses to “Integrating to find the volume underneath a set of nonuniformly spaced data”

  1. Naor replied on :

    Thanks Doug, that was helpful.
    Can you explain about the difference between griddata and interp2, and why you chose griddata in this case?
    -n

  2. dhull replied on :

    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

  3. Daniel Armyr replied on :

    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.

  4. Daniel Armyr replied on :

    And reading the help file I am immediately confronted with “griddata is not recommended. Use TriScatteredInterp instead”. Is there a comment on this?

    –DA

  5. dhull replied on :

    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

  6. matt fig replied on :

    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.

  7. dhull replied on :

    @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

  8. Cari Lim replied on :

    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

  9. dhull replied on :

    @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

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.