Rachid Brah
Rachid Brah

Reputation: 33

Finite differences

I am trying to resolve a 2D heat transfer problem (I am a MATLAB beginner). I have written the code below, and when I run it it does not stop. I tried to incorporate a convergence condition using the error (err), but whenever I stop the program I always find that my error is 150. Could someone help to adjust this code to get the convergence check?

(By the way, my project is a 3D project, I am just trying a 2D case then once I get it working I will expand it).

%===Solution of the problem of 2D in the bookmarks:Case2a=====

close all;
clear all;
clc;

DX=3; % step size
DY=3; 
Lx= 60; %length along x-axis in cmn
Ly=30;   %length along y-axis
m=Lx/DX+1;    %Number of nodes along x-axis
n=(Ly/DY+1);    %Number of nodes along y-axis
n=floor(n);

k=2;
h=500;
T_inf=20;

X=0:DX:Lx;
Y=0:DY:Ly;
T=zeros(m,n);

tol=1;
s=0; %s=2000 could be set as the maximum number of allowed iteration for example
err=1; 
T_old=10;

while err >=tol && s<2001
s=s+1;

%--boundary conditions----------------------------------------------------

T(1,:)=160;     %west
T(m,:)=100;    %east

%===South Boundary "insulation" ==============
for i=2:m-1
   T(i,1)=0.25*[2*T(i,2)+T(i-1,1)+T(i+1,1)];
end
%================North Boundary "Convection" ================
for i=2:m-1
  T(i,n)=0.5*k/(h*DX+2*k)*[2*T(i,n-1)+T(i-1,n)+T(i+1,n)+2*h*DX*T_inf/k];
end

for i = 2:m-1
    for j = 2:n-1
    T(i,j)=0.25*(T(i-1,j)+T(i+1,j)+T(i,j-1)+T(i,j+1));
    end
end
err=max(max(abs(T-T_old)));
end

%T=rot90(T)

Upvotes: 1

Views: 114

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60810

You should probably assign T to T_old inside your loop, after computing err.

Upvotes: 2

Related Questions