MATLAB: Contourplot with nonuniform data leads to strange interpolation

contourcontourfinterpolationMATLAB

I have data on the efficiency eta of an electric motor as function of its torque M and angular velocity omega. I wanted to make a contour plot of this to map the performance of the motor. Since it's real data, M and omega are nonuniformly spaced. I found a lot of examples of how to interpolate data on a grid to help Matlab to create contour plots of nonuniformly spaced data. I used these, but I get a wiggly contour plot which doesn't seem right at all.
Here's my code:
omega = [107.841, 153.211, 228.0715, 316.7175, 117.0895, 166.2985, 247.9645, 343.765, 122.9353, 174.6745, 260.5285, 364.1815, 125.989, 177.6410, 267.334, 373.6045];
M = [0.0911, 0.1231, 0.1785, 0.2435, 0.0628, 0.0828, 0.1159, 0.1550, 0.0412, 0.0518, 0.0696, 0.0902, 0.0319, 0.0378, 0.0480, 0.0600];
eta = [0.4094, 0.4466, 0.4846, 0.5076, 0.4140, 0.4554, 0.4942, 0.5218, 0.3813, 0.4167, 0.4601, 0.4935, 0.3460, 0.3816, 0.4174, 0.4600];
[xi, yi] = meshgrid(linspace(min(omega),max(omega)), linspace(min(M),max(M)));
zi = griddata(omega,M, eta, xi,yi,'linear');
figure;
contourf(xi,yi,zi);
hold on
scatter(omega,M,'ro');
This will produce the attached image contourplot_matlab.jpg, which would be beautiful if only the interpolation was correct.
There are lots of wiggles in the contours which is not what it should be like. Compare it to the one I made in Origin (a different software package):
It would seem that the interpolation in Matlab is doing something strange. I tried different options in the interpolation type (instead of 'linear' above), but it always looks strange.
My students (it's an experiment for the Lab) only have Matlab and no Origin… so my questions are:
  1. how can I get Matlab to make a reasonable interpolation of the nonuniform data?
  2. @Mathworks: There are so many questions in your forums regarding contour plots of nonuniform data, could you please provide a version of contour plot that accepts the data "as is" without the interpolation part? In Origin, I can simply select the 3 data columns and have it plot a contour plot without any further ado!

Best Answer

There is no need for any interpolation, griddata, meshgrid, ndgrid, or changing the number of points. As long as the data is stored in order (i.e. not randomly) then you can just use reshape (and possibly a transpose, depending on the order of the data) and it will work just fine. All you need is just one line:
contourf(reshape(omega,4,4),reshape(M,4,4),reshape(eta,4,4))
Together with your example values:
omega = [107.841,153.211,228.0715,316.7175,117.0895,166.2985,247.9645,343.765,122.9353,174.6745,260.5285,364.1815,125.989,177.6410,267.334,373.6045];
M = [0.0911,0.1231,0.1785,0.2435,0.0628,0.0828,0.1159,0.1550,0.0412,0.0518, 0.0696,0.0902, 0.0319,0.0378,0.0480,0.0600];
eta = [0.4094,0.4466,0.4846,0.5076,0.4140,0.4554,0.4942,0.5218,0.3813,0.4167, 0.4601,0.4935,0.3460,0.3816,0.4174,0.4600];
contourf(reshape(omega,4,4),reshape(M,4,4),reshape(eta,4,4))
hold on
scatter(omega,M,'ro');
which give this plot
: