How can i get the temperature convergence
Show older comments
I am trying to resolve a 2D heat transfer problem (I am a Matlab starter). 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 was 150. Could someone help to adjust this code to get the convergence check. (By the was my project is a 3d project, I am just trying a 2d case then once i get it working I will expand it). Thank you for the help
%===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)
2 Comments
Roger Stafford
on 27 Nov 2017
I see a possible problem in your doubly nested for-loops. At each trip through the 'while' loop you are clearly attempting to make each internal T(i,j) set to the average of the four surrounding T values. However, your method has the disadvantage that after altering T(i,j) you then use this altered value in computing T(i,j+1) rather than its original value. The same trouble is present in the two "boundary" for-loop. You need to retain original values until you have finished operation on the entire matrix by placing the results in a different matrix, and then copy these altered values back into the T values.
Another problem is that you don't update the variable 'T_old' so it will always remain at its original value of 10.
Rachid
on 27 Nov 2017
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!