You are here: Home Help/User Support Programs for FEA, Modeling, etc. MATLAB Example of Plotting Grid Data

Example of Plotting Grid Data

Assume you have a numerical model built that has 2 independent variables, and 1 dependent variable, for example, a two-dimensional heat transfer problem where T is a function of x and y. How can you make a simple mesh or contour plot of the results?

The Data

Matlab provides facilities for both reading external data files, and for creating these kinds of plots. Here's a sample data file, simply named 'data':

  0 0 0
  0 1 1
  0 2 4
  1 0 1
  1 1 2
  1 2 5
  2 0 4
  2 1 5
  2 2 8
  3 0 9
  3 1 10
  3 2 13

It's an ordered rectangular grid of points, with each row containing an x, y, z combination. We want to represent this data as a single surface plot where 0 <= x <= 3 and 0 <= y <= 2. There would be a total of 12 points shown in the plot, and they would be placed in 3 rows and 4 columns.

To bring this data file into Matlab, you can use the load command:

  load -ascii data

Now we have a 12x3 matrix of numbers in a variable named data. We want to strip out each column into a separate variable:

  x1=data(:,1)'; % strip out x axis values
  y1=data(:,2)'; % strip out y axis values
  z1=data(:,3)'; % strip out dependent variable

The ' operator is a transpose, and transforms the variable from a column of numbers into a row. It's optional, but I think it's easier to read a row of numbers than a column.

Transforming the Data

One problem with our x1 and y1 variables is that both of them contain repeated values. Let's get rid of them:

  x=unique(x1);  % turn x1 into a unique range of values
  y=unique(y1);  % turn y1 into a unique range of values

Now, we need to prepare a matrix that will hold our dependent variable data -- it'll need to be a 4x3 matrix, or more generally, to have as many rows as we have unique y values, and as many columns as we have unique x values:

  z=zeros(length(y),length(x)); % preallocate space for the z surface

Now we want to place our dependent data values into this new matrix. It's fairly easy to describe where each point would go, but the programming required isn't quite as obvious.

For example, we know that the (1,1) position in the z matrix should correspond to the dependent variable value where both x and y are at their lowest values (0 and 0). A combination of the find function and some boolean logic provides a solution:

  for m=1:length(x)
   for n=1:length(y)
     z(n,m)=z1(find(x1==x(m) & y1==y(n)));
     % reorder the z1 column into a rectangular matrix

This code loops through every valid (x,y) pair and finds the corresponding z1 value. Let's take a specific example where (x,y)=(0,0) and show how it all fits together:

  » find(x1==0)
  ans =
       1     2     3

Now look back at the original data file. On rows 1 through 3, the first column (x) is in fact 0. Let's repeat the process for y:

  » find(y1==0)
  ans =
       1     4     7    10

Look back at the data file again. On rows 1, 4, 7, and 10, the second column (y) is 0. Now, let's combine those lines to find the row where both x and y are zero:

  » find(x1==0 & y1==0)
  ans =

Both x and y are zero only on the first row. Therefore, we'll take the first value of our z column and stick it in the first position of the surface matrix.

Now all the hard work is done: we have a compatible set of x, y, and z data that's ready to be graphed:

  zlabel('z = x^2 + y^2')
  title('Simple grid data plot')

The mesh function could be replaced with contour, meshc, or any other surface-plotting function. The final result using the mesh function looks like this:

MATLAB Screenshot: xyz Plot

Filed under: