You are on page 1of 5

Example of finite difference numerical estimate of 2D TRANSIENT conduction using an IMPLICIT

method.

Consider a square solid that has been discretized as a 5x5 grid of nodes. On the left boundary, all 5
nodes are at a specified temperature T1. On the right boundary, all 5 nodes are at a specified
temperature T5. The middle 3 nodes along the top boundary have a zero-flux boundary condition
(insulated). The middle 3 nodes along the bottom boundary have a heat transfer boundary condition
(heat transfer coefficient h, fluid temperature Tinf).

Below are the results of a Matlab solution. The listing of the Matlab code is shown below. T1 = 1, T5
= 2, Tinf = 0. The first image shown is the initial state showing the fixed boundary T's along the left
and right sides and the initial condition of the other T's (T=0).



The second image is at time t = 1 (dt = 0.5).


The third image is at t = 2



The final image is at t = 20. The temperatures agree out to the fourth decimal places with the steady-
state solution obtained with simultaneous solution using matrix inverse.


Tg =

1.0000 1.1801 1.4003 1.6782 2.0000
1.0000 1.1601 1.3714 1.6563 2.0000
1.0000 1.0887 1.2691 1.5755 2.0000
1.0000 0.9258 1.0406 1.3767 2.0000
1.0000 0.5738 0.5910 0.8907 2.0000

Listing of the Matlab code:

% Example of finite difference heat transfer solution of
% 2D TRANSIENT PROBLEM - IMPLICIT METHOD


% The solid body is discretized as 5x5 square with
% constant 5 T's on left side = T1
% and with constant 5 T's on right side = T2
% insulated nodes are middle 3 nodes on top
% heat flux nodes are middle 3 nodes on bottom to Tinf

tfinal = 20; % final time, e.g., SI units of (s)
dt = 0.5; % time step

% implicit method should be unconditionally stable for all dt
% however, accuracy decreases as dt increases,
% and simple method used below for solution of equation system
% uses inverse function inv() and this has problems with large dt

h = 1; % heat transfer coefficient, e.g., SI units of (W/m2/K)
k = 1; % thermal conductivity, e.g., SI units of (W/m/K)
dx = 1; % node spacing, e.g., SI units of (m)

alpha = 1; % thermal diffusivity, e.g., SI units of (m2/K)

% T's may be (T - Tref) (K) or dimensionless T variables
Tinf = 0; % T heat transfer fluid at bottom surface
T1 = 1; % T on left side
T5 = 2; % T on right side

% Since I first wrote a program to construct the system of
% linear equations, I'm just going to use that set of equations
% for the transient problem

% See the example using simultaneous solution using matrix inverse
% for description of how the matrix A and vector c are constructed.
% A*T = c

% number nodes from left to right and top to bottom
% starting with top left as node 1 and bottom right as node 25
% column vector T is T of node 1 at top and T of node 25 at bottom

a = 2*h*dx*Tinf/k;
beta = 4+2*h*dx/k;
gamma = alpha*dt/dx^2;

zm = zeros(5,5); % zero submatrix

% imod is identity matrix with 0 on top-left and btm-right corners
imod = eye(5);
imod(1,1) = 0;
imod(5,5) = 0;

% m4
m4 = eye(5);
m4 = -4*m4;
m4(1,1) = 1;
m4(5,5) = 1;
m4(2,1) = 1;
m4(2,3) = 1;
m4(3,2) = 1;
m4(3,4) = 1;
m4(4,3) = 1;
m4(4,5) = 1;

b = eye(5);
b = -beta*b;
b(1,1) = 1;
b(5,5) = 1;
b(2,1) = 1;
b(2,3) = 1;
b(3,2) = 1;
b(3,4) = 1;
b(4,3) = 1;
b(4,5) = 1;

A = [m4 2*imod zm zm zm
imod m4 imod zm zm
zm imod m4 imod zm
zm zm imod m4 imod
zm zm zm 2*imod b];

ca = [T1 0 0 0 T5]'; % note that ' is MATLAB transform operator
cb = [T1 -a -a -a T5]';
c = [ca; ca; ca; ca; cb];

% INITIAL CONDITION here use 0 as initial T for internal nodes
T = [ca; ca; ca; ca; ca];

% display image of initial condition
% set colormap scale coefficient
% I haven't quite figured out the color map so
% the coefficient will need to be changed for different
% conditions to get a pretty image
ccf = 20;
Tg = zeros(5,5);
Tg(1,:) = T(1:5)';
Tg(2,:) = T(6:10)';
Tg(3,:) = T(11:15)';
Tg(4,:) = T(16:20)';
Tg(5,:) = T(21:25)';
image(ccf*Tg)
title('t = 0')
pause(dt)
% use dt for pause in order to try to display image in real time
% (plus Matlab's computation and display time)

% now step in time

t = 0;

while (t < tfinal)

t = t+dt;

% use implicit method equation, where Tp is T at new time
% Tp = T + gamma*(A*Tp - c)
% rearrange to
% [I - gamma*A]*Tp = [T - gamma*c]
% solve using matrix inversion here
% Tp = inv([I - gamma*A])*[T - gamma*c]
% may need other algorithms for larger matrices
% and here have problems with inv() with larger dt's

I = eye(25);
Tp = inv(I - gamma*A)*(T - gamma*c);

% update T to new T's
T = Tp;

% display image
Tg = zeros(5,5);
Tg(1,:) = T(1:5)';
Tg(2,:) = T(6:10)';
Tg(3,:) = T(11:15)';
Tg(4,:) = T(16:20)';
Tg(5,:) = T(21:25)';
image(ccf*Tg)
title(['t = ' num2str(t)])
pause(dt)

end

fprintf('here are the temperatures at t = %5.2f \n',t)
Tg = zeros(5,5);
Tg(1,:) = T(1:5)';
Tg(2,:) = T(6:10)';
Tg(3,:) = T(11:15)';
Tg(4,:) = T(16:20)';
Tg(5,:) = T(21:25)';
Tg

% display final image
image(ccf*Tg)
title(['t = ' num2str(t)])

Text output:

>> transsquare5x5

dt =
0.1000

here are the temperatures at t = 20.00
Tg =
1.0000 1.1801 1.4003 1.6782 2.0000
1.0000 1.1601 1.3714 1.6563 2.0000
1.0000 1.0887 1.2691 1.5755 2.0000
1.0000 0.9258 1.0406 1.3767 2.0000
1.0000 0.5738 0.5910 0.8907 2.0000

R. Herz herz@ucsd.edu
October 15, 2007

You might also like