Temperature Change with Cream Addition

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
10 replies · 9K views
beho86
Messages
24
Reaction score
0
Matlab Help! Euler method

Use Euler's method:
cooling constant r = 0.2 , initial temperature of 86 C, and room temperature at 17 C. Choose your time step h and integrate to 5 minutes.
You can add some cream which will drop the temperature instantly by 5 C.

(a) Now, add the cream at the beginning. Plot your results. What is the temperature after 5 min?
(b) Add the cream at the end. Plot the results (preferably on the same figure). What is the temperature at the end?

I started the program in matlab:

Code:

Code:
clc
clear all

r=0.2;           
T=86 ;           
t=0   ;             
Ts=17;         
h=0.1  ;         
nsteps=300  ;    

for i=0:nsteps
   fprintf('%0.2f   %0.3f\n',t,T)    
   dT=-r*h*(T-Ts)  ;
   T=T+dT   ;     
   t=t+h;   
end
plot(t,T)
xlabel('Time (Sec)');
ylabel('Temperature (C)');
grid;

the problem is the plot only plots the last value in the loop, I tried to store it in an array, but I kept getting an error?
 
Physics news on Phys.org


You need to store your times and temps in two arrays. The error you are getting might be due to trying to store values into the 0-index position in the arrays. Matlab requires arrays to start at index 1.

If that's not it, then please provide the error message and we'll go from there.
 


I tried to create array, but I am not sure if I am doing it right?! (first week using matlab)r=0.2; %Cooling Constant
T=86 ; % Initial Temperature
t=0 ;
Ts=17; %Surrounding Temperature
h=0.1 ; %Step Size
nsteps=300 ;
T=zeros(0,300);
t=[];
for i=0:nsteps
fprintf('%0.2f %0.3f\n',t,T)
dT=-r*h*(T-Ts) ;
T(i)=T+dT ;
t(i)=t+h;
end
plot(t,T)

--------------------------------
? Subscript indices must either be real positive integers or logicals.

Error in ==> coffee at 15
T(i)=T+dT ;
 
This is a pretty decent tutorial - http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf. See section 2-15, Working with Matrices.
 


2-15 is for creating matrices, I went over it, I know how to create a matrix.
section 2.24 for arrays: If I know that t should go from 0 to 300 (5 min), in 0.1 increments.
should I say x=(0:0.1:300); but that makes an array not store the value.
I looked online for any examples for plotting inside a loop, but no luck.

I don't not quite understand it, I already tried before.
 


You can store 0 in an array; you just can't store anything at x(0). Have your for loop iterate from i = 1 to 300, instead of from i = 0 to 300.

Also, when you use zeros to initialize an array or matrix, the first parameter means how many rows, and the second means how many columns.

See if this works for you.

Code:
r=0.2; %Cooling Constant
Ti=86 ; % Initial Temperature 
%% t=0 ; 
Ts=17; %Surrounding Temperature
h=0.1 ; %Step Size
nsteps=300 ; 
T=zeros(1,300);
t = zeros(1, 300);
T(1) = Ti;
for i=1:nsteps
   fprintf('%0.2f   %0.3f\n',t(i),T(i))    
   dT=-r*h*(T(i)-Ts)  ;
   T(i) =T(i)+dT   ;     
   t(i)=t(i)+h;   
end
 


Thanks Mark,
I ran the code, but it returns:
0.00 86.00
:
:
0.00 0.00
 


Move the fprintf statement to the end of the loop, after T(i) and t(i) have been calculated.
Code:
for i=1:nsteps
   dT=-r*h*(T(i)-Ts)  ;
   T(i) =T(i)+dT   ;     
   t(i)=t(i)+h;   
   fprintf('%0.2f   %0.3f\n',t(i),T(i))    
end
 


Hello Mark,
Thank you again, It makes sense to move it after the T(i), and t(i).
Still not right, I ran it and here's the output:
0.1 0.34
:
0.1 0.34
although, we have T(1)=86 , it's still 0.34 , another question, if the question says 5 min, and 86 at 0 sec, should I also say t(1)=0
 


It's getting late here in the US West Coast, so I'm going to turn in. I've been tinkering with your code, and we're getting closer to correct results but aren't there yet.

What you need to do is "play computer" by going through your loop by hand (with pencil and paper) for two or three iterations and seeing what you get at each iteration. For example, the first output line above has the right time, but it should have produced a temperature of 84.62 by my calculation. In the next output line, the time should have incremented to 0.2, but apparently it didn't.

This code is supposed to be doing what you tell the computer to do, so see if what you think it will produce is the same as what it actually produces. It will probably be helpful to add extra fprintf statements inside the for loop so you can see what is calculated. After you get the bugs worked out, you can either delete the extra fprintf statements or comment them out.
 


I will keep working on it, and then I try to get back to the main question!
Thanks Mark