Problem with Contour command

Hey ,
I am trying to plot the distribution of temperature across a 2-D plate,the 2 sides of which are isolated. I've created three column vectors of data X(400x1) Y(400x1) T(400x1),where (X(1),Y(1)) are the coordinates of the temperature T(1).I've used meshgrid and then contour(x,y,T) to plot the isolines but it shows this error :
''Error using contour (line 84)
Z must be size 2x2 or greater.''
Has anyone any idea on how to fix it? I've posted an image of how it should look and the data of the vectors.

回答(2 个)

We need to see your code. You probably need to create grids from ‘X’ and ‘Y’ with meshgrid first, and then do the calculations.
For example:
X = linspace(0, 10, 400); % ‘X’ Vector
Y = linspace(0, 10, 400); % ‘Y’ Vector
[Xg,Yg] = meshgrid(X,Y); % ‘Create Mesh Matrices
Zg = Yg.^2 - Xg.^2; % Calculate ‘Z’ Matrix
figure(1)
contour(Xg, Yg, Zg) % Plot Contours
axis equal
This actually looks a lot like your desired plot!

2 个评论

The code I wrote is in Fortran 77 and I just imported the results in Matlab to plot them. I've created the mesh but the problem is with the size of Z when I use the contour.Is there any way to change the size of Z?
I would import the ‘Z’ calculation code into MATLAB and do the calculation in MATLAB using element-wise (dot-operator (.*, .^, ./)) calculations, such as I did with my ‘Zg’ calculation. To use the MATLAB contour function, the three arguments have to match in dimensions.
The code you would have to emulate in FORTRAN 77 would be to compute your ‘Z’ as a matrix with every combination of ‘X’ and ‘Y’ in the same order that MATLAB would calculate them. The three arguments all have to be matrices with the same size and element correspondence.
It will be easier for you to simply code your ‘Z’ in MATLAB. See the documentation for Array vs. Matrix Operations for the details in using the element-wise operators.
I will help as I can.
EDIT It just occurred to me that if your vectors are calculated to actually be (20x20) matrices (vectors with regularly repeating elements in ‘X’ and ‘Y’ so that ‘Z’ is calculated from all combinations of them), then you could use the MATLAB reshape function to create matrices from them and from ‘Z’ that would work with the contour and other 3D plotting functions. I don’t know how you calculated them, so I can’t say for sure.

请先登录,再进行评论。

Bjorn Gustavsson
Bjorn Gustavsson 2015-11-21

0 个投票

First you should have a look at the tricontour function at the matlab file exchange, that function does what you seem to want in the shortest possible way.
Then perhaps also have a look at the griddata/TriScatteredInterp/ScatteredInterp functionalities to get to reinterpolate your data to full plaid grids.
HTH

类别

帮助中心File Exchange 中查找有关 Contour Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by