Week 5.1 - Mid term project - Solving the steady and unsteady 2D heat conduction problem

FUNCTION CODE:
% FOR JACOBI
function out=jacobi(t,told,i,j)
out=0.25*((told(i,j-1)+told(i,j+1)+told(i-1,j)+told(i+1,j)))
end
% FOR GS
function out=GS(t,told,i,j)
out=0.25*((t(i,j-1)+told(i,j+1)+t(i-1,j)+told(i+1,j)))
end
% FOR sor
function out=sor(t,told,i,j,f)
out=(1-f)*told(i,j)+f*(0.25*(t(i,j-1)+told(i,j+1)+t(i-1,j)+told(i+1,j)))
end
MAIN CODE:
%main program
clear all
close all
clc
% number of grid points along x and y axis
nx=10;
ny=10;
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,ny);
z=fliplr(y);
f=1.1;
iter=1;
t=300*ones([nx,ny]);
% applying BC
t(1,:)=600;
t(end,:)=900;
t(:,1)=400;
t(:,end)=800;
% creating a copy
told=t;
r1=t;
error=19;
tol=1e-4;
for iterative_solver=1:3;
while(error>tol);
for i=2:nx-1;
for j=2:ny-1;
if(iterative_solver==1);
t(i,j)=jacobi(t,told,i,j);
elseif(iterative_solver==2)
t(i,j)=GS(t,told,i,j);
elseif(iterative_solver==3)
t(i,j)=sor(t,told,i,j,f);
end
end
end
error=max(max(t-told));
told=t;
[u1,v1]=meshgrid(x,z);
%plot
if(iterative_solver==1);
figure(1);
contourf(u1,v1,t);
[C,h]=contourf(u1,v1,t);
colorbar;
colormap(jet);
clabel(C,h);
xlabel('x-axis');
ylabel('y-axis');
title_text=sprintf('jacobi iterention number=%d',iter);
title(title_text);
pause(0.003);
elseif(iterative_solver==2);
figure(2);
contourf(u1,v1,t);
[C,h]=contourf(u1,v1,t);
colorbar;
colormap(jet);
clabel(C,h);
xlabel('x-axis');
ylabel('y-axis');
title_text=sprintf('GS iterention number=%d',iter);
title(title_text);
pause(0.003);
else
figure(3);
contourf(u1,v1,t);
[C,h]=contourf(u1,v1,t);
colorbar;
colormap(jet);
clabel(C,h);
xlabel('x-axis');
ylabel('y-axis');
title_text=sprintf('sor iterention number=%d',iter);
title(title_text);
pause(0.003);
end
iter=iter+1;
end
error=19;
ioter=1;
t=r1;
told=t;
end
Result:



for unsteady:

For Explicit Method:
clear all
close all
clc
% number of grid points along x and y axis
nx=10;
ny=10;
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,nx);
dy=y(2)-y(1);
c=1;
%number of steps
nt=200;
% time steps
dt=0.001;
% CFL number
k=(c*dt)/dx^2;
t=ones([nx,nx])*673;
%applying BC
t(1,:)=600;
t(end,:)=900;
t(:,1)=400;
t(:,end)=800;
told=t;
tic
iter=1
for p=1:nt
for i=2:nx-1
for j=2:ny-1
t(i,j)=k*(told(i-1,j)+told(i+1,j)+told(i,j+1)+told(i,j-1))+ (1-4*k)*told(i,j);
end
end
error=max(abs(told-t));
told=t;
iter=iter+1;
end
time=toc;
figure(1)
[c,h]=contourf(x,fliplr(y),t);
colorbar;
colormap(jet);
clabel(c,h);
title(sprintf('2D unstudy heat conduction(Explicit method)n iteration number=%d; t computational time:%0.4f',iter,time));
xlabel('xx');
ylabel('yy');
RESULT:

FOR IMPLICIT METHOD:
FUNCTON CODE:
% FOR GS
function [iter,t]=jacob(told)
nx=10;
ny=10;
alpha=1.4;
dt=0.001;
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,nx);
dy=y(2)-y(1);
tol=1e-4;
t=told;
t_initial=t;
t_previous=t;
iter=1;
tic;
% defining k1 and k2
k1=alpha*(dt/dx^2);
k2=alpha*(dt/dy^2);
for nt=1:1400
error=9e9;
while error>tol
for i=2:nx-1
for j=2:ny-1
term1=1/(1+2*k1+2*k2);
term2=k1*term1;
term3=k2*term1;
t(i,j)=((t_previous(i,j)*term1)+term2*(told(i-1,j)+told(i+1,j))+(term3*(told(i,j-1)+told(i,j+1))));
end
end
error_n=max(abs(told-t));
error=max(error_n);
told=t;
iter=iter+1;
end
t_previous=t;
end
time=toc;
figure(1)
contourf(x,fliplr(y),t);
%[C,h]=contourf(x,fliplr(y),t);
colorbar;
colormap(jet);
%clabel(C,h);
text=sprintf('JACOBI Method n iteration number =%d; t computational time:%0.4f',iter,time);
title(text);
end
% FOR JACOBI
function [iter,t]=gs(told)
nx=10;
ny=10;
alpha=1.4;
dt=0.001;
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,nx);
dy=y(2)-y(1);
tol=1e-4;
t=told;
t_initial=t;
t_previous=t;
iter=1;
tic;
% defining k1 and k2
k1=alpha*(dt/dx^2);
k2=alpha*(dt/dy^2);
for nt=1:1400
error=9e9;
while error>tol
for i=2:nx-1
for j=2:ny-1
term1=1/(1+2*k1+2*k2);
term2=k1*term1;
term3=k2*term1;
t(i,j)=((t_previous(i,j)*term1)+term2*(t(i-1,j)+told(i+1,j))+(term3*(t(i,j-1)+told(i,j+1))));
end
end
error_n=max(abs(told-t));
error=max(error_n);
told=t;
iter=iter+1;
end
t_previous=t;
end
time=toc;
figure(2)
contourf(x,fliplr(y),t);
%[C,h]=contourf(x,fliplr(y),t);
colorbar;
colormap(jet);
%clabel(C,h);
text=sprintf('GS Method n iteration number =%d; t computational time:%0.4f',iter,time);
title(text);
end
% FOR sor
function [iter,t]=SOR(told,f)
nx=10;
ny=10;
alpha=1.4;
dt=0.001;
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,nx);
dy=y(2)-y(1);
tol=1e-4;
t=told;
t_initial=t;
t_previous=t;
tic;
iter=1;
% defining k1 and k2
k1=alpha*(dt/dx^2);
k2=alpha*(dt/dy^2);
for nt=1:1400
error=9e9;
while error>tol
for i=2:nx-1
for j=2:ny-1
term1=1/(1+2*k1+2*k2);
term2=k1*term1;
term3=k2*term1;
t(i,j)=(1-f)*told(i,j)+f*((t_previous(i,j)*term1)+(term2*(t(i-1,j)+told(i+1,j))+(term3*(t(i,j-1)+told(i,j+1)))));
end
end
error_n=max(abs(told-t));
error=max(error_n);
told=t;
iter=iter+1;
end
t_previous=t;
end
time=toc;
figure(3)
contourf(x,fliplr(y),t);
%[C,h]=contourf(x,fliplr(y),t);
colorbar;
colormap(jet);
%clabel(C,h);
text=sprintf('SOR Method n iteration number =%d; t computational time:%0.4f',iter,time);
title(text);
end
MAIN CODE:
clear all
close all
clc
% number of grid points along x and y axis
nx=10;
ny=10;
f=1.2;
%applying BC
t=ones(nx,ny)*298;
t(1,:)=600;
t(end,:)=900;
t(:,1)=400;
t(:,end)=800;
alpha=1.4;
dt=0.001;
told=t;
t_initial=t;
t_previous=t;
for iterative_solver=1:3
iterative_solver=1;
[iter,t]=jacob(told);
iterative_solver=2;
[iter,t]=gs(told);
iterative_solve=3;
[iter,t]=SOR(told,f);
end
PLOT:



Conclusion:
While comparing botj Explicit and Implicit methods results,the Expliocit method is much quicker than the implicit method