Reputation: 13
I am working on this coding for my heat transfer class. The number are suppose to be other equations but I replaced them with numbers to simply it
l=.2;
w=.2;
dx=.05;
dy=.05;
k=400;
q=500;
Nx = (l/dx+1); %nodes in the x direction
Ny = (w/dy+1); %nodes in the y direction
T=zeros(Nx,Ny);
for m = (1:Nx) %node counter in x nodes
for n = (1:Ny) %node counter in y nodes
if n==1; %left side
T(m,n)=50;
elseif m==1 && n<Ny;%Heat Flux
T(m,n)=60;
elseif m>=2 && n==Ny && m<Nx; %insulated, right side
T(m,n)=70;
elseif n>=2 && n<=Ny-1 && m==Nx ; %insulated, bottom side
T(m,n)=80;
elseif m>=((.325*l)/dx)+1 && m<=((.675*l)/dx)+1 && n>=((.325*w)/dy)+1 && n<=
((.675*w)/dy)+1;
T(m,n) = 400;%center or steam
elseif m>1 && m<Nx && n>1 && n<Ny
T(m,n) = 90;
elseif m==1 && n==Ny;
**T(m,n)=T(m+2,n)/2;%**<-------------------this wont work properly****
elseif n==Nx && m==Ny;
T(m,n)= 110;
end
end
end
I am not sure why it will not choose the correct value and divide it by 2 rather it is saying the answer is 0 when it is suppose to be T(2,5) which is 70/2=35?
T =
50 60 60 60 0
50 90 90 90 70
50 90 400 90 70
50 90 90 90 70
50 80 80 80 110
any help would be appreciate it
Thanks
Upvotes: 0
Views: 175
Reputation: 8548
As I see it,
You are trying to access the element of the Matrix which is still initialized to zero. T Matrix is Zero(5,5).
And the condition : if m==1 && n==Ny
gets executed early in the iteration, at which time, m=1, and n=5 and T(2,5)=0.
So you have T(1,5)=0
So, I would suggest you debug the code and you check the content of Matrix M at the end of each iteration.
Upvotes: 1
Reputation: 24147
The reason T(1,5) is zero is that the indicated line of code is executed at a point when T(3,5) is still set to zero - it has not yet been filled with the value 70, and therefore T(1,5) is set to 0/2 = 0.
If you loop through the values of m
in reverse order, by modifying the code to be
for m = (Nx:-1:1)
you'll find that T(3,5) is now set to 70 before T(1,5) is set, and T(1,5) is now correctly set to 35.
Upvotes: 1