You are on page 1of 4

clear; % clear current available datas

clc; % clear workspace


%===================================================
%
ASSIGNMENT 3
%===================================================
tic
L=1; % domain length
N=21; % total number of cells
n=N+2; % no of nodes
delx=L/N; % delta-x
delt=0.001; % time step
tstep=1001; % no. of time step
f=1; % relaxation (i) f=1 explicit (ii) f=0.5 Crank Nicolson (iii)
f=0 implicit
current_tstep=0; % set initial time step no
time=0; % initial time = 0
rc=1; % rho*c (J/m^3K) - set as 1 to impose non-dimensional
parameter
k=1; % thermal conductivity (W/mK) - set as 1 to impose nondimensional parameter
%===================================================
%
INITIALIZE ZEROS ARRAYS
%===================================================
x=zeros(n,1);
T_t=zeros(N,1);
T_tplus=zeros(N,1);
T=zeros(n,1);
G=zeros(N,N); % G matrix
F=zeros(N,1); % F matrix
G_td=zeros(N,N); % G matrix for tridiagonal system
F_td=zeros(N,1); % F matrix for tridiagonal system
% Initialize Data Storage Matrix
data=zeros(2+n,1+tstep); % prepare matrix for saving all data
data(1,1)=11000011;
data(2,1)=11000011;
%===================================================
%
INITIAL CONDITION
%===================================================
A=-f*k/delx; % set A value
B=rc*(delx/delt)+(2*f*k/delx); % set B value
C=(1-f)*k/delx; % set C value
D=rc*(delx/delt)-(2*(1-f)*k/delx); % set D value
% --- calculate x-location matrix values --x(1)=0;
x(2)=delx/2;
x(n)=L;
x(n-1)=L-delx/2;
for i=3:(n-2)
x(i)=x(i-1)+delx;
end

%========================================
%
Set Initial Condition
%========================================
T_left_t=1; % T = 1 at left boundary at t=0
T_right_t=1; % T = 1 at right boundary at t=0
for i=1:N
T_t(i)=1; % set value 1 for all T_t at t=0
end
% write Tc data into T matrix
T(1)=T_left_t;
T(n)=T_right_t;
for i=1:N
T(i+1)=T_t(i);
end
%----------------------------------------%===================================================
%
TIME MARCH
%===================================================
while current_tstep<tstep
% --- display T-x plot --% This part displays the T-x plot for the previous time step's
data
figure(1);
plot(x,T,'-o');
title(['vol:' num2str(N) ',' 'f:' num2str(f) ',' 'time step:'
num2str(delt) ',' 'time:' num2str(time)])
xlabel('x')
ylabel('T')
%drawnow
%pause
%-------------------------% ------ Write data to DATA Matrix -----% Write x-location values
for i=1:n
data(2+i,1)=x(i);
end
% Write time
data(1,2+current_tstep)=current_tstep;
data(2,2+current_tstep)=time;
% Write temperature
for i=1:n
data(2+i,2+current_tstep)=T(i);
end
%---------------------------------------%========================================
%
Calculate Matrix
%========================================
T_left_tplus=0; % B.C at t>0
T_right_tplus=0; % B.C at t>0

% Calculate G Matrix
G(1,1)=rc*(delx/delt)+(3*f*k/delx);
G(1,2)=-f*k/delx;
G(N,N)=rc*(delx/delt)+(3*f*k/delx);
G(N,N-1)=-f*k/delx;
for i=1:(N-2)
G(i+1,i)=A;
G(i+1,i+1)=B;
G(i+1,i+2)=A;
end
% Calculate F Matrix
F(1)=((2*f*k/delx)*T_left_tplus)+((2*(1-f)*k/delx)*T_left_t)+
((rc*(delx/delt)-(3*(1-f)*k/delx))*T_t(1))+(((1-f)*k/delx)*T_t(2));
F(N)=(((1-f)*k/delx)*T_t(N-1))+(((rc*(delx/delt))-(3*(1-f)*k/
delx))*T_t(N))+((2*f*k/delx)*T_right_tplus)+((2*(1-f)*k/
delx)*T_right_t);
for i=1:(N-2)
F(i+1)=(C*T_t(i))+(D*T_t(i+1))+(C*T_t(i+2));
end
%===========================================================
%
Tridiagonal System
%===========================================================
% Calculate G-tridiagonal Matrix
G_td(1,1)=G(1,1);
G_td(1,2)=G(1,2);
for i=1:(N-1)
G_td(i+1,i+1)=G(i+1,i+1)-(G(i+1,i)*G(i,i+1)/G_td(i,i));
end
for i=1:(N-2)
G_td(i+1,i+2)=G(i+1,i+2);
end
% Calculate F-tridiagonal Matrix
F_td(1)=F(1);
for i=1:(N-1)
F_td(i+1)=F(i+1)-(F_td(i)*G(i+1,i)/G_td(i,i));
end
% Solve and find T_tplus
T_tplus(N)=F_td(N)/G_td(N,N);
for i=1:(N-1)
j=N-i;
T_tplus(j)=(F_td(j)-G_td(j,j+1)*T_tplus(j+1))/G_td(j,j);
end
%===========================================================
%
Data Management
%===========================================================
% Update T matrix

T(1)=T_left_tplus;
T(n)=T_right_tplus;
for i=1:N
T(i+1)=T_tplus(i);
end
% Update T_t for the next time step
for i=1:N
T_t(i)=T_tplus(i);
end
%----------------------------------------------------------% Update Time
current_tstep=current_tstep+1; % march in time
time=time+delt; % update time
% Update Boundary Condition
T_left_t=0;
T_right_t=0;
%----------------------------------------------------------end
toc

You might also like