Temperature Change with Cream Addition

Click For Summary

Discussion Overview

The discussion revolves around implementing Euler's method in MATLAB to model temperature changes when cream is added to a hot liquid. Participants are addressing issues related to coding, specifically how to correctly store and plot temperature data over time, as well as how to handle the addition of cream at different times during the cooling process.

Discussion Character

  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant shares initial MATLAB code for simulating temperature changes using Euler's method, specifying parameters such as cooling constant, initial temperature, and room temperature.
  • Another participant suggests storing time and temperature values in arrays, noting that MATLAB arrays start at index 1, which may be causing errors in the original code.
  • A participant expresses confusion about correctly creating arrays and iterating through them, questioning the proper way to initialize and store values in MATLAB.
  • Further suggestions are made to adjust the loop to iterate from 1 to 300 instead of 0 to avoid indexing issues.
  • Participants discuss the placement of the output statement within the loop to ensure it displays the correct temperature and time values after calculations are made.
  • One participant mentions discrepancies in expected output values, prompting a suggestion to manually calculate iterations to identify errors in the code logic.
  • Another participant expresses intent to return to the main question after resolving coding issues.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the MATLAB code for proper array handling and output. However, there are unresolved issues regarding the correct implementation of the temperature calculations and the expected results, indicating ongoing uncertainty.

Contextual Notes

Participants have not reached a consensus on the correct output values or the final implementation of the code, and there are indications of missing assumptions regarding the temperature calculations and the effect of adding cream.

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
 
  • #10


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.
 
  • #11


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

Similar threads

Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K