4.86364

4.9 | 44 ratings Rate this file 462 downloads (last 30 days) File Size: 822.2 KB File ID: #8998

Surface Fitting using gridfit

by John D'Errico

 

11 Nov 2005 (Updated 22 Aug 2006)

No BSD License  

Model 2-d surfaces from scattered data

Editor's Notes:

This file was a File Exchange Select file.

Select files are submissions that have been peer-reviewed and approved as meeting a high standard of utility and quality.

Download Now | Watch this File

File Information
Description

Those wishing to model a surface from data in the  
form of z(x,y) from scattered or semi-scattered  
data have had few options in matlab - mainly  
griddata.  
 
Griddata is a valuable tool for interpolation of  
scattered data. However it fails when there are  
replicates or when the data has many collinear  
points. Griddata is also unable to extrapolate  
beyond the convex hull of the data unless the 'v4'  
option is used, which is slow.  
 
Gridfit solves all of these problems, although it  
is not an interpolant. It builds a surface over a  
complete lattice, extrapolating smoothly into the  
corners. You have control of the amount of  
smoothing done, as well as interpolation methods,  
which solver to use, etc.  
 
This release allows the user to solve much larger problems using a new tiling option. There is essentially no limit on the size of the suface one builds now, as long as you have dense enough data and enough memory to store the final gridded surface.  
 
Example uses are found in the file gridfit_demo.m,  
as well as comparisons to griddata on the same  
surfaces.

Acknowledgements
This submission has inspired the following:
HOW TO PLOT A COLOURED SURFACE FROM 3D SCATTER
MATLAB release MATLAB 7.0.1 (R14SP1)
Other requirements Gridfit should be accessible to older versions of matlab, as long as symamd and colamd are available. Older releases could use one of the iterative solvers, such as symmlq.
Zip File Content  
Published M Files gridfit_demo
Other Files
gridfitdir/.DS_Store,
gridfitdir/bluff_data.mat,
gridfitdir/gridfit.m,
gridfitdir/ravine.jpg,
gridfitdir/demo/.DS_Store,
gridfitdir/demo/gridfit_demo.m,
gridfitdir/doc/Understanding_gridfit.rtf,
gridfitdir/test/.DS_Store,
gridfitdir/test/test_main.m,
gridfitdir/demo/html/gridfit_demo_10.png,
gridfitdir/demo/html/gridfit_demo_09.png,
gridfitdir/demo/html/gridfit_demo_01.png,
gridfitdir/demo/html/gridfit_demo_02.png,
gridfitdir/demo/html/gridfit_demo_03.png,
gridfitdir/demo/html/gridfit_demo_04.png,
gridfitdir/demo/html/gridfit_demo_05.png,
gridfitdir/demo/html/gridfit_demo_06.png,
gridfitdir/demo/html/gridfit_demo_07.png,
gridfitdir/demo/html/gridfit_demo_08.png,
gridfitdir/demo/html/gridfit_demo.png
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (54)
11 Nov 2005 Michael Robbins Neat picture!
16 Nov 2005 Urs (us) Schwarz simply said: just another extremely useful member of the d'erriconian-draconian family of must-haves if you're professionally serious about nd-data investigation / reconstruction / interpolation / visualization, which smoothly unites with its now-famous siblings (consolidator, inpaint-nans) into a very nice toolbox for those everyday cheating-with-data endeavors...  
the code is very clean and the profiler reveals no unnecessary hot-spot...  
HOWEVER, in contrast, the help bit is just unbelievably clumsy and almost indigestible without sever anxiolytic medication - JOHN, JOHN! this part requires some serious overhaul and sprucin'-up...  
altogether, very well done - and thank you for this snippet  
us
13 Dec 2005 Chaowen Liang This is exactly what I am looking for... Thanks you!!! Save me a lot of time to write one...
13 Dec 2005 Alberto Baraldi I was struggling against griddata since days, beacause of its problems when extrapolating...this is absolutely great!  
Thank you for your great help to the Matlab community. We love you
16 Dec 2005 Kong Zuor Well done John, I have been looking for this while ago and I think it is a must have code. Thank you very much for being so helpful to mathlab community
10 Jan 2006 CASTRY Alen Thank you...
11 Feb 2006 Julio Oliveira Great ! Thank you!
21 Feb 2006 David Barker This is an excellent program. It makes it easy to fit surfaces to 3D data.
06 Mar 2006 Kevin Denis  
08 Mar 2006 T O Excellent Work!
25 Apr 2006 Wen Soong Does a great job!
05 May 2006 Aslak Grinsted Should be included into the default distribution of matlab! perhaps somehow integrated into griddata?
27 May 2006 thank you Marvelous!
29 May 2006 Andy Lulham  
16 Jun 2006 Håvard Torpe Works like a charm right from the moment you start using it.  
 
However: I'd like gridfit() NOT to extrapolate values for gridnodes that has no datapoint in the vicinity. Any ideas?
19 Jun 2006 John D'Errico I'll invite Håvard Torpe to contact me via e-mail to discuss this idea. But while I COULD allow the user to turn off extrapolation in gridfit, this will likely cause singularities - serious numerical problems, for most users who might do so.
21 Aug 2006 Christopher Schwalm Excellent program and documentation.  
 
A question: the help states "Gridfit is not an interpolant." Why is that? It seems to be perfectly able to be used as such, given the outputs and how they were generated. One use I see in my work is to use this function to interpolate where repeated values are present. I've yet to find another Matlab solution that works as well as this does.
13 Oct 2006 Wolfgang Schwanghart Excellent! This is exactly what I was searching for. Thank you very much for this great contribution.
26 Oct 2006 Phani Ivatury Excellent!!!
02 Nov 2006 Thomas Clark Excellent function, John - this has been extremely useful to me.  
 
The ability to turn off extrapolation beyond the bounds of the data would be very useful, though.  
 
A note for the inexperienced user:  
If you have repetitions (multiple points with the same (x,y) but different (z)), this algorithm will handle where griddata might fall. In such a case, think hard about why you have these repetitions; it may be that volumetric plotting is more appropriate than surfaces. If so, you could get a good-looking surface out of this algorithm; which isn't totally meaningful.  
 
 
That's not a fault with this algorithm; it just means that you have to think hard about why you are getting repeated points in Z: If it's acceptable to average these out to a surface, this algorithm is for you!
09 Nov 2006 Carlos Adrián Vargas Aguilera Very very good John! Take good care of Amy!
14 Dec 2006 E Farhi This is simply great, with all we can expect for a replacement to griddata (which fails too often).
07 Jan 2007 Zhijun Wang This is very very good work!
19 Jan 2007 Ken Lin Thanks for your programs. It solves my problem now:)  
by the way, would you like to work out some way to interpolate the data just like the surfer does
01 Feb 2007 David Sprinkle Great tool and insightful docs! Gridfit succeeds where griddata fails; that is, with noisy & ill-spaced data such as exist in the real world. Thanks!
17 Apr 2007 gabriele flauti  
24 May 2007 Juan Perez  
02 Aug 2007 Hendawi Mohamed I just write to say..."Thank you"!  
 
PS. but I add: It would be perfect if there was a way to sidestep the extrapolation of the the gridnodes beyond the datapoint boundary. It should be added in matlab next release!
05 Nov 2007 Darren Paget Thanks heaps for the function, it saved me a lot of work. Surface fitting has come a long way! absolutely awesome fit to my data. and really easy to use.
28 Nov 2007 Henrique Mendes It is not working! Matlab says:  
"??? Input argument "x" is undefined.  
Error in ==> gridfit at 325  
x=x(:);"  
I tried to change x(:) to x(:,1), but the problem kepps the same. My Matlab is version 7. It had to be more recent?
28 Nov 2007 John D'Errico Henrique - My guess is that you have called gridfit with no arguments. This code is not a gui or command. See the demos for examples of the use of this code. It truly does work, at least when called with data. John
10 Dec 2007 Gassilloud rémy How to use gridfit with polar coordinates ?  
[th,r] = meshgrid((0:5:360)*pi/180,0:5:300);  
[X,Y] = pol2cart(th,r);  
Z=griddata(x,y,z,X,Y);  
 
Thank you,  
Rémy
22 Jan 2008 Tim Marvin  
12 Feb 2008 V P What is wrong with calling GRIDFIT? Needs improvement.  
 
Z=rand(100);  
a=1:100;  
[X,Y]=meshgrid(a,a);  
good=(X-50).^2+(Y-50).^2>100;  
x=X(good);  
y=Y(good);  
z=Z(good);  
bad=~good;  
xi=X(bad);  
yi=Y(bad);  
zi=gridfit(x,y,z,xi,yi);  
??? Error using ==> gridfit at 404  
xnodes and ynodes must be monotone increasing  
12 Feb 2008 John D'Errico The problem that VP has is he failed to read the help, or apparently bother to understand what gridfit does. I'd recommend reading the help, and perhaps looking at the examples when you don't understand what you are using. I do understand that real men don't need no steenkin help. Perhaps this is the approach that VP has followed.  
 
Gridfit does not interpolate a surface at some random scattered list of points, as would griddata.  
 
Gridfit generates a surface from scattered data on a complete, regular lattice of points. The node arguments are what he might have passed into meshgrid. Again, READ THE HELP.  
13 Feb 2008 V P 1. In strict accordance with the help, xi and yi WERE x-nodes and y-nodes of interest, generated by MESHGRID. If this kind of input is not accepted by GRIDFIT, this means that GRIDFIT must be improved.  
2. This is also necessary for comparison with GRIDDATA, which has no problems with my example.  
3. If these arguments are still insufficient, and you insist that I am incorrect, then you have to improve the description of xi and yi in your help and define them as VALID INPUT arguments for MESHGRID. However, this will again mean an improvement.  
4. If this is not convincing for you, look at the example below. If it is not a strong indication of necessary improvements, then I have to follow the principle of "haende khokh" - this is much better than the cold war around an empty egg.  
 
Z=rand(100);  
a=(1:100)';  
[X,Y]=meshgrid(a,a);  
z=gridfit(X(:),Y(:),Z(:),a,a);  
norm(z(:)-Z(:))  
ans =  
    4.9225
21 Feb 2008 Alex Gardner  
08 Mar 2008 Carlos Adrián Vargas Aguilera Hi John:  
    
I've been using your incredible program for some years and in a wide range of applications. My question is, do you have written any kind of documentation in a scientific journal, for example, for any citation and also to know a little bit more about this super "surface fitting". Any reference can help too.  
    
BTW: VP uses the more silly test to "compare" GRIDDATA and GRIDFIT, and, as John says, he doesn't bother to read that by default the former is 'linear' and the latter uses 'smooth' to 1. But, any way...
29 Jul 2008 Anmol Agrawal Excellent utility!! A default setting did everything I needed with minimal error.
11 Sep 2008 gai cs it is very good toolbox ?thanks
20 Sep 2008 Andrey Matseevskiy You wrote "For example, my computer took 1020 seconds to solve a 500x500 problem." What an old comp do you use? I need 5 second to solve 500x500 problem. Download my prog from http://www.smartfills.com/Html/2D.zip . Compare with your own method. My own is in fact interpolation, but could be modified for approximation without much problems.  
Andrey, Kamchatka
20 Sep 2008 John D'Errico The review by Andrey is a bit of self serving braggadocio, since he does not actually offer Matlab code on the file exchange that replicates in any form what gridfit does. By the way, the link he gives is for a .exe file - BEWARE.  
 
His time comparisons are also meaningless of course, since the times referred to are for a now wildly obsolete computer, and comparing an interpolation code to this class of modeling code makes no sense anyway.  
 
Finally, converting an interpolation code to a noise reducing surface modeling code is not at all trivial. I welcome Andrey to do so and provide MATLAB code for the purpose, if that is truly so easy.
14 Oct 2008 Cedric Testaz  
20 Oct 2008 Pauline Wong Versatile smoothing function.
25 Oct 2008 Cristina Velasco  
05 Dec 2008 Alexis Good job John, beautiful tool! Very parameterizable and helps a lot in giving you a feel of how your data is behaving.  
 
Does anyone know how I can anchor the surface to any given point? I'd like to force the surface to pass through a given set of (x,y,z) anchor points, something like establishing an "infinite" weight to these keypoints, while not altering the weight of the other "normal" points...
25 Dec 2008 Jveer wow! looks awesome! was wondering if there was any way to fit a surface around a 3D scatter? maybe this could be used on the data of for e.g x,y data corresponding to max(z) and then repeating for max(x) and max(y)?
25 Dec 2008 Michael Jordan  
13 Jan 2009 J Ú  
04 Mar 2009 Bryan Raines  
11 May 2009 Michael Dupin  
06 Jun 2009 Karl This is spectacular. I hope the Mathworks is paying you some sort of royalty for your efforts!
12 Jun 2009 Chris Sherwood This routine is almost magic...it is the only routine I have found that finds surfaces I know are there amidst very noisy elevation data. Thanks for contributing it.
03 Jul 2009 Angel Atanasov Thank you mr. D'Errico, for making my life easier.You piece of code is really good. 
 
Please login to add a comment or rating.
Updates
13 Dec 2005 Allows the user to specify a number of nodes from  
min to max of the data. Also fixed it so if the user specifies a list of nodes which does not bound the data, it can either recover smoothly or issue an error, all at the discretion of the user.
23 May 2006 Release 2.0: Adds the option of tiling to solve much larger problems
22 Aug 2006 - Add autoscaling feature for badly scaled data  
 
- Upgrade to Select structure, added more  
examples  
 
- Added Understanding_gridfit.rtf
Tag Activity for this File
Tag Applied By Date/Time
approximation John D'Errico 22 Oct 2008 08:05:55
interpolation John D'Errico 22 Oct 2008 08:05:55
regression John D'Errico 22 Oct 2008 08:05:55
surface fitting John D'Errico 22 Oct 2008 08:05:55
2d surfaces John D'Errico 22 Oct 2008 08:05:55
scattered data John D'Errico 22 Oct 2008 08:05:55
2d surfaces Martin Furlan 17 Nov 2008 12:54:56
2d surfaces Omer Music 20 Nov 2008 06:51:20
file exchange select Oliver Woodford 27 Jan 2009 10:39:35
modeling John D'Errico 19 Feb 2009 12:08:35
surface John D'Errico 19 Feb 2009 12:08:35
2d surfaces berland berland 15 Jun 2009 08:53:25

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com