Plot along line from. PDE solution
Show older comments
The code below solves a 1-D, transient heat transfer problem set up as in general PDE format. The solution is plotted in color across the domain from 0 to 0.1 after 10 seconds have elapsed. What is the best way to plot the temperature across the length of this domain at this final time?
Thanks
clear all;
%% Create transient thermal model
thermalmodel = createpde(1);
R1= [3,4,0,0.1,0.1,0,0,0,1,1]';
gd= [R1];
sf= 'R1';
ns = char('R1');
ns = ns';
dl = decsg(gd,sf,ns);
%% Create & plot geometry
geometryFromEdges(thermalmodel,dl);
pdegplot(thermalmodel,"EdgeLabels","on","FaceLabels","on")
xlim([0 0.1])
ylim([-1 1])
% axis equal
%% Generate and plot mesh
generateMesh(thermalmodel)
figure
pdemesh(thermalmodel)
title("Mesh with Quadratic Triangular Elements")
%% Apply BCs
% Edge 4 is left edge; Edge 2 is right side
applyBoundaryCondition(thermalmodel, "dirichlet",Edge=[4],u=100);
applyBoundaryCondition(thermalmodel, "dirichlet",Edge=[2],u=20);
%% Apply thermal properties [copper]
rho= 8933 %
cp= 385 %
rhocp= rho*cp %
k= 401 % W/mK
%% Define uniform volumetric heat generation rate
Qgen= 0 % W/m3
%% Define coefficients for generic Governing Equation to be solved
m= 0
a= 0
d= rhocp
c= [k]
f= [Qgen]
specifyCoefficients(thermalmodel, m=0, d=rhocp, c=k, a=0, f=f);
%% Apply initial condition
setInitialConditions(thermalmodel, 20);
%% Define time limits
tlist= 0: 1: 10;
thermalresults= solvepde(thermalmodel, tlist);
% Plot results
sol = thermalresults.NodalSolution;
subplot(2,2,1)
pdeplot(thermalmodel,"XYData",sol(:,11), ...
"Contour","on",...
"ColorMap","jet")
13 Comments
What is the best way to plot the temperature across the length of this domain at this final time?
By using "pdepe" instead of the PDE Toolbox.
If you want to continue simulating in 2d for an 1d problem, this code should help:
I guess by "length" you mean from 0 to 0.1 (thus in x-direction).
John McGrath
on 5 Apr 2025
Edited: Walter Roberson
on 5 Apr 2025
Plot solution at time 50 from timelist on line connecting [0,0.5] and [6,0.5] using a resolution of 50 points:
plotAlongLine(thermalresults.Mesh.Nodes, thermalresults.NodalSolution(:,50), [0,.5], [6,.5], 50);
John McGrath
on 5 Apr 2025
Did you copy the function "plotAlongLine" from the link I gave you and placed it where MATLAB can reach it ?
Maybe you should try MATLAB Onramp before continuing to get the basics of the new programming language:
John McGrath
on 8 Apr 2025
You only have to add the call to "plotAlongLine" and the function itself at the end of your code:
clear all;
thermalmodel = createpde();
R1= [3,4,0,3,3,0,0,0,10,10]';
R2= [3,4,3,6,6,3,-5,-5,5,5]';
gd= [R1 R2];
sf= 'R1+R2';
ns = char('R1','R2');
ns = ns';
dl = decsg(gd,sf,ns);
geometryFromEdges(thermalmodel,dl);
pdegplot(thermalmodel,"EdgeLabels","on","FaceLabels","on")
xlim([-1 7])
ylim([-6 11])
axis equal
%% Generate and plot mesh
generateMesh(thermalmodel);
figure
pdemesh(thermalmodel)
title("Mesh with Quadratic Triangular Elements")
%% Apply BCs
% Edge 3 is left edge; Edge 5 is right side; edge 8 is middle; Edges 2 & 6 top; Edges 1 & 4 bottom
% Edges 7 & 9 offset vertical edges
applyBoundaryCondition(thermalmodel, "dirichlet",Edge=[3],u=100);
applyBoundaryCondition(thermalmodel, "dirichlet",Edge=[5],u=20);
%% Apply thermal properties in first region
rho1= 1 %
rho1 = 1
cp1= 1 %
cp1 = 1
rhocp1= rho1*cp1 %
rhocp1 = 1
k1= 1 % W/mK
k1 = 1
%% Apply thermal properties in second region
rho2= 10 %
rho2 = 10
cp2= 10 %
cp2 = 10
rhocp2= rho2*cp2 %
rhocp2 = 100
k2= 10 % W/mK
k2 = 10
%% Define uniform volumetric heat generation rate
Qgen= 0 % W/m3 [1e12]
Qgen = 0
%% Define coefficients for generic Governing Equation to be solved
f= [Qgen]
f = 0
specifyCoefficients(thermalmodel, m=0, d=rhocp1, c=k1, a=0, f=f, Face=1);
specifyCoefficients(thermalmodel, m=0, d=rhocp2, c=k2, a=0, f=f, Face=2);
coeffs= thermalmodel.EquationCoefficients;
findCoefficients(coeffs, Face=1)
findCoefficients(coeffs, Face=2)
%% Apply initial condition
setInitialConditions(thermalmodel, 20);
%% Define time limits
tlist = 0:1:50;
%% Solve
thermalresults= solvepde(thermalmodel, tlist);
% Plot results
sol = thermalresults.NodalSolution;
subplot(2,2,1)
pdeplot(thermalmodel,"XYData",sol(:,50), ...
"Contour","on",...
"ColorMap","jet")
plotAlongLine(thermalresults.Mesh.Nodes, thermalresults.NodalSolution(:,50), [0,.5], [6,.5], 50);
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; plot(x, uxy);
end
John McGrath
on 9 Apr 2025
I'm surprised you have these problems. Last year, the function worked fine for you:
Which MATLAB version do you use ?
If it's an old one, you could try removing the function at the end of your code and instead saving it in your MATLAB working directory as "plotAlongLine.m" .
If this does not help, I don't know. You see that it works here using MATLAB online without problems.
Does this code work on your MATLAB ?
a = 4;
a_squared = fun(a)
function result = fun(a)
result = a.^2;
end
John McGrath
on 11 Apr 2025
You cannot execute MATLAB code containing self-written functions by pasting it into the command window. The function is not known at the moment it is called.
In Octave, it works if you save a function under its name in the working directory. In this case, it can subsequently be called from the command window.
John McGrath
on 11 Apr 2025
What is Octave?
Answers (1)
Karanjot
on 8 Apr 2025
Hi John,
In response to your latest comment, MATLAB returns the following error,as 'plotAlongLine' is not a standard function that is part of MATLAB. It's a custom function defined by another user.
Unrecognized function or variable 'plotAlongLine'.
In order to resolve the error, you may copy the function definition into your codebase, from the link attached below:
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; plot(x, uxy);
end
As also suggested by Torsten, Feel free to go through the following beginner courses on MATLAB basics:
I hope this helps!
1 Comment
John McGrath
on 8 Apr 2025
Categories
Find more on Heat Transfer 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!








