Problem with Contour command
22 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
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
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.
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
0 Comments
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!