What am I doing wrong in calculating temperature change in Matlab?

Click For Summary
SUMMARY

The forum discussion centers on calculating temperature change in MATLAB using a difference equation. The user initially encounters errors due to incorrect syntax and logic in their script, particularly with the multiplication operator and the structure of the loops. The correct approach involves using two separate loops to account for the temperature changes during the two distinct 15-minute intervals, first at 20 degrees Celsius and then at 12 degrees Celsius. The user ultimately needs to revise their code to properly implement these changes for accurate temperature calculations.

PREREQUISITES
  • Understanding of MATLAB syntax and programming structures
  • Familiarity with difference equations in thermal dynamics
  • Knowledge of temperature change calculations
  • Experience with loops and array indexing in MATLAB
NEXT STEPS
  • Review MATLAB array indexing and ensure proper syntax for operations
  • Learn about implementing multiple loops for sequential calculations in MATLAB
  • Study the concept of thermal equilibrium and its mathematical modeling
  • Explore MATLAB debugging techniques to identify and resolve script errors
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those working on thermal dynamics simulations, students learning programming concepts, and anyone troubleshooting similar coding issues in MATLAB.

roam
Messages
1,265
Reaction score
12
I'm very confused on about solving the following question using Matlab:

A drink cools according to the difference equation:

Tn=Tn-1-0.02(Tn-1-Ta)

Where Tn is the temprature after n minutes. When you pour the drink it's 80 degrees. After you have poured it you leave it for 15 minutes in the kitchen where the air temprature is 20 degree, then you take it outside where the temprature is 12 degrees. Calculate the temprature of the drink afte it has been outside for 15 minutes.

This is my script:

Code:
k=0.02;
T_a=12;
S(1)=80;
S_a=20
for n=1:15
S(n+1)=S(n)-k(S(n)-S_a);
T(1)=S(16);
T(n+1)=T(n)-k(T(n)-T_a);
end
T(16)

I'm not quite sure what I've done wrong here but I keep getting errors like:

S_a =

20

? Attempted to access k(60); index out of bounds because numel(k)=1.

Error in ==> temp at 6
S(n+1)=S(n)-k(S(n)-S_a);

Any guidance is greatly appreciated.
 
Physics news on Phys.org
I'm no expert at MATLAB, but it looks like there are two problems. First, it doesn't seem to be parsing k(S(n)-S_q) as a multiplication of k * whatever. Looks like it thinks it's an array access?

And I don't see how you have two separate loops of 15 minutes each -- seems like you need to to the first 15 minutes, and then do another 15 minutes with the different delta-Temperature...
 
berkeman said:
I'm no expert at MATLAB, but it looks like there are two problems. First, it doesn't seem to be parsing k(S(n)-S_q) as a multiplication of k * whatever. Looks like it thinks it's an array access?

And I don't see how you have two separate loops of 15 minutes each -- seems like you need to to the first 15 minutes, and then do another 15 minutes with the different delta-Temperature...

Ok, I added the multiplication sign to my code:

Code:
k=0.02;
T_a=12;
S(1)=80;
S_a=20
for n=1:15
S(n+1)=S(n)-k*(S(n)-S_a);
T(1)=S(16);
T(n+1)=T(n)-k*(T(n)-T_a);
end
T(16)

And my code is still not working.:frown:

I think I need to have two separate loops for each 15 minutes because in the first 15 minutes the drink is placed in a room with a temperature of 20° and in the second 15 minutes it's placed outside where the temperature is 12°...
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
8K
Replies
0
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K