Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with code for heat transfer between two solid objects

Status
Not open for further replies.

TADmech

Mechanical
Jan 21, 2005
2
I am new to MATLAB and am having a little trouble. I have a relatively simple problem where I need to calculate the final temp of two contacting objects after approx 60 seconds, where T1 > T2. The code I have so far is very basic and does not allow input. I am trying to get it to work with to given values before I allow external, real-world input (where the time, initial temps, and heat capcities will be user specific).

I have listed the code that I have below hoping someone can provide a few pointers.

alpha = 1;
t = 0; % time (seconds)
delta_t = 1; % 1 second interval
Max_t = 60; % maximum time

T1 = 100; % deg C
m1 = 2; % kg
c1 = 5; % heat capacity

T2 = 20; % deg C
m2 = 4; % kg
c2 = 4; % heat capacity

% Values of expression won't change - calculate prior to while loop
alpha_1 = ((alpha)*(delta_t))/(m1 * c1);
alpha_2 = ((alpha)*(delta_t))/(m2 * c2);

while (t < Max_t);
t = t + delta_t
T1 = (1 - alpha_1).*(T1) + (1-alpha_1).*(T2)
T2 = (alpha_2).*(T1) + (1-alpha_2).*(T2)
end

plot (T1,t,T2,t,'--')

Thank you any help that you can provide in clearing up my MATLAB ignorance :).
 
Replies continue below

Recommended for you

You are overwriting the values of T1 and T2 each time you go round the loop. You need to store these values in a vector. You also have a ";" at the end of the while statement which should not be there.

Can you clarify exactly what you are trying to do? I would suggest that for a simple calculation like this you could use some other software with which you are more familiar (C or Basic or even Excel for example) rather than trying to learn Matlab!

M

--
Dr Michael F Platten
 
Thanks for your help. Unfortunately, it is an in-house initiative to use MATLAB. I shouldn't say unfortunately, it is a powerful program and it would benefit me to learn it. Just a little tough under these circumstances.

I have not found any documentation on storing a vector in a while loop. Most of my attempts have lead to the values still being overwritten. Can you point me in the right direction?

Thanks again.
 
I suggest you do some reading, eg "Mastering Matlab".

M

--
Dr Michael F Platten
 
I'd create a vector for t using the statement, then iterate through it with an indexed for loop, e.g.,

alpha = 1;

delta_t = 1; % 1 second interval
Max_t = 60; % maximum time
t = 0:delta_t:Max_t;

T1 = zeros(1,length(t));
T2 = zeros(1,length(t));

T1(1,1) = 100; % deg C
m1 = 2; % kg
c1 = 5; % heat capacity

T2(1,1) = 20; % deg C
m2 = 4; % kg
c2 = 4; % heat capacity

% Values of expression won't change - calculate prior to loop
alpha_1 = ((alpha)*(delta_t))/(m1 * c1);
alpha_2 = ((alpha)*(delta_t))/(m2 * c2);

for n = 1:length(t)
T1(1,n+1) = (1 - alpha_1)*(T1(1,n)) + (1-alpha_1)*(T2(1,n));
T2(1,n+1) = (alpha_2)*(T1(1,n)) + (1-alpha_2).*(T2(1,n));
end

I don't have MATLAB on the computer I'm using, but something like this should work. You may have to fiddle around with it a little. Oh, and your plot statement argument order seems backwards: plot(t,T1,T,T2,'--').

xnuke
"Do you think you used enough dynamite there, Butch?"
Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor