Challenge

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

%%Steady state


clear all
close all
clc
ny=10;
nx=ny;
L=1;
dx=L/(nx-1);
dy=L/(ny-1);
x=linspace(0,L,nx);
y=linspace(0,L,ny);
k1=0.5*(dy^2/(dx^2+dy^2));
k2=0.5*(dx^2/(dx^2+dy^2));
%After applying the Boundary conditions
u=temperature_steady(nx,ny);
tol=1e-4;

%steady state Heat Conduction
iterative_solver=1;
if iterative_solver==1
  [u, jb_iter] =jacobi_method(u,tol,nx,ny,k1,k2);
  figure
  C=contourf(x,y,u,'edgecolor','none');
  clabel(C)
  colormap(jet)
  colorbar
  set(gca,'YDir','reverse')
end
if iterative_solver==2
    [u, gs_iter] =gs_method(u,tol,nx,ny,k1,k2);
     figure
  C=contourf(x,y,u,'edgecolor','none');
  clabel(C)
  colormap(jet)
  colorbar
  set(gca,'YDir','reverse')
end
if iterative_solver==3
    [u, sor_iter] =sor_method(u,tol,nx,ny,k1,k2);
     figure
  C=contourf(x,y,u,'edgecolor','none');
  clabel(C)
  colormap(jet)
  colorbar
  set(gca,'YDir','reverse')
end


Functions: 

Tmeperature matarix :
function out=temperature_steady(nx,ny)
   u=ones(nx,ny);
   %Applying BC to the system
   %T(top)=600k;T(bottom)=900k;T(right)=800k ; T(left)= 400
   %Consider Corner points to be average of adjacent points
   u(1)=500;
   u(nx*ny)=850;
   u((nx*(ny-1))+1)=700;
   u(nx)= 650;
   for i=2:(nx-1)
       u(i)=400;
   end
   for i=nx+1:nx:(nx-1)*(ny-1)
       u(i)=600;   
   end
   for i= 2*nx:nx:(nx*ny)-1
       u(i)=900;
   end
   for i=((nx-1)*ny)+2:((nx*ny)-1)
       u(i)=800;
   end
   out=u;
end
2) Jccobi Function :
function [out jb_iter] = jacobi_method(u,tol,nx,ny,k1,k2)
%Steady State Heat Transfer
   error=9e9;
   uold=u;
   jb_iter=1;
while(error>tol)
%Spatial loop 
   %x-loop
  for i=2:nx-1  
    % y-loop
      for j=2:ny-1
        u(i,j)=k1*(uold(i+1,j)+uold(i-1,j))+k2*(uold(i,j+1)+uold(i,j-1));
      end
  end
a=abs(uold-u);
error= max(a,[],'all');
uold=u;
jb_iter=jb_iter+1;
end
out=u;
end

3) Gauss-cidel Function :
function [out gs_iter] = gs_method(u,tol,nx,ny,k1,k2)
   error=9e9;
   uold=u;
   gs_iter=1;
while(error>tol)
%Spatial loop 
   %x-loop
  for i=2:nx-1  
    % y-loop
      for j=2:ny-1
        u(i,j)=k1*(uold(i+1,j)+u(i-1,j))+k2*(uold(i,j+1)+u(i,j-1));
      end
  end
a=abs(uold-u);
error= max(a,[],'all');
uold=u;
gs_iter=gs_iter+1;
end
out=u;
end
4)SOR METHOD:
function [out sor_iter] = sor_method(u,tol,nx,ny,k1,k2)
   error=9e9;
   uold=u;
   sor_iter=1;
   alpha=1.6;
while(error>tol)
%Spatial loop 
   %x-loop
  for i=2:nx-1  
    % y-loop
      for j=2:ny-1
        u(i,j)=(1-alpha)*uold(i,j)+alpha*(k1*(uold(i+1,j)+u(i-1,j))+k2*(uold(i,j+1)+u(i,j-1)));
        
      end
  end
a=abs(uold-u);
error= max(a,[],'all');
uold=u;
sor_iter=sor_iter+1;
end
out=u;
end 


%% TRANSIENT OR UNSTEADY STATE EQUATION









2D steady state equation

d2T/dx2+ d2T/dy2 = 0

we need find the temperature at Tp i..e Ti,j

let

Ti-1,j= TL

Ti+1,j= TR

Ti,j-1= TB

Ti,j+1= TT

By Central differencing scheme we can write below equation as

d2T/dx2+ d2T/dy2 = 0

(Ti+1,j-2Ti,j+Ti-1,j)/Δx2 + (Ti,j+1-2Ti,j+Ti,j-1)/Δy2 = 0

By above terminology and after arranging the terms we can deduce the equation to

2*Tp =( Δy2/ Δx2+ Δy2)(TR+TL) + ( Δx2/ Δx2+ Δy2)(TT+TB)

Let

0.5*(Δy2/ Δx2+ Δy2) = k1

0.5*(Δx2/ Δx2+ Δy2)=k2

Tp =k1(TR+TL) + k2 (TT+TB)…………… (1)

Tp =k1(TR +TL) + k2 (TT+TB)……..equation at the required point.

This can be solved by three process

1)Jacobi

2)Gauss-cidel

3)Sucessive over relaxation(SOR)

1)Jaccobi Method:

Tp =k1(TR +TL) + k2 (TT+TB) This equation is calculated at the nth time interval only Tp is at n+1 time interval.

So the above equation will be used in the program as

Tpn+1 = k1 (Ti+1,jn + Ti-1,jn ) +k2 (Ti,j+1n +Ti,j-1n )……………(2)

Where n – old time interval

N+1 – next time interval

2)Gauss-cidel Method:

Tp =k1(TR +TL) + k2 (TT+TB)

Known values in this equation are n time interval and unknown values are from n+1 .

So the above equation will be used in the program as

Tpn+1 = k1 (Ti+1,jn+1 + Ti-1,jn ) +k2 (Ti,j+1n+1 +Ti,j-1n )……………(3)

Where n – old time interval

N+1 – next time interval

3)SOR Method:

Tp =k1(TR +TL) + k2 (TT+TB)

α - SOR cofeeicient

SOR method is an enhancer for Jacobi or gauss cidel method

So the above equation will be used in the program as

Tpn+1 = 1-α(Tpn) + α( k1 (Ti+1,jn+1 + Ti-1,jn ) +k2 (Ti,j+1n+1 +Ti,j-1n ))……………(4)

Gauss cidel solution

Where n – old time interval

N+1 – next time interval

Graphs :

Jacobi Metehod:

Guass-cidel Method:

SOR Method:

Conclusions:

1)All the graphs from three methods looks similar but the number of iterations to achieve a steady solution varies.

2) Number of iterations to achieve the steady solution in Jacobi method is greater than gauss-cidel greater than SOR method.

Jacobi_iter>gs_iter>sor_iter

UnSteady State or Transient :

d2T/dt2+ α (d2T/dx2+ d2T/dy2) = 0

Ti,jn+1- Ti,jn / Δt = α ((Ti+1,j-2Ti,j+Ti-1,j)/Δx2 + (Ti,j+1-2Ti,j+Ti,j-1)/Δy2)

Ti-1,j= TL

Ti+1,j= TR

Ti,j-1= TB

Ti,j+1= TT

Tpn+1 = Tpn + α Δt/ Δx2 ( TRn + TLn)+ α Δt/ Δy2 (TT + TB)-2* α*Δt*Tp(1/Δx2+1/Δy2)

Let α*Δt/Δx2 = k1…………this is also called as CFLX ……….(4)

α*Δt/Δy2 = k2 ………..this is also called as CFLy………..(5)

let α = 1.5 for solids heat conduction.

To solve unsteady state equation we have to solve it time marching and also by the iterations. Time marching has two methods implicit and explicit methods.

We need to calculate the value of Δt by following equation

CFL ≤0.5………… (6)

CFL = CFLx + CFLy

K1 +k2 = 0.5 from eq 4,5 & 6

α*Δt/Δx2 + α*Δt/Δy2 = 0.5

by solving we will get Δt value.

Explicit Method:

Ti,jn+1- Ti,jn / Δt = [α ((Ti+1,j-2Ti,j+Ti-1,j)/Δx2 + (Ti,j+1-2Ti,j+Ti,j-1)/Δy2)] n

Complete R.H.S term is calculated at nth time interval.so final equation can be deduced to

Tpn+1 = Tpn [1-2k1-2k2] + k1[ TR+TL]n + k2[TT+TB]n

Graph:

Implicit Method:

Ti,jn+1- Ti,jn / Δt = [α ((Ti+1,j-2Ti,j+Ti-1,j)/Δx2 + (Ti,j+1-2Ti,j+Ti,j-1)/Δy2)] n+1

Complete R.H.S term is calculated at n+1th time interval.so final equation can be deduced to

Tpn+1=[Tpn +k1(TR+TL )n+1 +k2 (TT+TB)n+1 ] /(1+2k1+2k2)……………

1)Jaccobi Method:

Tpn+1=[Tpn +k1(TR+TL )n+1 +k2 (TT+TB)n+1 ] /(1+2k1+2k2) This equation is calculated at the nth time interval , only Tp at L.H.S is at n+1 time interval.

So the above equation will be used in the program as

Tpn+1 = k1 (Ti+1,jn + Ti-1,jn ) +k2 (Ti,j+1n +Ti,j-1n )……………(2)

Where n – old time interval

N+1 – next time interval

2)Gauss-cidel Method:

Tpn+1=[Tpn +k1(TR+TL )n+1 +k2 (TT+TB)n+1 ] /(1+2k1+2k2) ] above equation can be deduced to

Tpn+1 = Tpn *1/(1+2k1+2k2) +k1/(1+2k1+2k2) *(TLn+1 +TRn)+ k2/(1+2k1+2k2) * (TTn+1 + TBn)

Where in coding we can designate as

1/(1+2k1+2k2) = term1

k1/(1+2k1+2k2) =term2

k2/(1+2k1+2k2)= term3

final equation is

Tpn+1 = term1*Tpn + term2* (TLn+1 + TRn)+ term3*(TTn+1 + TBn)

Where n – old time interval

N+1 – next time interval

3)SOR Method :

Tpn+1=[Tpn +k1(TR+TL )n+1 +k2 (TT+TB)n+1 ] /(1+2k1+2k2) ] above equation can be deduced to

α - SOR coefficient

SOR method is an enhancer for Jacobi or gauss cidel method

So the above equation will be used in the program as

Tpn+1 = 1-α(Tpn) + α(Tpn *1/(1+2k1+2k2) +k1/(1+2k1+2k2) *(TLn+1 +TRn)+ k2/(1+2k1+2k2) * (TTn+1 + TBn) )……………(3)

final equation is

Tpn+1 = 1-α(Tpn) + α (term1*Tpn + term2* (TLn+1 + TRn)+ term3*(TTn+1 + TBn)

Where n – old time interval

N+1 – next time interval

Graph:

Conclusions:

1)Solving the function in explicit has more iterations than solving the equation in implicit method.

2)All the graphs from three methods looks similar but the number of iterations to achieve a steady solution varies.

3) Number of iterations to achieve the steady solution in Jacobi method is greater than gauss-cidel greater than SOR method.

Jacobi_iter>gs_iter>sor_iter

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.