How to plot temperature along a defined line within a 2D temperature field

I can obtain 2D temperature field solutions such as the steady state example below. I would like to be able to plot the temperature along a defined line within this domain. For example, how can I produce a plot of T as a function of x for Y= 0 from X= -15 to X= 0 or for X= -12 from Y= -15 to Y= +15? I would also like to be able to do this at a defined time for a transient problem. I am aware of the slice command but I just want a simple T vs X or Y plot. Thanks
thermalmodel = createpde("thermal","steadystate");
R2= [3,4,-15,15,15,-15,-15,-15,15,15]';
geom=[R2]
g=decsg(geom)
model= createpde
geometryFromEdges(thermalmodel,g);
pdegplot(thermalmodel,"EdgeLabels","on")
xlim([-20 20])
axis equal
thermalProperties(thermalmodel,"ThermalConductivity",1);
thermalBC(thermalmodel,"Edge",1,"Temperature",100);
thermalBC(thermalmodel,"Edge",2,"Temperature",100);
thermalBC(thermalmodel,"Edge",3,"Temperature",100);
thermalBC(thermalmodel,"Edge",4,"Temperature",400);
generateMesh(thermalmodel);
figure
pdemesh(thermalmodel)
title("Mesh with Quadratic Triangular Elements")
thermalresults = solve(thermalmodel)
title("Temperature In The Plate, Steady State Solution")
xlabel("X-coordinate, meters")
ylabel("Y-coordinate, meters")
axis equal

 Accepted Answer

4 Comments

Hi Torsten
I'm a relative novice with Matlab so I'll have to figure out what the point matrix, p, and the solution vector, u, are for my problem. I think the solution vector is the temperature
T= thermalresults.TemperatureNot so sure about the point matrix vector
thanks
john
thermalmodel = createpde("thermal","steadystate");
R2= [3,4,-15,15,15,-15,-15,-15,15,15]';
geom=[R2];
g=decsg(geom);
model= createpde;
geometryFromEdges(thermalmodel,g);
pdegplot(thermalmodel,"EdgeLabels","on");
xlim([-20 20])
axis equal
thermalProperties(thermalmodel,"ThermalConductivity",1);
thermalBC(thermalmodel,"Edge",1,"Temperature",100);
thermalBC(thermalmodel,"Edge",2,"Temperature",100);
thermalBC(thermalmodel,"Edge",3,"Temperature",100);
thermalBC(thermalmodel,"Edge",4,"Temperature",400);
mesh = generateMesh(thermalmodel);
figure
pdemesh(thermalmodel);
title("Mesh with Quadratic Triangular Elements")
thermalresults = solve(thermalmodel);
pdeplot(thermalresults.Mesh,XYData=thermalresults.Temperature,ColorMap="jet")
title("Temperature In The Plate, Steady State Solution")
xlabel("X-coordinate, meters")
ylabel("Y-coordinate, meters")
axis equal
plotAlongLine(mesh.Nodes, thermalresults.Temperature, [-15,0], [0,0], 25);
plotAlongLine(mesh.Nodes, thermalresults.Temperature, [-12,-15], [-12,15], 25);
function plotAlongLine(p, u, xy1, xy2, numpts)
x = linspace(xy1(1),xy2(1),numpts);
y = linspace(xy1(2),xy2(2),numpts);
F = TriScatteredInterp(p(1,:)', p(2,:)', u);
uxy = F(x,y);
figure;
if xy1(1)==xy2(1)
plot(y, uxy);
else
plot(x, uxy)
end
grid on
end
Thorsten
This is fantastic! Thanks so much for your help.
Is there an easy way to export the T vs x or T vs Y data as a file?
john
Define x, y and uxy as output arguments of the function "plotAlongLine" and write them to file.
Usually, "fprintf" is used for this:
If you don't know how to proceed, I recommend MATLAB Onramp, an introductory course free-of-costs to learn the basics of the language:

Sign in to comment.

More Answers (1)

I know you already got an answer that you've accepted but I just thought I'd mention improfile. It can give you a profile of values in a matrix from any point to any other point.

Products

Release

R2024a

Community Treasure Hunt

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

Start Hunting!