# Heating of Finite Slab

This example shows how to find the temperature distribution of a one-dimensional finite slab by solving the governing differential equation. The example uses Symbolic Math Toolbox™ to solve the problem of heat transfer analytically in a finite slab as discussed in [1].

The finite slab has constant thermal properties. Assume that heat transfer is due only to conduction with a given thermal diffusivity $\alpha =\frac{k}{\rho {c}_{p}}$. The slab is located in the space between $y=-b$ and $y=b$, where heat propagates along the $y$-direction. Initially, the slab is at temperature ${T}_{0}$. At time $t=0$, the edges at $y=±b$ are suddenly raised to temperature ${T}_{1}$ and maintained there. This example solves for the temperature distribution of the slab $T\left(y,t\right)$ as a function of the $y$-coordinate and time $t$ by following these steps:

• Define the heat transfer equation.

• Use the method of separation of variables.

• Apply boundary conditions to find the mode solutions.

• Find the coefficients of the general solution by computing the integral of orthogonal functions.

• Plot the solution for the temperature as a function of the $y$-coordinate and time.

### Define Heat Transfer Equation

For a three-dimensional homogeneous medium, the heat transfer equation due to conduction is given by

$\frac{\partial \mathit{T}}{\partial \mathit{t}}=\alpha \left(\frac{{\partial }^{2}\mathit{T}}{\partial {\mathit{x}}^{2}}+\frac{{\partial }^{2}\mathit{T}}{\partial {\mathit{y}}^{2}}+\frac{{\partial }^{2}\mathit{T}}{\partial {\mathit{z}}^{2}}\right)$.

Because heat is transferred along the $y$-direction, you can reduce the heat transfer equation to

$\frac{\partial \mathit{T}}{\partial \mathit{t}}=\alpha \frac{{\partial }^{2}\mathit{T}}{\partial {\mathit{y}}^{2}}$.

Next, define these dimensionless variables:

• Dimensionless temperature $\Theta =\frac{{T}_{1}-T}{{T}_{1}-{T}_{0}}$

• Dimensionless coordinate $\eta =\frac{y}{b}$

• Dimensionless time $\tau =\frac{\alpha t}{{b}^{2}}$

The differential equation for the heat transfer in terms of these dimensionless variables is

$\frac{\partial \Theta }{\partial \tau }=\frac{{\partial }^{2}\Theta }{\partial {\eta }^{2}}$,

where the initial condition at $\tau =0$ is $\Theta =1$, and the boundary conditions at $\eta =±1$ are $\Theta =0$ for $\tau >0$.

Define the main differential equation.

syms Theta(eta,tau) eqMain = diff(Theta,tau) == diff(Theta,eta,eta)
eqMain(eta, tau) =  

### Method of Separation of Variables

Use the method of separation of variables to find the solution for $\Theta \left(\eta ,\tau \right)$. This method separates the spatial and time dependencies by writing them as a product of spatial and time-dependent functions, where $\Theta \left(\eta ,\tau \right)=f\left(\eta \right)g\left(\tau \right)$.

syms f(eta) g(tau) eqTheta = Theta(eta,tau) == f(eta)*g(tau)
eqTheta = $\Theta \left(\eta ,\tau \right)=f\left(\eta \right) g\left(\tau \right)$

Substitute this ansatz into the differential equation.

eqMain = subs(eqMain,lhs(eqTheta),rhs(eqTheta))
eqMain(eta, tau) =  

Separate the $f$ and $g$ terms so that they are be on different sides of the equation.

eqMain = eqMain/g(tau)/f(eta)
eqMain(eta, tau) =  

Set each side of the equation to a constant. For convenience, let this constant be $-{c}^{2}$.

syms c real eq1(tau) = lhs(eqMain) == -c^2
eq1(tau) =  
eq2(eta) = rhs(eqMain) == -c^2
eq2(eta) =  

### Apply Boundary Conditions to Find Mode Solution

Next, find the solutions for $f\left(\eta \right)$ and $g\left(\tau \right)$ by applying the appropriate boundary conditions.

Solve the first-order differential equation for $g\left(\tau \right)$ by using dsolve.

gsol(tau) = dsolve(eq1)
gsol(tau) = ${C}_{1} {\mathrm{e}}^{-{c}^{2} \tau }$

For $f\left(\eta \right)$, find the even solution based on the symmetry along the $y$-axis. Because the slab is located at $-b$ to $b$ and the temperature at both edges is fixed at ${T}_{1}$, the function $f\left(\eta \right)$ is symmetrical with respect to the origin. Find the general solution using dsolve.

fsol(eta) = dsolve(eq2)
fsol(eta) = ${C}_{1} \mathrm{cos}\left(c \eta \right)-{C}_{2} \mathrm{sin}\left(c \eta \right)$

For a well-behaved function, you can also write the general solution as $f\left(\eta \right)={f}_{even}\left(\eta \right)+{f}_{odd}\left(\eta \right)$. The even part of this solution is ${f}_{even}\left(\eta \right)=\frac{f\left(\eta \right)+f\left(-\eta \right)}{2}$ and the odd part of this solution is ${f}_{odd}\left(\eta \right)=\frac{f\left(\eta \right)-f\left(-\eta \right)}{2}$. Define the even solution of $f\left(\eta \right)$.

fsol_even(eta) = (fsol(eta) + fsol(-eta))/2
fsol_even(eta) = ${C}_{1} \mathrm{cos}\left(c \eta \right)$

Symbolic Math Toolbox uses ${C}_{1}$ as a dummy variable to represent a constant. In the solutions for $f\left(\eta \right)$ and $g\left(\tau \right)$ above, the variable ${C}_{1}$ represents different constants. Rewrite the variable ${C}_{1}$ from the even solution of $f\left(\eta \right)$ as ${C}_{f}$.

syms C1 C_f fsol = subs(fsol_even,C1,C_f)
fsol(eta) = ${C}_{f} \mathrm{cos}\left(c \eta \right)$

Apply the other boundary condition at $\eta =±1$, where $f\left(1\right)=0$, and solve for the parameter $c$.

[csol,params,conds] = solve(fsol(1)==0,c,ReturnConditions=true)
csol =  $\frac{\pi }{2}+\pi k$
params = $k$
conds = $k\in \mathbb{Z}\wedge {C}_{f}\ne 0$

The parameter $c$ in the solutions for $f\left(\eta \right)$ and $g\left(\tau \right)$ depends on an integer parameter $k$, where $c=\pi /2+\pi k$.

Substitute the solutions for $f\left(\eta \right)$ and $g\left(\tau \right)$ into $\Theta \left(\eta ,\tau \right)$.

eqTheta = subs(eqTheta,{f g},{fsol gsol})
eqTheta = $\Theta \left(\eta ,\tau \right)={C}_{1} {C}_{f} {\mathrm{e}}^{-{c}^{2} \tau } \mathrm{cos}\left(c \eta \right)$

Rewrite the ${C}_{1}{C}_{f}$ constant term as ${C}_{k}$. Rewrite the solution to depend on $k$ as well. This mode solution uses the index $k$ for the dimensionless temperature $\Theta \left(\eta ,\tau \right)$.

syms C(k) D(k) eqTheta = subs(eqTheta,{C1*C_f c},{C(k) csol})
eqTheta =  $\Theta \left(\eta ,\tau \right)=\mathrm{cos}\left(\eta \left(\frac{\pi }{2}+\pi k\right)\right) {\mathrm{e}}^{-\tau {\left(\frac{\pi }{2}+\pi k\right)}^{2}} C\left(k\right)$

### Compute Integral of Orthogonal Functions

Next, construct the general solution of the differential equation from the mode solution. Get the right side of the equation in eqTheta to perform further computations.

Theta_k(eta,tau,k) = rhs(eqTheta)
Theta_k(eta, tau, k) =  $\mathrm{cos}\left(\eta \left(\frac{\pi }{2}+\pi k\right)\right) {\mathrm{e}}^{-\tau {\left(\frac{\pi }{2}+\pi k\right)}^{2}} C\left(k\right)$

Following the steps in the textbook [1], combine the terms with indices $k$ and $-\left(k+1\right)$. Simplify these combined terms.

Theta_k2(eta,tau,k) = simplify(Theta_k(eta,tau,k) + Theta_k(eta,tau,-(k+1)))
Theta_k2(eta, tau, k) =  ${\mathrm{e}}^{-\frac{\tau {\pi }^{2} {\left(2 k+1\right)}^{2}}{4}} \mathrm{cos}\left(\frac{\pi \eta \left(2 k+1\right)}{2}\right) \left(C\left(-k-1\right)+C\left(k\right)\right)$

Rewrite the general solution by summing all of these modes. Here, define another $D\left(k\right)$ coefficient to represent $C\left(-k-1\right)+C\left(k\right)$. Then, take the summation of the $k$ indices from 0 to $\infty$.

Theta_sol = symsum(subs(Theta_k2,C(-k-1)+C(k),D(k)),k,0,Inf)
Theta_sol(eta, tau) =  ${\sum }_{k=0}^{\infty }{\mathrm{e}}^{-\frac{\tau {\pi }^{2} {\left(2 k+1\right)}^{2}}{4}} \mathrm{cos}\left(\frac{\pi \eta \left(2 k+1\right)}{2}\right) \text{D}\left(k\right)$

Next, find the $D\left(k\right)$ coefficient based on the initial condition of the problem. To do this, set the boundary condition and find the integral of the orthogonal functions that involve the $\mathrm{cos}$ function. Set the initial boundary condition $\Theta \left(\eta ,0\right)=1$.

ThetaBound = Theta_sol(eta,0) == 1
ThetaBound =  ${\sum }_{k=0}^{\infty }\mathrm{cos}\left(\frac{\pi \eta \left(2 k+1\right)}{2}\right) \text{D}\left(k\right)=1$

Multiply both sides of the above equation by $\mathrm{cos}\left(\pi \eta \left(m+\frac{1}{2}\right)\right)$, and find the integral with respect to $d\eta$. Here, the indices $m$ and $k$ are integers.

syms m k integer ThetaBound = ThetaBound*cos((m+1/2)*pi*eta)
ThetaBound =  $\mathrm{cos}\left(\pi \eta \left(m+\frac{1}{2}\right)\right) \left({\sum }_{k=0}^{\infty }\mathrm{cos}\left(\frac{\pi \eta \left(2 k+1\right)}{2}\right) \text{D}\left(k\right)\right)=\mathrm{cos}\left(\pi \eta \left(m+\frac{1}{2}\right)\right)$
ThetaBound = int(lhs(ThetaBound),eta,-1,1) == int(rhs(ThetaBound),eta,-1,1)
ThetaBound =  

This integration involves a symbolic summation series that uses the symsum function. Symbolic Math Toolbox does not find this integral explicitly, so you need to investigate the integral of each summation term on the left side of the above equation. Extract each symbolic summation term that has the indices $m$ and $k$. Define this term as a new expression. You can copy and paste from the output above when defining this expression manually.

expr_temp = cos(sym(pi)*eta*(m + sym(1/2)))*cos((sym(pi)*eta*(2*k + 1))/2)*D(k)
expr_temp =  $\mathrm{cos}\left(\pi \eta \left(m+\frac{1}{2}\right)\right) \mathrm{cos}\left(\frac{\pi \eta \left(2 k+1\right)}{2}\right) \text{D}\left(k\right)$

Next, find the integral of the expression over $\eta$ from $-1$ to $1$.

expr_int = int(expr_temp,eta,-1,1)
expr_int =  

Symbolic Math Toolbox simplifies the integral. To compare this result with the one in the textbook without simplification, check this integral by assuming that $m$ and $k$ are equal (as in the first piecewise condition above).

expr_temp2 = cos(pi*eta*(m+1/2))*cos(pi*eta*(m+1/2))*D(m)
expr_temp2 =  ${\mathrm{cos}\left(\pi \eta \left(m+\frac{1}{2}\right)\right)}^{2} \text{D}\left(m\right)$

Without simplification, Symbolic Math Toolbox returns the integral as in the textbook example.

expr_int2 = int(expr_temp2,eta,-1,1)
expr_int2 =  $\text{D}\left(m\right) \left(\frac{\mathrm{sin}\left(2 \pi \left(m+\frac{1}{2}\right)\right)}{2 \pi \left(m+\frac{1}{2}\right)}+1\right)$

Because $m$ is an integer, you can simplify this result.

expr_int2 = simplify(expr_int2)
expr_int2 = $\text{D}\left(m\right)$

Next, find the coefficient $D\left(m\right)$ by applying the boundary condition from the previous step. Simplify the equation for $D\left(m\right)$ with the applied boundary condition, and isolate $D\left(m\right)$ on the left side of the equation.

ThetaBound = expr_int2 == rhs(ThetaBound)
ThetaBound =  $\text{D}\left(m\right)=\frac{2 \mathrm{sin}\left(\pi \left(m+\frac{1}{2}\right)\right)}{\pi \left(m+\frac{1}{2}\right)}$
ThetaBound = isolate(simplify(ThetaBound),D(m))
ThetaBound =  $\text{D}\left(m\right)=\frac{4 {\left(-1\right)}^{m}}{\pi +2 \pi m}$

Change the dummy variable $m$ to $k$. Finally, substitute $D\left(k\right)$ into the general solution. This solution represents the heat transfer of a finite one-dimensional slab.

ThetaBound = subs(ThetaBound,m,k); Theta_sol = subs(Theta_sol,D,rhs(ThetaBound))
Theta_sol(eta, tau) =  $4 \left({\sum }_{k=0}^{\infty }\frac{{\left(-1\right)}^{k} {\mathrm{e}}^{-\frac{\tau {\pi }^{2} {\left(2 k+1\right)}^{2}}{4}} \mathrm{cos}\left(\frac{\pi \eta \left(2 k+1\right)}{2}\right)}{\pi +2 \pi k}\right)$

### Plot Solution for Temperature

Plot the dimensionless temperature of the slab as a function of $\eta$ at various values of $\tau$. Because the temperature solution involves a summation series with infinite terms, evaluating all these terms when plotting can be slow. To improve the plotting performance, you can approximate the infinite summation series as a summation series of up to $k=40$. Note that the temperature solution is a converging series and numerically stable. For this reason, approximating the series works well to improve the plotting performance.

Theta_sol_approx = subs(Theta_sol,Inf,40);

Plot the temperature of the slab for various values of $\tau$ by using fplot.

% Plot the result as in figure 12.1-1 of reference [1] fplot(eta,1-Theta_sol_approx(eta,0.01),[0 1]) axis equal hold on fplot(eta,1-Theta_sol_approx(eta,0.04),[0 1]) fplot(eta,1-Theta_sol_approx(eta,0.1),[0 1]) fplot(eta,1-Theta_sol_approx(eta,0.2),[0 1]) fplot(eta,1-Theta_sol_approx(eta,0.4),[0 1]) fplot(eta,1-Theta_sol_approx(eta,0.6),[0 1]) fplot(eta,1-Theta_sol_approx(eta,1),[0 1]) text(0,1.03,"center of slab") text(1,1.03,"surface of slab") xlabel("$y/b$",interpreter="latex") ylabel("$\frac{T-T_0}{T_1-T_0}$",interpreter="latex") legend("$\tau=0.01$","$\tau=0.04$","$\tau=0.1$","$\tau=0.2$", ... "$\tau=0.4$","$\tau=0.6$","$\tau=1$", ... interpreter="latex",Location="eastoutside") hold off

Next, plot the temperature in the SI unit $K$ as a function of the $y$-coordinate and time $t$, where ${T}_{1}=1000\phantom{\rule{0.2222222222222222em}{0ex}}K$, ${T}_{0}=500\phantom{\rule{0.2222222222222222em}{0ex}}K$, $b=0.2\phantom{\rule{0.2222222222222222em}{0ex}}m$, and $\alpha =1{0}^{-4}\phantom{\rule{0.2222222222222222em}{0ex}}{m}^{2}/s$. Define these parameters and assume the units are in SI.

T1 = 1000; T0 = 500; b = 0.2; alpha = 10^-4;

Define variables for the temperature $T$, coordinate $y$, and time $t$. From the dimensionless temperature solution in Theta_sol, find the temperature solution in $K$ by substituting these parameter values. Also, replace the infinite summation terms with a summation term of up to $k=40$ to improve the performance when plotting this solution.

syms T y t T_eqn = (T1-T)/(T1-T0) == subs(Theta_sol,{eta,tau},{y/b,alpha*t/b^2}); T_sol = isolate(T_eqn,T); T_sol = subs(rhs(T_sol),Inf,40);

Create a surface plot of the temperature as a function of the $y$-coordinate and time. You can see that initially the slab is at $500\phantom{\rule{0.2222222222222222em}{0ex}}K$ with edges at $1000\phantom{\rule{0.2222222222222222em}{0ex}}K$, and after about $600\phantom{\rule{0.2222222222222222em}{0ex}}s$, the temperature across the slab reaches the steady-state value of $1000\phantom{\rule{0.2222222222222222em}{0ex}}K$.

figure fsurf(T_sol,[1 600 -b b]) xlabel("time (s)") ylabel("y-coordinate (m)") zlabel("temperature (K)")

### References

[1] Bird, R. Byron, Warren E. Stewart, and Edwin N. Lightfoot. Transport Phenomena, 2nd ed. (New York: Wiley, 2007), 376–378, Example 12.1-2.