Temperature Change with Cream Addition

In summary, the program calculates the temperature after five minutes by using Euler's method. The program starts by calculating the cooling constant, the initial temperature, and the surrounding temperature. The program then sets up an for loop to calculate the temperature every 0.1 seconds. The program also stores the results in an array. The program tries to calculate the temperature by playing computer, but it is getting late so I will turn in for now.
  • #1
beho86
25
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
  • #2


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


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 ;
 
  • #4
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.
 
  • #5


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


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


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


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


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
 

1. What is the Euler method in Matlab?

The Euler method is a numerical method used in Matlab to approximate the solution of a first-order differential equation. It is based on the idea of approximating the curve of the solution by a series of straight lines.

2. How do I use the Euler method in Matlab?

To use the Euler method in Matlab, you will need to define your differential equation, initial conditions, and step size. Then, you can use a loop to iterate through the steps and calculate the approximate solution at each step using the Euler formula.

3. What are the advantages of using the Euler method in Matlab?

One advantage of using the Euler method in Matlab is that it is relatively simple to implement. It also provides a good approximation of the solution, especially when the step size is small.

4. Are there any limitations of the Euler method in Matlab?

Yes, there are some limitations to the Euler method in Matlab. It can be less accurate than other numerical methods, such as the Runge-Kutta method, and it may struggle with stiff differential equations that have rapidly changing solutions.

5. Can I use the Euler method in Matlab for higher-order differential equations?

Yes, the Euler method can be extended to higher-order differential equations by converting them into a system of first-order equations. This can be done using the ODE45 function in Matlab, which implements the Runge-Kutta method.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
742
  • Engineering and Comp Sci Homework Help
Replies
1
Views
899
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
990
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
951
Back
Top