{"id":392,"date":"2009-09-18T16:00:35","date_gmt":"2009-09-18T16:00:35","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/2009\/09\/18\/revisited-integrating-to-find-the-volume-underneath-a-set-of-non-uniformly-spaced-data\/"},"modified":"2009-09-18T16:00:35","modified_gmt":"2009-09-18T16:00:35","slug":"revisited-integrating-to-find-the-volume-underneath-a-set-of-non-uniformly-spaced-data","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2009\/09\/18\/revisited-integrating-to-find-the-volume-underneath-a-set-of-non-uniformly-spaced-data\/","title":{"rendered":"Revisited: Integrating to find the volume underneath a set of non uniformly spaced data"},"content":{"rendered":"Well, am I turning red!  Soon after I posted my last video about <a href=\"https:\/\/blogs.mathworks.com\/videos\/2009\/09\/08\/integrating-to-find-the-volume-underneath-a-set-of-nonuniformly-spaced-data\/\">how to find the volume underneath a set of non uniformly spaced data<\/a>, I found one of our developers had a better way to do it using features of the <a href=\"https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html\">newly released MATLAB 2009b<\/a>.\r\n<p>\r\nHere is the video again:\r\n<p>\r\n\r\n<div><div class=\"row\"><div class=\"col-xs-12 containing-block\"><div class=\"bc-outer-container add_margin_20\"><videoplayer><div class=\"video-js-container\"><video data-video-id=\"3877437981001\" data-video-category=\"blog\" data-autostart=\"false\" data-account=\"62009828001\" data-omniture-account=\"mathwgbl\" data-player=\"rJ9XCz2Sx\" data-embed=\"default\" id=\"mathworks-brightcove-player\" class=\"video-js\" controls><\/video><script src=\"\/\/players.brightcove.net\/62009828001\/rJ9XCz2Sx_default\/index.min.js\"><\/script><script>if (typeof(playerLoaded) === 'undefined') {var playerLoaded = false;}(function isVideojsDefined() {if (typeof(videojs) !== 'undefined') {videojs(\"mathworks-brightcove-player\").on('loadedmetadata', function() {playerLoaded = true;});} else {setTimeout(isVideojsDefined, 10);}})();<\/script><\/div><\/videoplayer><\/div><\/div><\/div>\r\n <\/div>\r\n<p>\r\nLet&#8217;s repeat the code here from the beginning\r\n<hr>\r\n\r\n<pre>\r\n<code>\r\n\r\nn = 10;\r\nrandOffset = 0.1;\r\nh = 1;\r\nx = rand(n);\r\nx(1:4)=[0 1 0 1]';\r\ny = rand(n);\r\ny(1:4)=[0 0 1 1]';\r\nz = h + randOffset*rand(n) - randOffset\/2; %make average height\r\nplot3(x,y,z,'.')\r\naxis equal\r\nzlim([0 h + randOffset])\r\n\r\n<\/code>\r\n<\/pre>\r\n\r\n\r\nNow, the new feature being used is the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/curvefit\/fit.html\">fit<\/a> command from the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/curvefit\/index.html?\/access\/helpdesk\/help\/releases\/R2009b\/toolbox\/curvefit\/curvefit.shtml&#038;http:\/\/www.google.com\/search%3fq=matlab+fit+documentation&#038;ie=utf-8&#038;oe=utf-8&#038;aq=t&#038;rls=org.mozilla:en-US:official&#038;client=firefox-a\">Curve Fitting Toolbox<\/a>:\r\n\r\n<pre><code>\r\n\r\nsf = fit( [x(:), y(:)], z(:), 'linearinterp' )\r\n\r\n     Linear interpolant:\r\n       sf(x,y) = piecewise linear surface computed from p\r\n     Coefficients:\r\n       p = coefficient structure\r\n\r\n<\/code>\r\n<\/pre>\r\n\r\nThen the volume is found using more or less the same call to QUAD2D. I told you it was cool:\r\n\r\n<pre><code>\r\n\r\nvol = quad2d( sf, 0, 1, 0, 1 )\r\n\r\nvol =\r\n       1.0038\r\n\r\n<\/code>\r\n<\/pre>\r\n\r\nCaveats\r\n\r\nYou&#8217;ll notice that I had appended some &#8220;special points&#8221; to this data set. This is so that we can interpolate into the corners. The &#8220;fit object&#8221;, sf, will suffer the same problem as it is built on top of GRIDDATA. Another way around this issue is to use a scheme that extrapolates,\r\n<p>\r\n<ul>\r\n\r\n    \t<li>Use a different interpolation scheme, e.g., &#8216;nearest&#8217;\/ &#8216;nearestinterp&#8217; or &#8216;v4&#8217;\/ &#8216;biharmonicinterp&#8217; (I&#8217;d use the latter because it is the best). This will work for both fit and GRIDDATA.\r\n<\/li>\r\n\r\n    \t<li>Fit the data in a least squares sense, e.g., using &#8216;lowess&#8217;.<\/li>\r\n\r\n<\/ul>\r\n\r\n\r\nExtrapolation should be OK in this case as we are not extrapolating too far from the data.\r\n<hr>\r\n\r\n\r\nSometimes, I think I write this blog so that *I* can learn more MATLAB from the people that read it!  Your comments and suggestions are always welcome below!","protected":false},"excerpt":{"rendered":"<div class=\"thumbnail thumbnail_asset asset_overlay video\"><a href=\"https:\/\/blogs.mathworks.com\/videos\/2009\/09\/18\/revisited-integrating-to-find-the-volume-underneath-a-set-of-non-uniformly-spaced-data\/?dir=autoplay\"><img decoding=\"async\" src=\"https:\/\/cf-images.us-east-1.prod.boltdns.net\/v1\/static\/62009828001\/76f1e06a-90df-400a-bf69-f568c146203b\/572683bf-27e3-4e17-bf71-093138d795f1\/1280x720\/match\/image.jpg\" onError=\"this.style.display ='none';\"\/>\n      <div class=\"overlay_container\">\n      <span class=\"icon-video icon_color_null\"><time class=\"video_length\">5:50<\/time><\/span>\n      <\/div>\n      <\/a><\/div><p>Well, am I turning red!  Soon after I posted my last video about how to find the volume underneath a set of non uniformly spaced data, I found one of our developers had a better way to do it using&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2009\/09\/18\/revisited-integrating-to-find-the-volume-underneath-a-set-of-non-uniformly-spaced-data\/\">read more >><\/a><\/p>","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/392"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=392"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/392\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}