0% found this document useful (0 votes)
27 views

MATLAB Source Codes

This document compares explicit and implicit methods for solving the 2D transient heat equation numerically using MATLAB. For the explicit method, it discretizes the spatial and time derivatives, initializes the temperature field, and iterates to solve for the temperature at each time step. For the implicit method, it uses Jacobi, Gauss-Seidel, and SOR iterations to solve the discretized equations at each time step. It plots the temperature contour after each method.

Uploaded by

Arief Yazid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

MATLAB Source Codes

This document compares explicit and implicit methods for solving the 2D transient heat equation numerically using MATLAB. For the explicit method, it discretizes the spatial and time derivatives, initializes the temperature field, and iterates to solve for the temperature at each time step. For the implicit method, it uses Jacobi, Gauss-Seidel, and SOR iterations to solve the discretized equations at each time step. It plots the temperature contour after each method.

Uploaded by

Arief Yazid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

A.

MATLAB (explicit method)

x = 1:10; % Geometric Parameters


dx = abs( x(1) - x(2) );
nx = length(x);
y = 1:10;
dy = abs( y(1) - y(2) );
ny = length (y);

T_left = 25; % Boundary conditions


T_top = 0;
T_right = 25;
T_bottom = 50;

T = 10*ones(nx,ny); % Initial condition

T(:,1) = T_left; % Assigning IC & BC


T(1,:) = T_top;
T(:,nx) = T_right;
T(ny,:) = T_bottom;
T(1,1) = (T_left + T_bottom)/2;
T(1,ny) = (T_top + T_left)/2;
T(nx,ny) = (T_top + T_right)/2;
T(nx,1) = (T_right + T_bottom)/2;

T_initial = T; % Initializing
T_old = T;

alpha = 1.11; % Thermal diffusivity – Copper (cm^2/s)

dt = 0.01 ; % Time steps


k1 = alpha*(dt/(dx^2));
k2 = alpha*(dt/(dy^2));

conv = 1e-4; % Convergence criteria


error = 1;

iteration = 0;

for nt = 1: 1000 % Starting the time loop.

while error > conv % Until error is within convergence criteria

for j = 2:(ny-1) % Spatial loop

for i = 2:(nx-1)

node1 = (T_old(i-1,j) - 2*T_old(i,j) + T_old(i+1,j));


node2 = (T_old(i,j-1) - 2*T_old(i,j) + T_old(i,j+1));
T(i,j) = T_old(i,j) + (node1*k1) + (node2*k2);
end

end

error = max(max(abs(T_old - T))); % Calculating error

T_old = T;
iteration = iteration + 1;

end

T_initial = T;

end

[c,h] = contourf(x,y,T); % Plotting each iteration results


clabel(c,h);
colorbar
colormap(jet)
set(gca,'YDIR','reverse');
xlabel('X Axis');
ylabel('Y Axis');
title({['2D Transient Heat Conduction.'],['Number of iterations = ',num2str(iteration)]});
pause(0.01);

B. MATLAB (implicit method)

x = 1:10; % Geometric Parameters


dx = abs( x(1) - x(2) );
nx = length(x);
y = 1:10;
dy = abs( y(1) - y(2) );
ny = length (y);

T_left = 25; % Boundary conditions


T_top = 0;
T_right = 25;
T_bottom = 50;

T = 10*ones(nx,ny); % Initial condition

T(:,1) = T_left; % Assigning IC & BC


T(1,:) = T_top;
T(:,nx) = T_right;
T(ny,:) = T_bottom;
T(1,1) = (T_left + T_bottom)/2;
T(1,ny) = (T_top + T_left)/2;
T(nx,ny) = (T_top + T_right)/2;
T(nx,1) = (T_right + T_bottom)/2;

T_initial = T; % Initializing
T_old = T;

alpha = 1.17; % Thermal diffusivity – Copper (cm^2/s)

dt = 10; % Time steps

k1 = alpha*(dt/(dx^2));
k2 = alpha*(dt/(dy^2));

conv = 1e-4; % Convergence criteria


error = 1;

iteration = 0;

% Time loop

for nt = 1: 500 % Starting the time loop.

While error>conv % Until error is within convergence criteria

for j = 2(ny-1) % Spatial loop

for i = 2:(nx-1)
node1 = 1/(1+(2*k1)+(2*k2)); % Jacobi method
node2 = k1*node1;
node3 = k2*node1;
a = (T_old((i-1), j) + T_old((i+1), j));
b = (T_old(i, (j-1)) + T_old(i, (j+1)));
T(i,j) =(T_initial(i,j) *
node1)+(a*node2)+(b*node3);
end

end

error = max(max(abs(T_old - T))); % Calculating error

T_old = T;
iteration = iteration + 1;

end
T_initial = T;

end

figure(1) % Plotting
[c,h] = contourf(x,y,T);
clabel(c,h);
colorbar
colormap(jet)
set(gca,'YDIR','reverse');
xlabel('X Axis');
ylabel('Y Axis');
title({['2D Heat Conduction in Transient State - Implicit method using Jacobi
method.'],['Number of iterations = ',num2str(iteration)]});

error = 1; % Reinitialising the error value and the iteration


count and repeat the above steps for Gauss Seidel method.

Iteration = 0;

for nt = 1: 500

while error > conv

for j = 2:(ny-1)

for i = 2:(nx-1)
node1 = 1/(1+(2*k1)+(2*k2)); % Gauss Seidel method
node2 = k1*node1;
node3 = k2*node1;
a = (T((i-1), j) + T_old((i+1), j));
b = (T(i, (j-1)) + T_old(i, (j+1)));
T(i,j) =(T_initial(i,j) * node1)+(a*node2)+(b*node3);

end

end

error = max(max(abs(T_old - T))); % Calculating error

T_old = T;
iteration = iteration + 1;

end

T_initial = T;

end
figure(2)
[c,h] = contourf(x,y,T);
clabel(c,h);
colorbar
colormap(jet)
set(gca,'YDIR','reverse');
xlabel('X Axis');
ylabel('Y Axis');
title({['2D Transient Heat Conduction - Implicit method using Gauss Seidel
method.'],['Number of iterations = ',num2str(iteration)]});

error = 1; %reinitialising the error value and the iteration count and
repeat the above steps for SOR method.

iteration = 0;

for nt = 1: 500

while error > conv

for j = 2:(ny-1)

for i = 2:(nx-1)

node1 = 1/(1+(2*k1)+(2*k2));
node2 = k1*node1;
node3 = k2*node1;
a = (T((i-1), j) + T_old((i+1), j));
b = (T(i, (j-1)) + T_old(i, (j+1)));
T(i,j) =(T_initial(i,j) *node1)+(a*node2)+(b*node3);
T(i,j) = T_old(i,j)*(1-alpha)+alpha*T(i,j);

end

end

error = max(max(abs(T_old - T)));


T_old = T;
iteration = iteration + 1;

end

T_initial = T;

end

figure(3)
[c,h] = contourf(x,y,T);
clabel(c,h);
colorbar
colormap(jet)
set(gca,'YDIR','reverse');
xlabel('X Axis');
ylabel('Y Axis');
title({['2D Transient Heat Conduction - Implicit method using SOR method.'],['Number of
iterations = ',num2str(iteration)]});

You might also like