88 views (last 30 days)

Hi there,

I'm working on a tribology problem which looks into the temperature distribution in a thin oil film between a piston ring and a cylinder bore. I wrote a code which incorporates the finite difference method to give me the output of how the temperature varies throughout the film thickness with different crank angles (engine strokes).

My code works fine however I'm not quite sure how to plot my results correctly. I and my supervisor chose matlab to generate nice 3D plots of the temperature distribution however I don't have enough matlab experience to do it correctly and I don't understand much having read the long descriptions in the Matlab documents. (The original version of my code was written in Fortran).

The problem is that all my temperature values are stored in a 3D matrix of the size (721 x 21 x 101).

Where the number 721 is a number of degrees showing the engine crank angle going from 1 to 721. 21 is the number of grid points in the x direction and 101 is the number of grid points in the y direction.

When I wrote a first version of my code which didn’t include the crank angle part I just ended up with a 2D temperature matrix t of the size of 21 x 101 then I was able to use the meshgrid and mesh commands to do the plots and my code looked like the following:

%Plot results

kmin = 1;

kmax = 101;

imin = 1;

imax = 21;

[kk,ii] = meshgrid(kmin:kmax,imin:imax);

mesh(kk,ii,t);

xlabel('Y-Coordinate (mm)')

ylabel('X-Coordinate (mm)')

zlabel('Temperature (deg)')

Now I don't know how to deal with this big 3D matrix t which stores 721 pages of seperate 2D temperature matrices.

Do I need to use a for-loop to loop through the crank angles?

Any help would be greatly appreciated.

jonas
on 17 Aug 2020

Edited: jonas
on 17 Aug 2020

It is convenient to have the variable you want to plot as layers in your 3d data. In other words you want a XYZ matrix but you have a ZXY. Use permute to rearrange your data, A

A = permute(A,[2,3,1]);

then you can easily plot the i'th layer as

surf(A(:,:,i));

If you want to plot a single coordinate over crank angles, then you could use

out = squeeze(A(x,y,:))

jonas
on 18 Aug 2020

Sure, you can have multiple surface objects in the same graph. I would use a for-loop to plot different layers. Remember to use "hold on" on the axes. There is a big risk the plot will be messy.

figure

surf(peaks); hold on

surf(peaks+10)

surf(peaks+20)

Another option is to use a flat surface plot, like pcolor() or contourf(), to avoid intersections between different surface objects. You can change the z-value to some arbitrary offset.

for i = 1:5

h(i) = pcolor(peaks);hold on

set(h(i),'zdata',repmat(i*5,size(peaks,2),size(peaks,1)),'facealpha',0.8) %first argument of repmat (i*5) is a variable offset

end

set(h,'facealpha',0.8) %if you want transparent surfaces

view(3) %view in 3d

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/580836-3d-temperature-distribution-plot#comment_976353

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/580836-3d-temperature-distribution-plot#comment_976353

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/580836-3d-temperature-distribution-plot#comment_976365

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/580836-3d-temperature-distribution-plot#comment_976365

Sign in to comment.