Reputation: 95
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.
Upvotes: 2
Views: 12707
Reputation: 4172
For arbitrarily scattered data, look at TriScatteredInterp as an alternative to gridfit.
Upvotes: 1
Reputation: 4685
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)
Upvotes: 2