Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

clc;

clear;
%% Initializing grid size as 10,20,40,80
n = 10; % grid size
NX = n;
NY = n;
k = 16.2;
rho = 7750;
cp =500;
alpha = k/(rho*cp); % alpha
itr = 1;
t = 0;
Tn = (150+273)*ones(NX+2,NY+2); % ic temp at n time step
Tn1 = zeros(NX+2,NY+2); % temp at n+1 time step
dx = 1/NX;
dy = 1/NY;
dt = 0.8;
Rrms = inf;
for i = 1:NX+2
Tn(i,1) = 2*(100+273)-Tn(i,2); % left boundary
Tn(i,NY+2) = 2*(100+273)-Tn(i,NY+1); % right boundary
end
for j = 1: NY+2
Tn(1,j) = 2*(200+273)-Tn(2,j); % top boundary
Tn(NX+2,j) = 2*(100+273)-Tn(NX+1,j); % bottom boundary
end
Tn1 = Tn;
Tn % temp values
while (Rrms>1e-6)
Rrms = 0;
for i = 2:NX+1
for j = 2:NY+1
Tn1(i,j) = Tn(i,j) + ((Tn(i+1,j)-2*Tn(i,j)+Tn(i-1,j))/(dy^2)+(Tn(i,j+1)-
2*Tn(i,j)+Tn(i,j- 1))/(dx^2) ) * (alpha*dt); % FDM equation for diffusion equation
Rrms = Rrms + (Tn1(i,j)-Tn(i,j))^2;
end
end

% boundary cells
for i = 1:NX+2
Tn1(i,1) = 2*(100+273)-Tn1(i,2);
Tn1(i,NY+2) = 2*(100+273)-Tn1(i,NY+1);
end
for j = 1: NY+2
Tn1(1,j) = 2*(200+273)-Tn1(2,j);
Tn1(NX+2,j) = 2*(100+273)-Tn1(NX+1,j);
end

tt(itr) = t;
Temp(itr) = Tn(NX/2,NY/2); t = t+dt;
hold on;
Rrms = sqrt(Rrms/(NX*NY));
Tn = Tn1;
itr = itr+1;
end
itr % no of iterations
figure(1)
plot(tt,Temp);
x = linspace(0,1,n+2);
y = linspace(0,1,n+2);
xlabel("Time");
ylabel("Temperature at centre");
figure(2);
pcolor(x(2:n+1),y(2:n+1),flip(Tn(2:NX+1,2:NY+1))),shading interp,colormap jet;
%question 2 sub question 1
% comparison with analytical solutions
alpha1 = 0;
T1 = 373;
T2 = 573;
for n = 1:10
alpha1 = alpha1 + (((-1)^(n+1)+1) * sin(n*pi/2)* sinh(n*pi/2)/(n*sinh(n*pi)));
% at center of grid
end
%beta = symsum(alpha, n , 1, 10);
T = T1 + 2*(T2-T1)* alpha1/pi % analytical solution of temp at center of grid
% question 2 sub question 3
% heat flux
sum1=0;
sum2=0;
for i=2:NX+1
for j=2:NY+1
sum2=sum2-(k*((Tn1(NX+2,j)-Tn1(NX+1,j)+Tn1(2,j)-Tn1(1,j)))/dy); % top and
bottom boundaries
end
sum1=sum1-(k*((Tn1(i,2)-Tn1(i,1)+Tn1(i,NY+2)-Tn1(i,NY+1)))/dx); % right and
left boundaries
end
sum1
sum2

You might also like