Is My MATLAB Code for Heat Transfer Correct?

In summary, this conversation involves a heat transfer project and the determination of material and length for a 2D wall. The criteria for the project includes a net rate of heat loss between -1 and 1 from the southern side and a rate of heat transfer above 145 from the eastern side. The conversation also touches on equations for finding heat rates and plotting temperature in the wall. The code is also discussed, including inputs, geometry, and iterative loops. The conversation also includes a side note about warm temperatures in a room and the struggles of finding employment as an American engineer.
  • #1
MEAHH
10
0
Hi so i have a heat transfer project to determine the material (aka the thermal conductivity) and length of a 2d wall. It has to have between -1 and 1 net rate of heat loss from the southern side and above 145 rate of heat transfer from the eastern side. The wall is 20 cm high na the boundry conditions are given in the code...
#1 I cannot find a material/length combo that satisfys the Q rateconditions so i wonder if my code is wrong?I was really unsure of how to do the equations for the heat rate of the specified sides, how do you find the heat rate of a given side with nodal temps known, heat conduction and convection?
#2 I have to plot the temp in the wall as a function of x position for y=0,10 an 20cm...how would i do this...
Heres the code i wrote so far

%Heat Transfer Project
%Material chosen was:
%Length Chosen was:
%Inputs, Given Values in degrees C, m, and
Tn=100;
hn=20;
Te=20;
he=15;
Ts=70;
hs=30;
Tw=40;
hw=20;
H=0.2;
L=.5;
k=0.14;
I=21;
J=21;

%Geometry
deltax=L/(I-1);
deltay=L/(J-1);
x=[0:0.1:L];
y=[0:0.1:H];
A=L*H;
%Iterative loop
T=ones(I,J)*Tw;
Dum=1;
while Dum==1
Told=T;
%Numeric Equations
%Interior Control Volumes
for i=2,I-1;
for j=2,J-1;
T(i,j)=(k*deltay*T(i-1,j)+k*deltay*T(i+1,j)+k*deltax*T(i,j-1)-k*deltax*T(i,j+1))/(2*k*deltay+2*k*deltax);
end
end

%West & East sides
for j=2,J;
T(1,j)=(-hw*deltay*Tw-(k*deltax)/(2*deltay)*T(1,j+1)-(k*deltax)/(2*deltay)*T(1,j-1)-(k*deltay)/deltax*T(2,j))/(-hw*deltay-2*((k*deltax)/(2*deltay))-(k*deltax)/(deltay));
T(I,j)=(-he*deltay*Te-(k*deltax)/(2*deltay)*T(I,j+1)-(k*deltax)/(2*deltay)*T(I,j-1)-(k*deltay)/deltax*T(I-1,j))/(-he*deltay-2*((k*deltax)/(2*deltay))-(k*deltax)/(deltay));
end

%South & North
for i=2,I;
T(i,1)=(-hs*deltax*Ts-(k*deltay)/(2*deltax)*T(i+1,1)-(k*deltay)/(2*deltax)*T(i-1,1)-(k*deltax)/deltay*T(i,2))/(-hs*deltax-2*((k*deltay)/(2*deltax))-(k*deltay)/(deltax));
T(i,J)=(-hn*deltax*Tn-(k*deltay)/(2*deltax)*T(i+1,J)-(k*deltay)/(2*deltax)*T(i-1,J)-(k*deltax)/deltay*T(i,J-1))/(-hn*deltax-2*((k*deltay)/(2*deltax))-(k*deltay)/(deltax));
end

%Four Coners
T(1,1)=((-hw*Tw*deltay)/2-(hs*Ts*deltax)/2-(k*deltay)/(2*deltax)*T(2,1)-(k*deltax)/(2*deltay)*T(1,2))/(-(hw*deltay)/2-(hs*deltax)/2-(k*deltay)/(2*deltax)-(k*deltax)/(2*deltay));
T(I,1)=((-he*Te*deltay)/2-(hs*Ts*deltax)/2-(k*deltay)/(2*deltax)*T(I-1,1)-(k*deltax)/(2*deltay)*T(I,2))/(-(he*deltay)/2-(hs*deltax)/2-(k*deltay)/(2*deltax)-(k*deltax)/(2*deltay));
T(1,J)=((-hw*Tw*deltay)/2-(hn*Tn*deltax)/2-(k*deltay)/(2*deltax)*T(2,J)-(k*deltax)/(2*deltay)*T(1,J))/(-(hw*deltay)/2-(hn*deltax)/2-(k*deltay)/(2*deltax)-(k*deltax)/(2*deltay));
T(I,J)=((-he*Te*deltay)/2-(hn*Tn*deltax)/2-(k*deltay)/(2*deltax)*T(I-1,J)-(k*deltax)/(2*deltay)*T(I,J-1))/(-(he*deltay)/2-(hn*deltax)/2-(k*deltay)/(2*deltax)-(k*deltax)/(2*deltay));
%Check for convergence
Dum=0;
for i=1,I;
for j=1,J;
if (abs((Told(i,j)-T(i,j))/T(i,j))>0.0000001)
Dum=1;
end
end
end
end

Qe=he*deltay*[(T(I,1)-Te)/2+(T(I,2)-Te)+(T(I,3)-Te)+(T(I,4)-Te)+(T(I,5)-Te)+(T(I,6)-Te)+(T(I,7)-Te)+(T(I,8)-Te)+(T(I,9)-Te)+(T(I,10)-Te)+(T(I,11)-Te)+(T(I,12)-Te)+(T(I,13)-Te)+(T(I,14)-Te)+(T(I,15)-Te)+(T(I,16)-Te)+(T(I,17)-Te)+(T(I,19)-Te)+(T(I,20)-Te)+(.5*T(I,J)-Te)]
Qs=hs*deltax*[(T(1,1)-Ts)/2+(T(2,1)-Ts)+(T(3,1)-Ts)+(T(4,1)-Ts)+(T(5,1)-Ts)+(T(6,1)-Ts)+(T(7,1)-Ts)+(T(8,1)-Ts)+(T(9,1)-Ts)+(T(10,1)-Ts)+(T(11,1)-Ts)+(T(12,1)-Ts)+(T(13,1)-Ts)+(T(14,1)-Ts)+(T(15,1)-Ts)+(T(16,1)-Ts)+(T(17,1)-Ts)+(T(19,1)-Ts)+(T(20,1)-Ts)+(.5*T(I,1)-Ts)]
 
Physics news on Phys.org
  • #2
And what are your units?

On an aside, it's so warm in my room - it's like 28. And it's been warm for a while too - for almost 4 now. I tried to cool things down a bit by opening a gap in my window - a gap of 2.

Now - good luck deciphering that.

And then they wonder why nobody wants to hire american engineers anymore.
 
  • #3
I added [ code ] and [ /code ] tags (without extra spaces).
MEAHH said:
Hi so i have a heat transfer project to determine the material (aka the thermal conductivity) and length of a 2d wall. It has to have between -1 and 1 net rate of heat loss from the southern side and above 145 rate of heat transfer from the eastern side. The wall is 20 cm high na the boundry conditions are given in the code...
#1 I cannot find a material/length combo that satisfys the Q rateconditions so i wonder if my code is wrong?I was really unsure of how to do the equations for the heat rate of the specified sides, how do you find the heat rate of a given side with nodal temps known, heat conduction and convection?
#2 I have to plot the temp in the wall as a function of x position for y=0,10 an 20cm...how would i do this...
Heres the code i wrote so far
Code:
%Heat Transfer Project
%Material chosen was:
%Length Chosen was:
%Inputs, Given Values in degrees C, m, and 
Tn=100;
hn=20;
Te=20;
he=15;
Ts=70;
hs=30;
Tw=40;
hw=20;
H=0.2;
L=.5;
k=0.14;
I=21;
J=21;

%Geometry
deltax=L/(I-1);
deltay=L/(J-1);
x=[0:0.1:L];
y=[0:0.1:H];
A=L*H;
%Iterative loop
T=ones(I,J)*Tw;
Dum=1;
while Dum==1
    Told=T;
%Numeric Equations
%Interior Control Volumes
for i=2,I-1;
    for j=2,J-1;
        T(i,j)=(k*deltay*T(i-1,j)+k*deltay*T(i+1,j)+k*deltax*T(i,j-1)-k*deltax*T(i,j+1))/(2*k*deltay+2*k*deltax);
    end
end

%West & East sides
for j=2,J;
    T(1,j)=(-hw*deltay*Tw-(k*deltax)/(2*deltay)*T(1,j+1)-(k*deltax)/(2*deltay)*T(1,j-1)-(k*deltay)/deltax*T(2,j))/(-hw*deltay-2*((k*deltax)/(2*deltay))-(k*deltax)/(deltay));
    T(I,j)=(-he*deltay*Te-(k*deltax)/(2*deltay)*T(I,j+1)-(k*deltax)/(2*deltay)*T(I,j-1)-(k*deltay)/deltax*T(I-1,j))/(-he*deltay-2*((k*deltax)/(2*deltay))-(k*deltax)/(deltay));
end

%South & North
for i=2,I;
    T(i,1)=(-hs*deltax*Ts-(k*deltay)/(2*deltax)*T(i+1,1)-(k*deltay)/(2*deltax)*T(i-1,1)-(k*deltax)/deltay*T(i,2))/(-hs*deltax-2*((k*deltay)/(2*deltax))-(k*deltay)/(deltax));
    T(i,J)=(-hn*deltax*Tn-(k*deltay)/(2*deltax)*T(i+1,J)-(k*deltay)/(2*deltax)*T(i-1,J)-(k*deltax)/deltay*T(i,J-1))/(-hn*deltax-2*((k*deltay)/(2*deltax))-(k*deltay)/(deltax));
end

%Four Coners
T(1,1)=((-hw*Tw*deltay)/2-(hs*Ts*deltax)/2-(k*deltay)/(2*deltax)*T(2,1)-(k*deltax)/(2*deltay)*T(1,2))/(-(hw*deltay)/2-(hs*deltax)/2-(k*deltay)/(2*deltax)-(k*deltax)/(2*deltay));
T(I,1)=((-he*Te*deltay)/2-(hs*Ts*deltax)/2-(k*deltay)/(2*deltax)*T(I-1,1)-(k*deltax)/(2*deltay)*T(I,2))/(-(he*deltay)/2-(hs*deltax)/2-(k*deltay)/(2*deltax)-(k*deltax)/(2*deltay));
T(1,J)=((-hw*Tw*deltay)/2-(hn*Tn*deltax)/2-(k*deltay)/(2*deltax)*T(2,J)-(k*deltax)/(2*deltay)*T(1,J))/(-(hw*deltay)/2-(hn*deltax)/2-(k*deltay)/(2*deltax)-(k*deltax)/(2*deltay));
T(I,J)=((-he*Te*deltay)/2-(hn*Tn*deltax)/2-(k*deltay)/(2*deltax)*T(I-1,J)-(k*deltax)/(2*deltay)*T(I,J-1))/(-(he*deltay)/2-(hn*deltax)/2-(k*deltay)/(2*deltax)-(k*deltax)/(2*deltay));
%Check for convergence
Dum=0;
for i=1,I;
for j=1,J;
    if (abs((Told(i,j)-T(i,j))/T(i,j))>0.0000001)
        Dum=1;
    end
end
end
end

Qe=he*deltay*[(T(I,1)-Te)/2+(T(I,2)-Te)+(T(I,3)-Te)+(T(I,4)-Te)+(T(I,5)-Te)+(T(I,6)-Te)+(T(I,7)-Te)+(T(I,8)-Te)+(T(I,9)-Te)+(T(I,10)-Te)+(T(I,11)-Te)+(T(I,12)-Te)+(T(I,13)-Te)+(T(I,14)-Te)+(T(I,15)-Te)+(T(I,16)-Te)+(T(I,17)-Te)+(T(I,19)-Te)+(T(I,20)-Te)+(.5*T(I,J)-Te)]
Qs=hs*deltax*[(T(1,1)-Ts)/2+(T(2,1)-Ts)+(T(3,1)-Ts)+(T(4,1)-Ts)+(T(5,1)-Ts)+(T(6,1)-Ts)+(T(7,1)-Ts)+(T(8,1)-Ts)+(T(9,1)-Ts)+(T(10,1)-Ts)+(T(11,1)-Ts)+(T(12,1)-Ts)+(T(13,1)-Ts)+(T(14,1)-Ts)+(T(15,1)-Ts)+(T(16,1)-Ts)+(T(17,1)-Ts)+(T(19,1)-Ts)+(T(20,1)-Ts)+(.5*T(I,1)-Ts)]
 

FAQ: Is My MATLAB Code for Heat Transfer Correct?

1. What is MATLAB code and how is it used in heat transfer analysis?

MATLAB (short for Matrix Laboratory) is a high-level programming language and interactive environment used for numerical computation, data visualization, and algorithm development. In heat transfer analysis, MATLAB code is used to perform calculations and simulations to analyze the transfer of heat between different objects or materials.

2. How do I write a MATLAB code for heat transfer analysis?

The first step in writing a MATLAB code for heat transfer analysis is to define the problem and understand the underlying principles of heat transfer. Then, you can use built-in functions and commands in MATLAB to create a numerical model and solve the equations. It is also helpful to break down the problem into smaller steps and test each step to ensure accuracy.

3. Can MATLAB code be used for both 1D and 2D heat transfer analysis?

Yes, MATLAB code can be used for both 1D and 2D heat transfer analysis. However, the approach and equations used may differ slightly for each case. For 1D heat transfer, the temperature variation is considered only in one direction, while for 2D heat transfer, it is considered in two directions.

4. Is it necessary to have a background in programming to use MATLAB for heat transfer analysis?

No, it is not necessary to have a background in programming to use MATLAB for heat transfer analysis. While some knowledge of programming can be helpful, MATLAB has a user-friendly interface and provides built-in functions and commands to perform complex calculations without prior programming experience. It is important to have a basic understanding of heat transfer principles to effectively use MATLAB for analysis.

5. Can I visualize heat transfer results using MATLAB code?

Yes, one of the advantages of using MATLAB for heat transfer analysis is the ability to visualize results through data plotting and graphing functions. This allows for a better understanding of the heat transfer process and makes it easier to interpret and analyze the results. Additionally, MATLAB also offers options for creating 3D visualizations of heat transfer data.

Similar threads

Replies
10
Views
2K
Replies
1
Views
1K
Replies
6
Views
2K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
2
Views
1K
Replies
1
Views
1K
Back
Top