Heat Transfer: Solve using "relaxation" method while storing residuals and temperatures in arrays

5 views (last 30 days)
Hello all,
I must write a MATLAB code for my thermal design (Mechanical Engineering) course which must solve four nodal temperatures using "relaxation method". I have written a code according to my professor's guidelines in his lecture, but I cannot find a way to get MATLAB to perform multiple iterations without being thrown into an infinite loop (one in which there are no new calculations taking place; MATLAB will simply count iterations to infinity). I have gotten the first iteration to recalculate T(1) from 300 to 325, but the code stops there. I tried a few different methods using a while or for loop, but every attempt has resulted in an infinite loop. Disclaimer: I am not good at MATLAB whatsoever; please be detailed with any explaination you may offer.
Notes:
-Initial temperature guess must be 300
-Residual tolerance must be 0.1
-Dimensions of element are not specified; we are tasked to solve for three generic interior nodes, and one central-height node on the right wall (which is insulated)
-"Relaxation method" must be used
Thank you in advance.
Code:
close all
clear all
clc
%Set temperature of surfaces
Tt=500; %Top surface temperature
Tl=400; %Left surface temperature
Tb=100; %Bottom surface temperature
%Tr=n/a; %Right surface temperature (insulated)
%Define array for temperatures A through D
T(1,1:4)=300;
%Define arrray for residuals A through D
R(1,1:4)=1;
%Equations for residuals
R(1) = abs(Tt + Tl + Tb + T(2) - 4*T(1)); %Equation for residual A
R(2) = abs(Tt + T(1) + Tb + T(3) - 4*T(2)); %Equation for residual B
R(3) = abs(Tt + T(2) + Tb + T(4) - 4*T(3)); %Equation for residual C
R(4) = abs(Tt + Tb + 2*T(3) - 4*T(4)); %Equation for residual D
Rt = 0.1; %Tolerance for residual
It = 0; %Initial iteration
if max(R) > Rt
It = It + 1; %Iteration series
disp(It);
if (find(max(R)) == 1)
T(1) = (Tt + Tl + Tb + T(2))/4
if (find(max(R)) == 2)
T(2) = (Tt + T(1) + Tb + T(3))/4
if (find(max(R)) == 3)
T(3) = (Tt + T(2) + Tb + T(4))/4
if (find(max(R)) == 4)
T(4) = (Tt + Tb + 2*T(3))/4
disp(max(R));
print(T)
else max(R) <= Rt
print(T)
end
end
end
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 21 Feb 2023
if (find(max(R)) == 1)
R is a vector. max® is a scalar value. find() of a scalar will be 1 if the scalar is non-nan and non-zero. And that is an odd test if that is your intention.
Your if are all nested. If the first find test is true then since you do not change R before the second test the second must be false.
If you are trying to find the index of the maximum then use the second output of max()
  1 Comment
Julian Saninocencio
Julian Saninocencio on 21 Feb 2023
Walter,
Thank you for your input. Your comments lead me in the right direction, and my code is now working properly.
Others, see adjusted code below for reference:
clc
close all
clear all
clc
%Set temperature of surfaces
Tt=500; %Top surface temperature
Tl=400; %Left surface temperature
Tb=100; %Bottom surface temperature
%Tr=n/a; %Right surface temperature (insulated)
%Define array for temperatures A through D
T(1,1:4)=300;
%Define arrray for residuals A through D
R(1,1:4)=1;
%Equations for residuals
R(1) = abs(Tt + Tl + Tb + T(2) - 4*T(1)); %Equation for residual A
R(2) = abs(Tt + T(1) + Tb + T(3) - 4*T(2)); %Equation for residual B
R(3) = abs(Tt + T(2) + Tb + T(4) - 4*T(3)); %Equation for residual C
R(4) = abs(Tt + Tb + 2*T(3) - 4*T(4)); %Equation for residual D
Rt = 0.1; %Tolerance for residual
It = 0; %Initial iteration
while max(R) > Rt
It = It + 1; %Iteration series
It
if (R(1) > Rt)
T(1) = (Tt + Tl + Tb + T(2))/4;
R(1) = abs(Tt + Tl + Tb + T(2) - 4*T(1));
R(2) = abs(Tt + T(1) + Tb + T(3) - 4*T(2));
R(3) = abs(Tt + T(2) + Tb + T(4) - 4*T(3));
R(4) = abs(Tt + Tb + 2*T(3) - 4*T(4));
end
if (R(2) > Rt)
T(2) = (Tt + T(1) + Tb + T(3))/4;
R(1) = abs(Tt + Tl + Tb + T(2) - 4*T(1));
R(2) = abs(Tt + T(1) + Tb + T(3) - 4*T(2));
R(3) = abs(Tt + T(2) + Tb + T(4) - 4*T(3));
R(4) = abs(Tt + Tb + 2*T(3) - 4*T(4));
end
if (R(3) > Rt)
T(3) = (Tt + T(2) + Tb + T(4))/4;
R(1) = abs(Tt + Tl + Tb + T(2) - 4*T(1));
R(2) = abs(Tt + T(1) + Tb + T(3) - 4*T(2));
R(3) = abs(Tt + T(2) + Tb + T(4) - 4*T(3));
R(4) = abs(Tt + Tb + 2*T(3) - 4*T(4));
end
if (R(4) > Rt)
T(4) = (Tt + Tb + 2*T(3))/4;
R(1) = abs(Tt + Tl + Tb + T(2) - 4*T(1));
R(2) = abs(Tt + T(1) + Tb + T(3) - 4*T(2));
R(3) = abs(Tt + T(2) + Tb + T(4) - 4*T(3));
R(4) = abs(Tt + Tb + 2*T(3) - 4*T(4));
end
if max(R) <= Rt
T
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Thermal Analysis in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!