{"id":278,"date":"2011-06-13T13:25:22","date_gmt":"2011-06-13T13:25:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/06\/13\/calculating-the-area-under-a-surface\/"},"modified":"2011-06-17T14:54:26","modified_gmt":"2011-06-17T14:54:26","slug":"calculating-the-area-under-a-surface","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/06\/13\/calculating-the-area-under-a-surface\/","title":{"rendered":"Calculating the <strike>Area<\/strike> Volume Under a Surface"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Recently there was an email making the rounds at MathWorks about how to calculate the <strike>area<\/strike> volume under a surface.  Not surprisingly,\r\n         there were several methods chosen, based on each sender's proclivities.  Here are some of the ways.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Data Already on a Regular Grid<\/a><\/li>\r\n         <li><a href=\"#4\">Scattered Data : Finding the Convex Hull<\/a><\/li>\r\n         <li><a href=\"#6\">Scattered Data : Finding the <strike>Area<\/strike> volume<\/a><\/li>\r\n         <li><a href=\"#7\">How Do You Work with Scattered Data?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Data Already on a Regular Grid<a name=\"1\"><\/a><\/h3>\r\n   <p>If you have data already on a regular grid, you can simply call <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/trapz.html\"><tt>trapz<\/tt><\/a> twice, once along the <i>X<\/i> dimension, and once along <i>Y<\/i>.  Here's an example. Let's first make some random x and y points.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xdata = [0; rand(100,1); 1];\r\nydata = [0; rand(100,1); 1];\r\nx = sort(xdata);\r\ny = sort(ydata);<\/pre><p>For now, let's calculate our gridded sampled function.  Then use <tt>trapz<\/tt> twice.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[X,Y] = meshgrid(x,y);\r\nZ = X.^2.*sin(3*(X-Y));\r\ntrapz(y,trapz(x,Z,2),1)<\/pre><pre style=\"font-style:oblique\">ans =\r\n      0.13173\r\n<\/pre><p>Let's compare the result to one using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/dblquad.html\"><tt>dblquad<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">F = @(x,y)(x.^2).*sin(3*(x-y));\r\ndblquad(F,0,1,0,1)<\/pre><pre style=\"font-style:oblique\">ans =\r\n      0.13173\r\n<\/pre><h3>Scattered Data : Finding the Convex Hull<a name=\"4\"><\/a><\/h3>\r\n   <p>MATLAB has the ability to deal with scattered data in a variety of ways. I've just shown, using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/meshgrid.html\"><tt>meshgrid<\/tt><\/a> a technique for integrating a function that you know by sampling, and comparing this result to numerically integrating the\r\n      same function that generated the samples. I'll take advantage of some of the newer <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/math\/br81y5b-1.html\">computational geometry<\/a> functionality in MATLAB in this next attempt.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load <span style=\"color: #A020F0\">seamount<\/span>\r\nminz = min(z);\r\nzadj = z-min(z);<\/pre><p>Note that I \"normalized\" the depth data (<tt>z<\/tt>), since it is negative, and changed it into the array <tt>zadj<\/tt> that is all non-negative.  Next I create an interpolant from the scattered data.  Then integrate this function using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/quad2d.html\"><tt>quad2d<\/tt><\/a>, but not before trimming the minimum and maximum values for <tt>x<\/tt> and <tt>y<\/tt> so I won't run into extrapolated values (which are <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/nan.html\"><tt>NaN<\/tt><\/a>).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">F2 = TriScatteredInterp(x,y,zadj);\r\nq1 = quad2d(@(x,y) F2(x,y),211.1,211.4,-48.35,-48,<span style=\"color: #A020F0\">'AbsTol'<\/span>,0.01)<\/pre><pre style=\"font-style:oblique\">q1 =\r\n       136.04\r\n<\/pre><h3>Scattered Data : Finding the <strike>Area<\/strike> Volume<a name=\"6\"><\/a><\/h3>\r\n   <p>If you have access to <a href=\"https:\/\/www.mathworks.com\/products\/curvefitting\/\">Curve Fitting Toolbox<\/a>, you can take advantage of the relatively new capability for fitting surfaces.  I'll use the same seamount data as before,\r\n      created a fit to the data, and, from the function that comes from the fit, use <tt>quad2d<\/tt> again to compute the <strike>area<\/strike> volume.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">f = fit([x, y], zadj, <span style=\"color: #A020F0\">'linearinterp'<\/span>);\r\nq2 = quad2d(f,211.1,211.4,-48.35,-48,<span style=\"color: #A020F0\">'AbsTol'<\/span>,0.01)<\/pre><pre style=\"font-style:oblique\">q2 =\r\n       136.04\r\n<\/pre><h3>How Do You Work with Scattered Data?<a name=\"7\"><\/a><\/h3>\r\n   <p>If you have scattered data representing a surface, how do you think about it and work with it - numerically or by fitting\r\n      a function of some sort? Do you have other techniques?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=278#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_da31c53d878445e3b5ca97fa53caa1b2() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='da31c53d878445e3b5ca97fa53caa1b2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' da31c53d878445e3b5ca97fa53caa1b2';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_da31c53d878445e3b5ca97fa53caa1b2()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.12<br><\/p>\r\n<\/div>\r\n<!--\r\nda31c53d878445e3b5ca97fa53caa1b2 ##### SOURCE BEGIN #####\r\n%% Calculating the Volume Under a Surface\r\n% Recently there was an email making the rounds at MathWorks about how to\r\n% calculate the volume under a surface.  Not surprisingly, there were several\r\n% methods chosen, based on each sender's proclivities.  Here are some of\r\n% the ways.\r\n%% Data Already on a Regular Grid\r\n% If you have data already on a regular grid, you can simply call\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/trapz.html\r\n% |trapz|> twice, once along the _X_ dimension, and once along _Y_.  Here's\r\n% an example. Let's first make some random x and y points.\r\nxdata = [0; rand(100,1); 1]; \r\nydata = [0; rand(100,1); 1]; \r\nx = sort(xdata); \r\ny = sort(ydata);\r\n%%\r\n% For now, let's calculate our gridded sampled function.  Then use |trapz|\r\n% twice.\r\n[X,Y] = meshgrid(x,y);\r\nZ = X.^2.*sin(3*(X-Y));\r\ntrapz(y,trapz(x,Z,2),1)\r\n\r\n%%\r\n% Let's compare the result to one using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/dblquad.html\r\n% |dblquad|>.\r\nF = @(x,y)(x.^2).*sin(3*(x-y));\r\ndblquad(F,0,1,0,1)\r\n\r\n%% Scattered Data : Finding the Convex Hull\r\n% MATLAB has the ability to deal with scattered data in a variety of ways.\r\n% I've just shown, using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/meshgrid.html\r\n% |meshgrid|> a technique for integrating a function that you know by\r\n% sampling, and comparing this result to numerically integrating the same\r\n% function that generated the samples. I'll take advantage of some of the\r\n% newer\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/math\/br81y5b-1.html\r\n% computational geometry> functionality in MATLAB in this next attempt. \r\nload seamount\r\nminz = min(z);\r\nzadj = z-min(z);\r\n%%\r\n% Note that I \"normalized\" the depth data (|z|), since it is negative, and\r\n% changed it into the array |zadj| that is all non-negative.  Next I create\r\n% an interpolant from the scattered data.  Then integrate this function\r\n% using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/quad2d.html\r\n% |quad2d|>, but not before trimming the minimum and maximum values for |x|\r\n% and |y| so I won't run into extrapolated values (which are\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/nan.html |NaN|>).\r\nF2 = TriScatteredInterp(x,y,zadj);\r\nq1 = quad2d(@(x,y) F2(x,y),211.1,211.4,-48.35,-48,'AbsTol',0.01)\r\n%% Scattered Data : Finding the Volume\r\n% If you have access to <https:\/\/www.mathworks.com\/products\/curvefitting\/\r\n% Curve Fitting Toolbox>, you can take advantage of the relatively new\r\n% capability for fitting surfaces.  I'll use the same seamount data as\r\n% before, created a fit to the data, and, from the function that comes from\r\n% the fit, use |quad2d| again to compute the volume.\r\nf = fit([x, y], zadj, 'linearinterp');\r\nq2 = quad2d(f,211.1,211.4,-48.35,-48,'AbsTol',0.01)\r\n%% How Do You Work with Scattered Data?\r\n% If you have scattered data representing a surface, how do you think about\r\n% it and work with it - numerically or by fitting a function of some sort?\r\n% Do you have other techniques?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=278#respond here>.\r\n\r\n##### SOURCE END ##### da31c53d878445e3b5ca97fa53caa1b2\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Recently there was an email making the rounds at MathWorks about how to calculate the area volume under a surface.  Not surprisingly,\r\n         there were several methods chosen, based... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/06\/13\/calculating-the-area-under-a-surface\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,39,23],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/278"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}