You are on page 1of 23

Viscous Flows Class Project

Numerical and analytical Solutions to


Diffusion and 2D Heat conduction Equations

Table of Contents
Diffusion Equation ........................................................................................................................................ 3
Numerical Schemes................................................................................................................................... 3
Explicit Solution..................................................................................................................................... 4
Implicit Solution .................................................................................................................................... 5
2-D Heat Conduction Equation .................................................................................................................... 6
Numerical Schemes................................................................................................................................... 6
Explicit Solution..................................................................................................................................... 7
Implicit Solution .................................................................................................................................... 8
Steady State 2-D Heat Conduction Equation............................................................................................... 9
Analytical Solution .................................................................................................................................... 9
Appendices ................................................................................................................................................. 14
Appendix A Diffusion Equation approximation.................................................................................... 14
Explicit Solution MATLAB Code........................................................................................................... 14
Implicit Solution MATLAB Code ......................................................................................................... 16
Appendix B- 2-D Heat Conduction Approximation ................................................................................ 19
Explicit Solution MATLAB Code .......................................................................................................... 19
Implicit Solution MATLAB Code ......................................................................................................... 21

Table of Figures
Figure 1: Solution to diffusion scheme using explicit approximation.2
Figure 2: Solution to diffusion scheme using implicit approximation.2
Figure 3: Solution to 2-D Heat Conduction Equation using Explicit Approximation.2
Figure 4: Solution to 2-D Heat Conduction Equation using Implicit Approximation...2
Figure 5: Plot of Sinh(x) and Tanh(x) showing Sinh(x)-Tanh(x)....2

|2

Diffusion Equation
Diffusion Equation given as
[1.1]
Subject to the initial condition
(

for

[1.2]

for

[1.3]

And the boundary condition

Equation [1.1] can be discretized using explicit scheme


(

[1.4]

Where
[1.5]

With an imposed constraint on


[1.6]
To avoid constrain on

. Equation [1.1] can be discretized using implicit scheme as


(

[1.7]

Analytical solution to equation [1.1]


(

[1.8]

Where
( )

[1.9]

Solutions to equation [1.1] using both explicit and implicit approximations, equations [1.4] and [1.8],
while compared with equation [1.8] are displayed in figures (1) and (2).

|3

Figure 1: Solution to diffusion equation using explicit approximation with Exact solution
overlaid. dt=0.01 dy=0.005

|4

Figure 2: Solution to diffusion equation using implicit approximation with Exact solution
overlaid. dt=0.1 dy=0.001

|5

Heat Conduction Equation


2-D heat conduction equation given as
(

[2.1]

Subject to the initial condition


(

[2.2]

[2.3]

[2.4]

[2.5]

And the boundary condition


(

[2.6]

Can be discretized using explicit scheme

))

[2.7]
(

Where
[2.8]
[2.9]
Can be discretized using implicit scheme as

))

[2.10]
(

Solutions to equation [2.1] using both explicit and implicit approximations, equations [2.7] and [2.10],
are displayed n figures (3) and (4)

|6

Figure 3: Solution to 2-D Heat Conduction Equation using Explicit Approximation. dt=1
dx=dy=0.1

|7

Figure 4: Solution to 2-D Heat Conduction Equation using Implicit Approximation.


dt=100 dx=dy=0.01

|8

Steady State Heat Conduction Equation


Two-dimensional heat conduction equation is given as

(
At steady state (

[3.1]

) equation [1] becomes


[3.2]

Equation [3.1] is subject to the following boundary conditions


(

[3.3]

[3.4]

[3.5]

( )

( )

[3.6]
(

Introduce temperature variant to convert to homogenous boundary conditions


[3.7]
Thus, equations [3.2]-[ 3.7] become
[3.8]
Equation [3.1] is subject to the following boundary conditions
(
(
(
(

)
)
)
)

( )

[3.9]
[3.10]
[3.11]
[3.12]

Using method of separation of variables, equation [3.8] becomes


(

( ) ( )

[3.13]

|9

Substitute equation [3.13] into equation [3.2]


( )

( )

( )

( )

[3.14]

Equation [3.14] becomes

Introduce

[3.15]

( )

( )

to convert PDE to two separate ODEs

Transform the boundary conditions

[3.16]

( )

( )

( ) ( )

( )
(

[3.17]

( ) ( )

( )
(

[3.18]

( ) ( )

( )
(

[3.19]
( ) ( )

( )

( )

( )

[3.20]

Equation [3.16] becomes two Sturm-Liouville problems


( )

[3.21]

( )

[3.22]

The general Solutions to equations [3.21] and [3.22] are

( )

[3.23]

( )
( )

[3.24]
(

[3.25]

|10

Substituting equation [3.17] into [3.23]


(

( )

)
[3.26]

Substituting equation [3.26] into [3.23]


(

( )

[3.27]

Substituting equation [3.18] and [3.26] into [3.23]


(

( )
(

)
[3.28]

Form equation [3.28], eigenfunction for

is
(

( )

[3.29]

Substituting equation [3.17] into [3.24]


( )

( )
[3.30]

Substituting equation [3.18] and [3.30] into [3.24]


( )
[3.31]
From equation [3.30] and [3.31] there are no eigenvalues for
Substituting equation [3.19] into [3.25]
( )

(
(

(
(

)
)

[3.32]

Substituting equation [3.32] back into equation [3.23]


(
(

)
(

(
)

)
))

[3.33]

|11

Figure 5: Plot of Sinh(x) and Tanh(x) showing Sinh(x)-Tanh(x)

From equation [3.33] and figure 5, there are no eigenvalues for

which therefore implies

[3.34]
Substituting equation [3.34] into [3.25]
(

( )

[3.35]

Substituting equation [3.27] and [3.35] into [3.13]


(

[3.36]

Where
[3.37]
Since equation [3.34] is linear, the summation of
(

also satisfies the boundary conditions

[3.38]

( )

[3.39]

Substitute equation [3.20] into [3.38]


(

( )

|12

To apply orthogonality, introduce


(

[3.40]

Multiply [3.39] and [3.40] and integrate over (0,L) where L=1

( ( )

[3.41]

The solution to the RHS of equation [3.41]


When

(
(

[3.42]

When
(

[3.43]

This implies
( ( )

( ( )

)
)

[3.44]

Change summation index of equation [3.44] back to


( ( )

[3.45]

Substitute equation [3.45] into [3.38] to obtain the temperature profile in the plate
(

) ( ( )

|13

Appendix A
MATLAB Code Diffusion Explicit
%%%%% DIFFUSION EQUATION APPRXIMATION USING EXPLICIT SCHEME %%%%
clc
clear all
tic
% using the Forward Time Centered Space (FTCS) scheme.
Uo = 1;
dt = 0.01;
dy = 0.005;
h = 0.20;
v = 1.5*10^-5;
d = v*dt/(dy^2);
JM = 1+(h/dy);
t = 60;
%* Set initial and boundary conditions.
u = zeros(JM,1); % Initialize velocity to zero at all points
u(1)= Uo;
u(JM)= 0;
unew = zeros(JM,1);
unew(1)=Uo;
unew(JM)= 0;
plotu = zeros(JM,4);
%%
p=1;
for t=dt:dt:60
for j=2:JM-1
unew(j) = u(j)+d*(u(j+1)-2*u(j)+u(j-1));
end
u=unew;
if (t==1)||(t==2)||(t==5)||(t==10)
plotu(:,p)=u;
p=p+1;
end
end
h = 0;
for i = 2:JM
y(1)=0;
y(i) = h+dy;
h = y(i);
end
hold on
plot(plotu(:,1),y,plotu(:,2),y,plotu(:,3),y,plotu(:,4),y)
title('FTCS Implicit dt=0.01')
xlabel('Velocity [m/s]')
ylabel('height [m]')
axis([0 1 0 .2])
legend('t=1','t=2','t=5','t=10')
toc
tic

|14

Appendix A
MATLAB Code Diffusion Explicit
%% ------------- Compare Solution --------------------------- %%
syms x h
y = exp(-x^2);
a = 0;
t = 1;
b = h/(2*sqrt(v*t));
fnint = int(y,x,a,b);
erf1 = (2/(sqrt(pi)))*fnint;
U1 = (1-erf1);
y = exp(-x^2);
a = 0;
t = 2;
b = h/(2*sqrt(v*t));
fnint = int(y,x,a,b);
erf2 = (2/(sqrt(pi)))*fnint;
U2 = (1-erf2);
y = exp(-x^2);
a = 0;
t = 5;
b = h/(2*sqrt(v*t));
fnint = int(y,x,a,b);
erf3 = (2/(sqrt(pi)))*fnint;
U3 = (1-erf3);
y = exp(-x^2);
a = 0;
t = 10;
b = h/(2*sqrt(v*t));
fnint = int(y,x,a,b);
erf4 = (2/(sqrt(pi)))*fnint;
U4 = (1-erf4);
h=0;
for i = 2:10
y(1)=0;
y(i) = h+dy;
h = y(i);
end
h = y;
u1
u1
u2
u2
u3
u3
u4
u4

=
=
=
=
=
=
=
=

subs(U1);
double(u1);
subs(U2);
double(u2);
subs(U3);
double(u3);
subs(U4);
double(u4);

plot(u1,h,'-.',u2,h,'-.',u3,h,'-.',u4,h,'-.')
title('Explicit Approximation with Exact Solution')
toc
hold off

|15

Appendix A
MATLAB Code Diffusion Implicit
%%%%% DIFFUSION EQUATION APPRXIMATION USING IMPLICIT SCHEME %%%%
clc
clear all
tic
% using the Forward Time Centered Space (FTCS) scheme.
Uo = 1;
dt = 0.1;
dy = 0.001
h = 0.20;
v = 1.5*10^-5;
d = v*dt/(dy^2);
JM = 1+(h/dy);
t = 60;
%* Set initial and boundary conditions.
u = zeros(JM,1); % Initialize velocity to zero at all points
u(1)= Uo;
u(JM)= 0;
unew = zeros(JM,1);
unew(1)=Uo;
unew(JM)= 0;
plotu = zeros(JM,4);
% Thomas Alg.
a = -d;
b = 2*d+1;
c = -d;
p=1;
for t=dt:dt:t
for i=2:JM
D(1) = u(1);
D(i) = u(i);
H(1) = 0;
H(i)=c/(b-a*H(i-1));
G(1) = Uo;
G(i)=(D(i)-a*G(i-1))/(b-a*H(i-1));
end
for j=JM-1:-1:2
unew(j)=-H(j)*unew(j+1)+G(j);
end
u=unew;
if (t==1)||(t==2)||(t==5)||(t==10)
plotu(:,p)=u;
p=p+1;
end
end
h = 0;
for i = 2:JM
y(1)=0;

|16

Appendix A
MATLAB Code Diffusion Implicit
y(i) = h+dy;
h = y(i);
end
hold on
plot(plotu(:,1),y,plotu(:,2),y,plotu(:,3),y,plotu(:,4),y)
xlabel('Velocity [m/s]')
ylabel('height [m]')
axis([0 1 0 .2])
legend('t=1','t=2','t=5','t=10')
toc
tic
%% ------------- Compare Solution --------------------------- %%
syms x h
y = exp(-x^2);
a = 0;
t = 1;
b = h/(2*sqrt(v*t));
fnint = int(y,x,a,b);
erf1 = (2/(sqrt(pi)))*fnint;
U1 = (1-erf1);
y = exp(-x^2);
a = 0;
t = 2;
b = h/(2*sqrt(v*t));
fnint = int(y,x,a,b);
erf2 = (2/(sqrt(pi)))*fnint;
U2 = (1-erf2);
y = exp(-x^2);
a = 0;
t = 5;
b = h/(2*sqrt(v*t));
fnint = int(y,x,a,b);
erf3 = (2/(sqrt(pi)))*fnint;
U3 = (1-erf3);
y = exp(-x^2);
a = 0;
t = 10;
b = h/(2*sqrt(v*t));
fnint = int(y,x,a,b);
erf4 = (2/(sqrt(pi)))*fnint;
U4 = (1-erf4);
h=0;
for i = 2:10
y(1)=0;
y(i) = h+dy;
h = y(i);
end

|17

Appendix A
MATLAB Code Diffusion Implicit
h = y;
u1 = subs(U1);
u1 = double(u1);
u2 = subs(U2);
u2 = double(u2);
u3 = subs(U3);
u3 = double(u3);
u4 = subs(U4);
u4 = double(u4);
plot(u1,h,'-.',u2,h,'-.',u3,h,'-.',u4,h,'-.')
title('Implicit Approximation with Exact Solution')
xlabel('Velocity [m/s]')
ylabel('height [m]')
axis([0 1 0 .2])
legend('t=1','t=2','t=5','t=10','(Exact) t=1',...
'(Exact) t=2','(Exact) t=5','(Exact) t=10')
toc
hold off

|18

Appendix B
MATLAB Code Heat Conduction Explicit
%%%%% 2-D HEAT CONDUCTION APPRXIMATION USING EXPLICIT SCHEME %%%%
clc
clear all
tic
% Domain Size
x = 1;
y = 1;
% Step Size
dx = 0.1;
dy = 0.1;
% Time Step
dt = 1;
% Grid Size
JM = 1+(y/dy);
IM = 1+(x/dx);
% Thermal Diffusivity
k = 1.5*10^-5;
d1 = (1/2)*(k*dt/dx^2);
d2 = (1/2)*(k*dt/dy^2);
% Initial Conditions
T1 = 300;
T3 = 300;
T4 = 300;
%% Initialize Boundary Conditions
T=300*ones(IM,JM);
T(:,JM)=T3;
T(IM,:)=T4;
T(:,1)=T1;
Tx=300*ones(IM,JM);
Tx(:,JM)=T3;
Tx(IM,:)=T4;
Tx(:,1)=T1;
Tnew=300*ones(IM,JM);
Tnew(:,JM)=T3;
Tnew(IM,:)=T4;
Tnew(:,1)=T1;
Twall=zeros(JM);
% Preallocate vectors for Thomas algorithm
H=zeros(IM);
G=zeros(IM);

|19

Appendix B
MATLAB Code Heat Conduction Explicit
D=zeros(IM);
% Solve for T(0,y,t)
y=0;
for j=1:JM
T2 = 300+500*sin(pi*y);
y = y+dy;
T(1,j)=T2;
Tx(1,j)=T2;
Tnew(1,j)=T2;
Twall(j)=T2;
end
errormax = 0.000001; % Define max error value
error = 1; %Set initial error value
iterations = 0; % Iteration count set to zero
%% Thomas Algorithm
while error >= errormax;
% X sweep
for j=2:JM-1
for i=2:IM-1
Tx(i,j) = d2*T(i,j+1)+(1-2*d2)*T(i,j)+d2*T(i,j-1);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
err=0; % Set error count to zero
% Y sweep
for i=2:IM-1
for j=2:JM-1
Tnew(i,j) = d1*Tx(i+1,j)+(1-2*d1)*Tx(i,j)+d1*Tx(i-1,j);
end
% Sum error
for j=2:JM-1
sum = (abs(Tnew(i,j)-T(i,j))/(T(i,j)));
sumerror = sum + err;
err = sumerror;
end
end
T = Tnew; % Update values for new time step
iterations = iterations + 1 % Iteration count
error=err % Determine total error
end
contour(Tnew)
colorbar
colormap gray
title('2-D Heat Conduction Equation Explicit Approximation')

|20

Appendix B
MATLAB Code Heat Conduction Implicit
%%%%% 2-D HEAT CONDUCTION APPRXIMATION USING IMPLICIT SCHEME %%%%
clc
clear all
tic
% Domain Size
x = 1;
y = 1;
% Step Size
dx = 0.01;
dy = 0.01;
% Time Step
dt = 100;
% Grid Size
JM = 1+(y/dy);
IM = 1+(x/dx);
% Thermal Diffusivity
k = 1.5*10^-5;
d1 = (1/2)*(k*dt/dx^2);
d2 = (1/2)*(k*dt/dy^2);
% Initial Conditions
T1 = 300;
T3 = 300;
T4 = 300;
%% Initialize Boundary Conditions
T=300*ones(IM,JM);
T(:,JM)=T3;
T(IM,:)=T4;
T(:,1)=T1;
Tx=300*ones(IM,JM);
Tx(:,JM)=T3;
Tx(IM,:)=T4;
Tx(:,1)=T1;
Tnew=300*ones(IM,JM);
Tnew(:,JM)=T3;
Tnew(IM,:)=T4;
Tnew(:,1)=T1;
Twall=zeros(JM);
% Preallocate vectors for Thomas algorithm
H=zeros(IM);
G=zeros(IM);

|21

Appendix B
MATLAB Code Heat Conduction Implicit
D=zeros(IM);
% Solve for T(0,y,t)
y=0;
for j=1:JM
T2 = 300+500*sin(pi*y);
y = y+dy;
T(1,j)=T2;
Tx(1,j)=T2;
Tnew(1,j)=T2;
Twall(j)=T2;
end
errormax = 0.000001; % Define max error value
error = 1; %Set initial error value
iterations = 0; % Iteration count set to zero
%% Thomas Algorithm
while error >= errormax;
% X sweep
for j=2:JM-1
for i=2:IM-1
a = -d1;
b = 1+2*d1;
c = -d1;
D(1) = T(1,j);
D(i) = d2*T(i,j+1)+(1-2*d2)*T(i,j)+d2*T(i,j-1);
H(1) = 0;
H(i)=c/(b-a*H(i-1));
G(1) = Twall(j);
G(i)=(D(i)-a*G(i-1))/(b-a*H(i-1));
end
Tx(IM,j)=T4;
for i=IM-1:-1:2
Tx(i,j)=-H(i)*Tx(i+1,j)+G(i);
end
Tx(1,j)=Twall(j);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
err=0;

% Set error count to zero

% Y sweep
for i=2:IM-1
for j=2:JM-1
a = -d2;
b = 1+2*d2;
c = -d2;

|22

Appendix B
MATLAB Code Heat Conduction Implicit
D(1) = T(i,1);
D(j) = d1*Tx(i+1,j)+(1-2*d1)*Tx(i,j)+d1*Tx(i-1,j);
H(1) = 0;
H(j) = c/(b-a*H(j-1));
G(1) = T1;
G(j)=(D(j)-a*G(j-1))/(b-a*H(j-1));
end
Tnew(i,JM)=T3;
for j=JM-1:-1:2
Tnew(i,j)=-H(j)*Tnew(i,j+1)+G(j);
end
Tnew(i,1)=T1;
% Sum error
for j=2:JM-1
sum = (abs(Tnew(i,j)-T(i,j))/(T(i,j)));
sumerror = sum + err;
err = sumerror;
end
end
T = Tnew; % Update values for new time step
iterations = iterations + 1 % Iteration count
error=err % Determine total error
end
for i=1:IM
for j=1:JM
Tnew(j,i)=T(i,j);
end
end
contour(Tnew)
colorbar
colormap gray
title('2-D Heat Conduction Equation Implicit Approximation')
xlabel('x [m]')
ylabel('y [m]')
set(gca,'XTick',[1:IM/5.05:IM] );
set(gca,'XTickLabel',[0:.2:x]);
set(gca,'YTick',[1:JM/5.05:JM] );
set(gca,'YTickLabel',[0:.2:y]);
toc

|23

You might also like