Main Content

Plots of Gradients and Streamlines with MATLAB Functions

This example shows how to calculate the approximate temperature gradient, and how to use the gradient in a quiver plot or streamline plot.

Create an femodel object for steady-state thermal analysis and include a block geometry into the model.

model = femodel(AnalysisType="thermalSteady", ...
                Geometry="Block.stl");

Plot the geometry.

pdegplot(model.Geometry,FaceLabels="on",FaceAlpha=0.5)
title("Copper block, cm")

Figure contains an axes object. The axes object with title Copper block, cm contains 6 objects of type quiver, text, patch, line.

Assuming that this is a copper block, the thermal conductivity of the block is approximately 4W/(cmK).

model.MaterialProperties = ...
    materialProperties(ThermalConductivity=4);

Apply a constant temperature of 373 K to the left side of the block (edge 1) and a constant temperature of 573 K to the right side of the block.

model.FaceBC(1) = faceBC(Temperature=373);
model.FaceBC(3) = faceBC(Temperature=573);

Apply a heat flux boundary condition to the bottom of the block.

model.FaceLoad(4) = faceLoad(Heat=-20);

Mesh the geometry and solve the problem. The solver finds the values of temperatures and temperature gradients at the nodal locations.

model = generateMesh(model);
R = solve(model)
R = 
  SteadyStateThermalResults with properties:

    Temperature: [12822x1 double]
     XGradients: [12822x1 double]
     YGradients: [12822x1 double]
     ZGradients: [12822x1 double]
           Mesh: [1x1 FEMesh]

Create a grid specified by x, y, and z coordinates, and interpolate temperatures to the grid.

[X,Y,Z] = meshgrid(0:10:100,0:4:20,0:5:50);

T = interpolateTemperature(R,X,Y,Z);
T = reshape(T,size(X));

Create a contour slice plot for five fixed values of the y-coordinate.

figure
colormap jet
contourslice(X,Y,Z,T,[],0:5:20,[])
xlabel("x")
ylabel("y")
zlabel("z")
xlim([-1,100])
ylim([-1,20])
zlim([-1,50])
axis equal
view(-50,22)
colorbar

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 30 objects of type patch.

Evaluate temperature gradients to the grid.

[gradTx,gradTy,gradTz] = ...
evaluateTemperatureGradient(R,X,Y,Z);

Plot the temperature gradients. First reshape the gradTx, gradTy, and gradTz vectors to the shape of the mesh.

gradTx = reshape(gradTx,size(X));
gradTy = reshape(gradTy,size(Y));
gradTz = reshape(gradTz,size(Z));

figure
quiver3(X,Y,Z,gradTx,gradTy,gradTz)
axis equal
xlabel("x")
ylabel("y")
zlabel("z")
title("Quiver Plot of Temperature Gradient")

Figure contains an axes object. The axes object with title Quiver Plot of Temperature Gradient, xlabel x, ylabel y contains an object of type quiver.

Plot the streamlines of the approximate gradient. Start the streamlines from a sparser set of initial points.

hold on
[sx,sy,sz] = meshgrid(1:20:100,1:5:20,1:10:50);
streamline(X,Y,Z,gradTx,gradTy,gradTz,sx,sy,sz)
title("Quiver Plot with Streamlines")
hold off

Figure contains an axes object. The axes object with title Quiver Plot with Streamlines, xlabel x, ylabel y contains 101 objects of type quiver, line.