0
votes

I calculated the function of interest $f(x,y)$ numerically in Mathematica. For consistency, I want to use Matlab for plotting.

I exported my function in the matrix form:

 test =

-1.0000   -1.0000   -0.4864
-1.0000   -0.6000   -0.2804
-1.0000   -0.2000   -0.0462
-1.0000    0.2000   -0.0462
-1.0000    0.6000   -0.2804
-1.0000    1.0000   -0.4864
-0.6000   -1.0000   -0.2997
-0.6000   -0.6000   -0.1526
-0.6000   -0.2000    0.1118
-0.6000    0.2000    0.1118
-0.6000    0.6000   -0.1526
-0.6000    1.0000   -0.2997
-0.2000   -1.0000   -0.1809
-0.2000   -0.6000   -0.0939
-0.2000   -0.2000   -0.0046
-0.2000    0.2000   -0.0046
-0.2000    0.6000   -0.0939
-0.2000    1.0000   -0.1809
 0.2000   -1.0000   -0.1809
 0.2000   -0.6000   -0.0939
 0.2000   -0.2000   -0.0046
 0.2000    0.2000   -0.0046
 0.2000    0.6000   -0.0939
 0.2000    1.0000   -0.1809
 0.6000   -1.0000   -0.2997
 0.6000   -0.6000   -0.1526
 0.6000   -0.2000    0.1118
 0.6000    0.2000    0.1118
 0.6000    0.6000   -0.1526
 0.6000    1.0000   -0.2997
 1.0000   -1.0000   -0.4864
 1.0000   -0.6000   -0.2804
 1.0000   -0.2000   -0.0462
 1.0000    0.2000   -0.0462
 1.0000    0.6000   -0.2804
 1.0000    1.0000   -0.4864

So that test is 36x3 matrix, where the columns are x, y and f(x,y). Now I need to contour plot it.

However, it is not in the form of meshgrid. Any thoughts, how to quickly convert it in the form that can be plotted by contour function?

1
Why do you need it in meshgrid form? Why not just do contour(f)? - Suever
it produces very strange result. I am sure it is incorrect, as I plotted it in Mathematica - Mikhail Genkin
Plot it as y,x,f(x,y)? - Andras Deak
Can you explain in more details please - Mikhail Genkin
contour(test(:,2),test(:,1),test(:,3))? - Andras Deak

1 Answers

2
votes

If your data is regular (data set coincide with meshgrid but in vector form), then you can use reshape.

xgrid=6;
ygrid=6;
contour(reshape(test(:,1),xgrid,ygrid),reshape(test(:,2),xgrid,ygrid),reshape(test(:,3),xgrid,ygrid))

Another option is tricontour which will also work for irregular dataset

tri = delaunay(test(:,1),test(:,2));

figure;
tricontour(tri,test(:,1),test(:,2),test(:,3));

You can find tricontour at https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data/content/tricontour.m

If your surface is not convex,

then you need to remove some of triangulation from tri because of the nature of delaunay