Professional Documents
Culture Documents
%========================================
%
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