Matlab 2D contour using X-Y coordinate data

杀马特。学长 韩版系。学妹 提交于 2019-12-02 02:10:59

问题


I have a set of data that looks likes the following:

!Sr.#    x-coord.    y-coord     potential at (x,y)
  1       0.0000     1.0000      0.3508
  2       0.7071     0.7071      2.0806
  .       ....       ....        ....
  .       ....       ....        ....
 1000    0.0000     -1.0000      0.5688

I need to generate a 2D contour for the above data where the value of the potential will be plotted at the corresponding (x,y) location on the 2D contour map. I believe that in order to be able to plot a 2D contour using the contour command in Matlab, I will have to use a 2D matrix (which will essentially contain potential values in my case). How do I create a 2D matrix for this case? Or is there a workaround which can altogether avoid a 2D matrix and still give a 2D contour. The x-y coodinate data I have is not in any particular order but can be arranged if needed.


回答1:


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)



回答2:


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



来源:https://stackoverflow.com/questions/14968867/matlab-2d-contour-using-x-y-coordinate-data

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