Week 5.1 - Mid term project - Solving the steady and unsteady 2D heat conduction problem
Successive over-relaxation
%% Transient heat conduction by Jacobi, guass Siedel & sor iterations
clear all
close all
clc
%slving 2D transient heat conduction equation using implicit
nx=10;
ny=10;
%defining the mesh size
x=linspace(0,1,nx);
y=linspace(0,1,ny);
%defining varriables
alpha=1.3;
dt=3e-4;
t=0.5;
dx=x(2)-x(1);
dy=y(2)-y(1);
%pre defining T
t=300*ones(nx,ny);
%applying boundary conditions
t(:,1)=400;
t(:,end)=800;
t(1,:)=600;
t(end,:)=900;
t(1,1)=500;
t(1,end)=700;
t(end,1)=650;
t(end,end)=850;
% copy of T which changes with in the time step
told=t;
t_pre_dt=t;
k1=((alpha*dt)/(dx^2));
k2=((alpha*dt)/(dy^2));
cfl=k1+k2;
error=9e9;
tol=1e-4;
%number of time steps
nt=t/dt;
iterative_solver=1;
%jacobi method
if iterative_solver==1
jacobi_iter=1;
for nts=1:nt;
error=1;
while error>tol
for i=2:nx-1;
for j=2:ny-1;
term1=(1+(2*k1)+(2*k2))^-1;
term2=k1*term1;
term3=k2*term1;
h=(told(i-1,j)+told(i+1,j));
v=(told(i,j-1)+told(i,j+1));
t(i,j)=(t_pre_dt(i,j)*term1)+(h*term2)+(v*term3);
end
end
error=max(max(abs(told-t)));
told=t;
jacobi_iter=jacobi_iter+1;
end
t_prev_dt = t;
end
end
figure(1)
contourf(x,y,t,'showtext','on')
colormap(jet)
colorbar
title_text=sprintf('transient heat conduction jacobi iteratons=%d',jacobi_iter);
title(title_text);
xlabel('x_axis');
ylabel('y_axis');
%gauss siedel method
if iterative_solver==2;
gs_iter = 1;
for nts=1:nt;
error=1;
while error>tol
for i=2:nx-1;
for j=2:ny-1;
term1=(1+(2*k1)+(2*k2))^-1;
term2=(t(i-1,j)+t(i+1,j))*(term1);
term3=(t(i,j-1)+t(i,j+1))*(term1);
t(i,j)=(t_pre_dt(i,j)*term1)+(k1*(term2))+(k2*(term3));
end
end
error=max(max(abs(told-t)));
told=t;
gs_iter = gs_iter + 1 ;
end
t_pre_dt=t;
end
end
figure(2);
contourf(x,y,t);
colormap(jet)
colorbar;
title(title_text);
xlabel('x_axis');
ylabel('y_axis');
if iterative_solver==3
sor_iter=1
for nts=1:nt
error=1;
while error>tol
for i=2:nx-1;
for j=2:nx-1;
term1=((1+(2*k1)+(2*k2))^-1);
term2=(told(i-1,j)+t(i+1,j))*(term1);
term3=(told(i,j-1)+t(i,j+1))*(term1);
t(i,j)=(1-omega)*told(i,j)+omega*(t_prev_dt(i,j)*term1)+(k1*(term2))+(k2*(term3));
end
end
error=max(max(abs(told-t)));
told=t;
sor_iter=sor_iter+1;
end
t_pre_dt=t;
end
end
figure(3)
contourf(x,y,t,'showtext','on');
colormap(jet)
colorbar;
title(title_text);
xlabel('x_axis');
ylabel('y_axis');
iterative solver==1
iterative solver==2
iterative solver==3

Gauss Seidel Method
clear all
close all
clc
%slving 2D steady state heat conduction equation using implicit
nx=10;
ny=10;
%defining the mesh size
x=linspace(0,1,nx);
y=linspace(0,1,ny);
%defining varriables
alpha=1.3;
dt=3e-4;
t=0.5;
dx=x(2)-x(1);
dy=y(2)-y(1);
%pre defining T
t=300*ones(nx,ny);
%applying boundary conditions
t(:,1)=400;
t(:,end)=800;
t(1,:)=600;
t(end,:)=900;
t(1,1)=500;
t(1,end)=700;
t(end,1)=650;
t(end,end)=850;
% copy of T which changes with in the time step
told=t;
t_pre_dt=t;
k=2*(dx^2+dy^2)/(dx^2*dy^2)
error=9e9;
tol=1e-4;
%number of time steps
nt=t/dt;
iterative_solver=1;
%gauss siedel method
if iterative_solver==1;
jacobi_iter = 1;
while error>tol
for i=2:nx-1;
for j=2:ny-1;
term1=(told(i-1,j)+told(i+1,j))/(dx^2);
term2=(told(i,j-1)+told(i,j+1))*(dy^2);
t(i,j)=(term1+term2)/k;
end
end
error=max(max(abs(told-t)));
told=t;
jacobi_iter = jacobi_iter + 1 ;
end
end
figure(1)
contourf(x,y,t,'showtext','on');
colormap(jet)
colorbar;
title_text=sprintf('steady state heat conduction=%d',jacobi_iter);
title(title_text);
xlabel('x_axis');
ylabel('y_axis');
%gs_method
if iterative_solver==2;
gs_iter = 1;
while error>tol
for i=2:nx-1;
for j=2:ny-1;
term1=(t(i-1,j)+t(i+1,j))/(dx^2);
term2=(t(i,j-1)+t(i,j+1))*(dy^2);
t(i,j)=(term1+term2)/k;
end
end
error=max(max(abs(told-t)));
told=t;
gs_iter = gs_iter + 1 ;
end
end
figure(2)
contourf(x,y,t,'showtext','on');
colormap(jet)
colorbar;
title(title_text);
xlabel('x_axis');
ylabel('y_axis');
if iterative_solver==3;
sor_iter=1
for nts=1:nt
error=1;
while error>tol
for i=2:nx-1;
for j=2:nx-1;
term1=((1+(2*k1)+(2*k2))^-1);
term2=(told(i-1,j)+t(i+1,j))*(term1);
term3=(told(i,j-1)+t(i,j+1))*(term1);
t(i,j)=(1-omega)*told(i,j)+omega*(t_pre_dt(i,j)*term1)+(k1*(term2))+(k2*(term3));
end
end
error=max(max(abs(told-t)));
told=t;
sor_iter=sor_iter+1;
end
t_pre_dt=t;
end
end
figure(3)
contourf(x,y,t,'showtext','on');
colormap(jet)
colorbar;
title(title_text);
xlabel('x_axis');
ylabel('y_axis');
jacobi_iter = 1;

jacobi_iter = 2;

jacobi_iter = 3;

Solving 2D transient heat conduction equation using implicit
clear all
close all
clc
%slving 2D transient heat conduction equation using implicit
nx=10;
ny=10;
%defining the mesh size
x=linspace(0,1,nx);
y=linspace(0,1,ny);
%defining varriables
alpha=1.3;
dt=3e-4;
t=0.5;
dx=x(2)-x(1);
dy=y(2)-y(1);
%pre defining T
t=300*ones(nx,ny);
%applying boundary conditions
t(:,1)=400;
t(:,end)=800;
t(1,:)=600;
t(end,:)=900;
t(1,1)=500;
t(1,end)=700;
t(end,1)=650;
t(end,end)=850;
% copy of T which changes with in the time step
told=t;
t_pre_dt=t;
k1=((alpha*dt)/(dx^2));
k2=((alpha*dt)/(dy^2));
cfl=k1+k2;
error=9e9;
tol=1e-4;
%number of time steps
nt=t/dt;
%jacobi method
iterative_solver=1;
if iterative_solver==1
ex_iter=1;
for nts=1:nt;
error=1;
while error>tol
for i=2:nx-1;
for j=2:ny-1;
term1=(told(i-1,j)-2*t(i,j)+t(i+1,j));
term2=(t(i,j-1)-2*t(i,j)+t(i,j+1));
t(i,j)=t_pre_dt(i,j)+((k1*term1)+(k2*term2));
end
end
error=max(max(abs(told-t)));
told=t;
ex_iter=ex_iter+1;
end
t_prev_dt = t;
end
end
contourf(x,y,t,'showtext','on')
colormap(jet)
colorbar
xlabel('x_axis');
ylabel('y_axis');

from the above results, the following observations have been made
for both steady and transient analysis Jacobi iterations are always greater than the Gauss-Siedel iterations
but for the successive over-relaxation method the no. of iterations mainly depends upon the value of omega either it is over relaxed or under relaxed in the problem we considered omega=1.2 therefore, the no. of iterations are greater than guass-Siedel iterations.