Plotting the 3D Heat Equation in 2D Slices
Show older comments
Hello, I am trying to plot the temperature distribution of a cake using the heat equation in 3D. The boundary conditions are below.

I am trying to plot the temperature distribution of a cake shaped as a rectangular prism on 2D slices. These slices are placed on the x-axis consecutively. (see below)

How can I visualize these slices? Is it also possible to make an animation of the temperature change over time for one of the slices? Thanks.
Answers (2)
Image Analyst
on 1 Jul 2023
1 vote
"How can I visualize these slices? " <== There are several functions that can display a 2-D slice/matrix: imshow, imagesc, or image.
"Is it also possible to make an animation?" <==Once you've displayed them, you can get the currently displayed frame and save it as a frame in a movie. I'm attaching some demos. Look especially at movie_made_from_surf.m.
3 Comments
uk
on 1 Jul 2023
Your time step is too large. Look up "Courant condition" on how to set the maximum allowed time step size dt for Explicit Euler for the heat equation.
If I remember correctly, dt <= 0.25/(alpha*(1/dx^2+1/dy^2+1/dz^2)) where alpha is the Thermal Diffusivity.
By the way: Your alpha is incorrectly defined - it must read k/(rho*cp) instead of rho/(cp*k) (although for all values equal to 1 this doesn't matter).
rho = 1;
cp = 1;
k = 1;
alpha = k /( rho * cp);
Lx = 10;
Ly = 10;
Lz = 10;
Nx = 21; Nt = 35*6000;
Ny = 21;
Nz = 21;
dx = Lx / (Nx - 1);
dy = Ly / (Ny - 1);
dz = Lz / (Nz - 1);
c = 1;
C = 0.1;
dt = C * dx / c
dtmax = 0.25/(alpha*(1/dx^2+1/dy^2+1/dz^2))
uk
on 1 Jul 2023
Torsten
on 1 Jul 2023
Moved: Image Analyst
on 1 Jul 2023
1 vote
Your implementation of Explicit Euler looks correct.
If you want larger time steps and faster performance, use an ODE solver like ode15s.
6 Comments
uk
on 1 Jul 2023
Moved: Image Analyst
on 1 Jul 2023
Torsten
on 1 Jul 2023
Yes, but also in your code, you solve
dT(i,j,k)/dt = alpha* (((T(i+1,j,k) - 2*T(i,j,k) + T(i-1,j,k))/dx/dx)+...
((T(i,j+1,k) - 2*T(i,j,k) + T(i,j-1,k))/dy/dy)+...
((T(i,j,k+1) - 2*T(i,j,k) + T(i,j,k-1))/dz/dz))
thus (Nx-2)*(Ny-2)*(Nz-2) ODEs for the temperatures T(i,j,k) in the inner grid points, don't you ?
uk
on 1 Jul 2023
Torsten
on 1 Jul 2023
I have no experience with the PDE toolbox, but I would assume that it's comparable in speed to an implementation in ODE15S.
uk
on 1 Jul 2023
Torsten
on 1 Jul 2023
Evaluating a threefold series together with suitable stopping criteria to check when convergence has been achieved - I wouldn't go this way. Of course you can by setting x = 5, varying y and z in between 0 and 10 and setting t to the output time t = 35*60.
Categories
Find more on General PDEs 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!