# 2D steady state heat conduction equation using Jacobi iteration

60 views (last 30 days)
Zahraa on 20 Sep 2020
Commented: Zahraa on 20 Sep 2020
2D steady heat conduction equation on the unit square subject to the following
Dirichlet boundary conditions:
T(x,0)=100x T(0,y)=200y
T(x,1) =200+100sin(pi*x) T(1,y)=100(1+y)
T(x,y) =0 (initial condition)
Use uniform grid in x and y of 21 points in each direction. Iterate until the maximum change is less than 0.1.
I set up the problem using a Jacobi iteration method;
and I have an issue setting up the boundary conditions correctly. Whenever I run the code it show me this (Unable to perform assignment because the size of the left side is 21-by-1 and the size of the right side is 21-by-21.) if any one could solve the issue for me? Thanks in advance !
Here's my code
for I=1:3
tic
% problem setup
Lx = 1; %length of x grid
Ly = Lx; % length of y grid
nx = 21; % number of nodes in x- direction
ny = nx; %number of nodes in y- direction
dx = Lx/(nx-1); % step size for spatial x-cord
dy = Ly/(ny-1); % step size for spatial y-cord
tol = 0.1; %error criteria
error = 9e9; %initialising error
k=0; %initialising iteration
% creating 1D and 2D grid
x = linspace(0,Lx,nx); %1d x grid
y = linspace(0,Ly,ny); %1d y grid
[x y] = meshgrid(x,y); %2d x-y grid
% initialising temperature array and BC
T = zeros(nx,ny); %initial condition array
% BC
T(1:ny,ny)= 100*(1+y) ;
T(1:ny,1)= 200*y ;
T(nx,1:nx)= 200+100*sin(pi*x) ;
T(1,1:nx)= 100*x ;
%creating a copy of temperature array after BC
Told = T;
while error > tol %while loop runs untill error > tolerance value
for i = 2:nx-1 %for loop runs for x values from 2 to last node -1
for j = 2:ny-1 %for loop runs for y value form 2 to last node -1
%iterative solver
if I== 1 %jacobi
figure(1)
T(i,j) = 0.25*(Told(i-1,j) + Told(i+1,j) + Told(i,j-1) + Told(i,j+1));
end
end
end
error_T = max(abs(Told - T)); %calculating error to decide wether while loop should stop or not
error = max(error_T); %calculating maximum error between absolute value of old arry T and new arry of T
Told = T; %updating temperature array for next iteration
k = k+1; %incrementing iterations by 1
% contour map
[C, h] = contourf(x,y,T); %contourf function used
clabel(C, h,'FontSize',8,'FontWeight','bold','color','k')
xlabel('spatial X -coordinate','FontSize',12)
ylabel('spatial Y -coordinate','FontSize',12)
title (sprintf('Temperature distribution on a %d x %d gridn using Jacobi method at iteration no %d', Lx, Ly, k),'FontSize',10)
pause(0.003)
end
##### 2 CommentsShowHide 1 older comment
Zahraa on 20 Sep 2020
thank you for your comment, I just edit it !

esat gulhan on 20 Sep 2020
Try this instead of BC;
T(1:ny,ny)= 200+100*(sin(pi*y(:,1))) ;
T(1:ny,1)= 200+100*y(:,1)
T(nx,1:nx)= 200*x(1,:) ;
T(1,1:nx)= 100*(1+x(1,:)) ;
##### 1 CommentShowHide None
Zahraa on 20 Sep 2020
yes it works, thanks !