# Thread: [SOLVED] Matlab: using law of cooling

1. ## [SOLVED] Matlab: using law of cooling

I am trying to work on my assignment, but I am having some trouble, the problem statement is:

Use Euler's method, and assume a cooling constant r = 0.2 , initial temperature of 86 C, and room temperature at 17 C. Choose your time
step h judiciously 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:
clc
clear all

r=0.2;           %Cooling Constant
T=86 ;           % Initial Temperature
t=0   ;
Ts=17;           %Surrounding Temperature
h=0.1  ;         %Step Size
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?

2. There are more efficient ways to write this function in matlab but since you already have the majority of the code written (and quite well structured to) maybe the following might help you out. I have only tested it in octave but I think it should be ok (sorry about the formating):

Code:
clc
clear all
r=0.2;           %Cooling Constant
##T=86 ;           % Initial Temperature
##t=0   ;
Ts=17;           %Surrounding Temperature
h=0.1  ;         %Step Size
nsteps=300  ;
T = zeros(nsteps,1);
t = zeros(nsteps,1);
T(1) = 86;
for i=2:nsteps
## fprintf('%0.2f   %0.3f\n',t,T)
dT=-r*h*(T(i-1)-Ts)  ;
T(i)=T(i-1)+dT   ;
t(i)=t(i-1)+h;
end
plot(t,T)
xlabel('Time (Sec)');
ylabel('Temperature (C)');
grid;
Elbarto

3. Hello Elbarto,
Thanks for your help,
I tried the code in matlab, not the plotting works, but the data doesn't make any sense!!!
it start like that :
0 86
:
:
300 Some temperature

I wanna make sure if I am doing it right in the first place, like should it be 300 ( for 5 min ) or I should keep it 300. because it may change the rest of the code.

4. Originally Posted by beho86
I am trying to work on my assignment, but I am having some trouble, the problem statement is:

Use Euler's method, and assume a cooling constant r = 0.2 , initial temperature of 86 C, and room temperature at 17 C. Choose your time
step h judiciously 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:
clc
clear all

r=0.2;           %Cooling Constant
T=86 ;           % Initial Temperature
t=0   ;
Ts=17;           %Surrounding Temperature
h=0.1  ;         %Step Size
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?
This is because you are not saving the intermediate values of time and temperature. You need to create buffer arrays to store T and t as you calculate them then plot the contents of the buffers at the end.

CB

5. I don't get it! if I have :
T(i)=T(i-1)+dT ;
t(i)=t(i-1)+h;
that stores it, but I am not sure why it's not working.

Thanks CaptainBlack,

6. Try this:

Code:
clc
clear all

r=0.2;           %Cooling Constant
T=86 ;           % Initial Temperature
t=0   ;
Ts=17;           %Surrounding Temperature
h=0.1  ;         %Step Size
nsteps=300  ;

Tbuff=[86];
tbuff=[0];

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

7. Hello CaptainBlack,
It worked perfect, for some reason it stops at 30.00 not 300!!!

8. you have 300 steps in the loop with a step size of 0.1 hence (300)x(0.1)=30

Either increase the step size to 1 or increase the number of steps to 3000 if you want the data up to 300 seconds.

When you say that the data doesn't make any sense, I think you are refering to the way it is printed to the command line. This is because the variables T and t are arrays in my example so you have to modify the fprintf statement like CB has done in his post.

Elbarto

9. That worked fine now! I will keep working on the rest of the problem.
Thank you guys for your help.

10. Originally Posted by beho86
Hello CaptainBlack,
It worked perfect, for some reason it stops at 30.00 not 300!!!
Because you have 300 steps of 0.1 second duration make the number of steps 3000 for 300s

CB

11. Hello CB,
I tried to play with the code, just to understand how it works.
Code:
r=0.2; T=86; t=0; Ts=17; h=0.1; nsteps=3000;
Tbuff=[86];
tbuff=[0];

for i=0:nsteps
fprintf('%0.1f   %0.3f\n',t,T)
dT=-r*h*(T-Ts);
T=T+dT;
t=t+h;
Tbuff=[T];
tbuff=[t];
end
%Tbuff(2)=Tbuff-5;
plot(tbuff,Tbuff)
I changed Tbuff=[Tbuff,T] to Tbuff=[T] , the data still works, but the plot only has the last point!

another question, i know by adding cream, temperature drops 5 C, and I have a matrix, I am not sure if I am doing it right. If I say Tbuff(2)=Tbuff-5 , doesn't that mean element number two in Tbuff will be subtracted from 5.

12. Originally Posted by beho86
Hello CB,
I tried to play with the code, just to understand how it works.
Code:
r=0.2; T=86; t=0; Ts=17; h=0.1; nsteps=3000;
Tbuff=[86];
tbuff=[0];

for i=0:nsteps
fprintf('%0.1f   %0.3f\n',t,T)
dT=-r*h*(T-Ts);
T=T+dT;
t=t+h;
Tbuff=[T];
tbuff=[t];
end
%Tbuff(2)=Tbuff-5;
plot(tbuff,Tbuff)
I changed Tbuff=[Tbuff,T] to Tbuff=[T] , the data still works, but the plot only has the last point!.
Why, did the code not work?

CB

13. You will still get the same output at the comand line but because you are only printing your data out and not storing it in an array (buffer) then when you go to plot your data you only have 1 point to plot.