Problem with Contour command

22 views (last 30 days)
Ilias Seferlis
Ilias Seferlis on 21 Nov 2015
Edited: Star Strider on 22 Nov 2015
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.

Answers (2)

Star Strider
Star Strider on 21 Nov 2015
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 Comments
Ilias Seferlis
Ilias Seferlis on 21 Nov 2015
Edited: Ilias Seferlis on 21 Nov 2015
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?
Star Strider
Star Strider on 21 Nov 2015
Edited: Star Strider on 22 Nov 2015
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.

Sign in to comment.


Bjorn Gustavsson
Bjorn Gustavsson on 21 Nov 2015
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

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!