Everything seem's ok, but my solution's is wrong.
%% 2D HEAT EQUATION WITH CONSTANT TEMP. AT BC'S
%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////
%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////%\\\\\\\\\\\\\\\\\\\\\\\///////////////////////%% INITIALIZING
clcclear close all%% PARAMETERIZATION
a = 1e-4; % THERMAL DIFFUSIVITY
t = 200; % TOTAL TIME
nt = 2; % TOTAL NUMBER OF TIME STEPS
dt = t/nt; % TIMESTEP
L = 1; % X = Y
nx = 4; % TOTAL NUMBER OF SPATIAL GRIDS
dx = L/nx; % dX = dY
%% BC'S
T1 = 100; % BCS OF DOMAIN
T2 = 100;T3 = 100;T4 = 100;T5 = 200; % INITIAL TEMP.
Tmax = max([T1,T2,T3,T4,T5]);%% DEFINITION
m = (L/dx)+1; % NO. OF X STEP (X = Y)
r = (t/dt)+1; % NO. OF TIME STEP
d = a*dt/(dx)^2; % DIFF. NO.
%% CREATING BC's & IC'S
B = zeros(m-2,m-2);[nr,nc] = size(B);nT = nr * nc; B = zeros(m,m);B(:,end) = T1;B(end,:) = T2;B(1,:) = T3;B(:,1) = T4;B(2:end-1,2:end-1) = T5; %% EDGES MUST BE REFINED
B1 = B(2:end-1,2:end-1)';BC_v = B1(:); %IC's
%% CREATE TDMA LHS
AA2(1:nT) = 1+4*d;AA3(1:nT-1) = -d;AA5(1:nT-3) = -d;AA = diag (AA2,0) + diag (AA3, -1) + diag (AA3,1) + ... diag(AA5,-3)+diag(AA5,3); %% LHS HAS BEEN CREATEAD
%% CREATE RHS
T13(:,:,:) = zeros(size(B,1),size(B,1),r); % Define a cubic matrix
n = size(AA,1);T13(:,:,1) = B;BB(n,1) = 0;for i = 1:n if mod(i,2) == 1 BB(i,1) = T1+T2; end if mod(i,2) ~= 1 BB(i,1) = T1; end if mod(n,2)~=1 BB((n+1)/2,1) = 0; elseif mod(n,2)==1 BB((n+1)/2,1) = 0; endend RHS = BC_v + d*BB;for k = 1:r%\\\\\Solution of x in Ax=b using Gauss Seidel Method/////
%\\\\\Solution of x in Ax=b using Gauss Seidel Method/////C = RHS; % constants vector
n = length(C);X = zeros(n,1);Error_eval = ones(n,1);% Start the Iterative method
iteration = 0;while max(Error_eval) > 1e-7 iteration = iteration + 1; Z = X; % save current values to calculate error later
for i = 1:n j = 1:n; % define an array of the coefficients' elements
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(i) = []; % eliminate the unknown under question from the set of values
X(i) = (C(i) - sum(AA(i,j) * Xtemp)) / AA(i,i); end Error_eval = sqrt((X - Z).^2);end%\\\\\Solution of x in Ax=b using Gauss Seidel Method/////%\\\\\Solution of x in Ax=b using Gauss Seidel Method///// RHS = X + d*BB; T13(2:end-1,2:end-1,k+1) = (vec2mat(RHS',m-2)); % VEC. TO MAT.
T13(:,end,k+1) = T1; T13(end,:,k+1) = T2; T13 (1,:, k + 1) = T3; T13 (:, 1, k + 1) = T4;end %% Animation
x = 1:m;y = 1:m;T21 = T13 (:,:, 1); for k = 1:r-1 figure(1) h = imagesc(x,y,T21); shading interp axis([0 m 0 m 0 Tmax]) title({['Transient Heat Conduction'];['time = ',... num2str((k)*dt),' s']}) colorbar; drawnow; xlabel(['T = ',num2str(T2),'C']) yyaxis left ylabel(['T = ',num2str(T4),'C']) yyaxis right str = 'T0 = 20C' ; dim = [.6 0.55 .3 .3]; pause(0.1); refreshdata(h); if k~=r T21 = T13(:,:,k+1); else break; end end
Best Answer