Bad result in 2D Transient Heat Conduction Problem Using BTCS Finite Difference Method implicitly
Show older comments
Everything seem's ok, but my solution's is wrong.


%% 2D HEAT EQUATION WITH CONSTANT TEMP. AT BC'S
%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////
%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////
%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////
%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////
%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////
%% INITIALIZING
clc
clear
close all
%% PARAMETERIZATION
a = 1e-4; % THERMAL DIFFUSIVITY
t = 200; % TOTAL TIME
nt = 2; % TOTAL NUMBER OF TIME STEPS
dt = t/nt; % TIMESTEP
L = 1; % X = Y
nx = 4; % TOTAL NUMBER OF SPATIAL GRIDS
dx = L/nx; % dX = dY
%% BC'S
T1 = 100; % BCS OF DOMAIN
T2 = 100;
T3 = 100;
T4 = 100;
T5 = 200; % INITIAL TEMP.
Tmax = max([T1,T2,T3,T4,T5]);
%% DEFINITION
m = (L/dx)+1; % NO. OF X STEP (X = Y)
r = (t/dt)+1; % NO. OF TIME STEP
d = a*dt/(dx)^2; % DIFF. NO.
%% CREATING BC's & IC'S
B = zeros(m-2,m-2);
[nr,nc] = size(B);
nT = nr * nc;
B = zeros(m,m);
B(:,end) = T1;
B(end,:) = T2;
B(1,:) = T3;
B(:,1) = T4;
B(2:end-1,2:end-1) = T5; %% EDGES MUST BE REFINED
B1 = B(2:end-1,2:end-1)';
BC_v = B1(:); %IC's
%% CREATE TDMA LHS
AA2(1:nT) = 1+4*d;
AA3(1:nT-1) = -d;
AA5(1:nT-3) = -d;
AA = diag (AA2,0) + diag (AA3, -1) + diag (AA3,1) + ...
diag(AA5,-3)+diag(AA5,3); %% LHS HAS BEEN CREATEAD
%% CREATE RHS
T13(:,:,:) = zeros(size(B,1),size(B,1),r); % Define a cubic matrix
n = size(AA,1);
T13(:,:,1) = B;
BB(n,1) = 0;
for i = 1:n
if mod(i,2) == 1
BB(i,1) = T1+T2;
end
if mod(i,2) ~= 1
BB(i,1) = T1;
end
if mod(n,2)~=1
BB((n+1)/2,1) = 0;
elseif mod(n,2)==1
BB((n+1)/2,1) = 0;
end
end
RHS = BC_v + d*BB;
for k = 1:r
%\\\\\Solution of x in Ax=b using Gauss Seidel Method/////
%\\\\\Solution of x in Ax=b using Gauss Seidel Method/////
C = RHS; % constants vector
n = length(C);
X = zeros(n,1);
Error_eval = ones(n,1);
% Start the Iterative method
iteration = 0;
while max(Error_eval) > 1e-7
iteration = iteration + 1;
Z = X; % save current values to calculate error later
for i = 1:n
j = 1:n; % define an array of the coefficients' elements
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(i) = []; % eliminate the unknown under question from the set of values
X(i) = (C(i) - sum(AA(i,j) * Xtemp)) / AA(i,i);
end
Error_eval = sqrt((X - Z).^2);
end
%\\\\\Solution of x in Ax=b using Gauss Seidel Method/////
%\\\\\Solution of x in Ax=b using Gauss Seidel Method/////
RHS = X + d*BB;
T13(2:end-1,2:end-1,k+1) = (vec2mat(RHS',m-2)); % VEC. TO MAT.
T13(:,end,k+1) = T1;
T13(end,:,k+1) = T2;
T13 (1,:, k + 1) = T3;
T13 (:, 1, k + 1) = T4;
end
%% Animation
x = 1:m;
y = 1:m;
T21 = T13 (:,:, 1);
for k = 1:r-1
figure(1)
h = imagesc(x,y,T21);
shading interp
axis([0 m 0 m 0 Tmax])
title({['Transient Heat Conduction'];['time = ',...
num2str((k)*dt),' s']})
colorbar;
drawnow;
xlabel(['T = ',num2str(T2),'C'])
yyaxis left
ylabel(['T = ',num2str(T4),'C'])
yyaxis right
str = 'T0 = 20C' ;
dim = [.6 0.55 .3 .3];
pause(0.1);
refreshdata(h);
if k~=r
T21 = T13(:,:,k+1);
else
break;
end
end
8 Comments
Mohammad Farhadi
on 26 May 2020
darova
on 26 May 2020
Can you attach original equations?
Mohammad Farhadi
on 28 May 2020
darova
on 28 May 2020
It's too complicated for this forum. What about method of lines?
Mohammad Farhadi
on 28 May 2020
Vineet Sengar
on 6 Jun 2020
Bro, could you send me your code? As I'm having the project related to 2-D transient heat conduction. Please do reply asap. Thanking u in the advance😊.
Leroy Coelho
on 7 Jul 2020
I have written the code for this problem
I am more then happy to send the code your way for your refrence
Ragul Kumar
on 6 Nov 2020
Hello experts,
I am trying to solve the finite difference methof for crank nicolson scheme to 2d heat equation. please let me know if you have any MATLAB CODE for this 

Boundary condition are

If you can kindly send me the matlab code, it will be very useful for my research work . thank you very much.
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB 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!






