Need help with loop for a heat transfer question

5 views (last 30 days)
Hello, I worked on a code to calculate the temperature of the slab from a given initial temperature and a given slab thickness after some time t. Now lets say that the slab thickness wasn't given, and the question is asking to calculate the minimum thickness of a slab to satisfy a final temperature after some time t, how can i adjust my code so that it calculates the minimum thickness? im new to matlab so any help would be appreciated. Please let me know if theres something that is not clear about the code, thanks in advance for your help.
%this code calculates the temperature of the slab surface after time t
clear
clc
close
L=0.2; %slab thickness
Ti=600; %initial surface temp
Tinf=22; %surrounding temp
h=240; %convection coefficient
k=80.2; %thermal conductivity
alpha=3.31e-5;
dx=0.01;
dt=1.5;
Fo=(alpha*dt)/dx^2; %forier number
Bii=(h*dx)/k;
Bio=(h*dx)/k;
t=1800;
x=[0:dx:L];
n=length(x);
t=[0:dt:t];
T = zeros(numel(t), numel(x)); %temperature of the slab at time t (rows) and position x (columns)
T(1, :) = Ti; %temperature at t = t0;
for k = 2:numel(t)
T(k, 1) = T(k-1, 1)*(1-2*Fo-2*Bio*Fo)+2*Fo*T(k-1, 2)+2*Bio*Fo*Tinf; %node1
for i = 2:numel(x)-1
T(k, i) = T(k-1, i)*(1-2*Fo)+Fo*(T(k-1, i+1)+T(k-1, i-1)); %interior nodes
end
T(k, n) = T(k-1, n)*(1-2*Fo-2*Bii*Fo)+2*Fo*T(k-1, n-1)+2*Bii*Fo*Tinf; %exterior node
end
T(k,n)
%plot temperature for all t at a different x
plot(t, T(:, end));
  3 Comments

Sign in to comment.

Accepted Answer

Bob Thompson
Bob Thompson on 20 Jul 2018
Edited: Bob Thompson on 20 Jul 2018
Since your code already calculates the temperature based on a thickness I think the most minimal change would be to turn L into a variable and "guess" your way to the proper L.
TIdeal = input('Enter the ideal final temperature: ');
T = []; % Initialization
dL = 0.5; % Initialization
while T(end,end)~= TIdeal;
L = 1.0; % Just a generic starting value
% Rest of your code here
......
% End of existing code
if T(end,end)>TIdeal % Temperature is high, too thin
L = L+dL; % Increase thickness
dL = dL/2; % Reduce size of dL
elseif T(end,end)<TIdeal % Temperature is low, too thick
L = L-dL; % Reduce thickness
dL = dL/2; % Reduce size of dL
else % T = TIdeal or something is broken
break
end % Temp check if
end % While loop
There are a number of things that could go wrong with this, but it's a first cut. You may need to change how dL changes, or may need to change the starting value of L as each loop reduces the amount of change possible. Also, you will probably need to adjust the T you are specifically looking at since I assumed you wanted final temp at the furthest thickness.
As a side note, I would strongly advise against using k as your index, since it is also defined as thermal conductivity, and you are currently erasing your thermal conductivity variable in order to index the loop.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!