Matlab 2D contour using X-Y coordinate data

你。 提交于 2019-12-02 02:44:27

I have run into this problem myself, and have found an incredible solution from a stackoverflow member, John D'Errico, i.e. woodchips. His package on Matlab Central called gridfit, will solve your problem handily. Here's my own example, but John has much better ones in his incredible documentation and demo files.

% first, get some random x,y coordinates between -3 and 3
% to allow the peaks() function to look somewhat meaningful
x = rand(10,1)*6 - 3;
y = rand(10,1)*6 - 3;
% calculate the peaks function for this points
z = peaks(x,y);
% now, decide the grid we want to see, -3 to 3 at 0.1 intervals
% will be fine for this crude test
xnodes = -3:0.1:3;
ynodes = -3:0.1:3;
% now, all gridfit, notice, no sorting!  no nothing!
% just tell it the rectangular grid you want, and give it
% the raw data.  It will use linear algebra and other robust
% techniques to fit the function to the grid points
[zg,xg,yg] = gridfit(x,y,z,xnodes,ynodes);
% finally, plot the data, Viola!
contour(xg,yg,zg)

For arbitrarily scattered data, look at TriScatteredInterp as an alternative to gridfit.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!