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

In summary: 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°...
  • #1
roam
1,271
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
  • #2
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...
 
  • #3
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°...
 

1. How do I plot temperature change over time in Matlab?

To plot temperature change over time in Matlab, you can use the "plot" function and input your time and temperature data as the x and y variables, respectively. You can also customize the plot by adding a title, labels for the x and y axes, and changing the color or style of the line.

2. Can I calculate the rate of temperature change using Matlab?

Yes, you can calculate the rate of temperature change using Matlab by taking the derivative of the temperature data over time. The "diff" function can be used to calculate the difference between consecutive temperature values, and then dividing this difference by the time interval between each data point will give you the rate of change.

3. How do I import temperature data from an external file in Matlab?

To import temperature data from an external file in Matlab, you can use the "importdata" function. This function allows you to specify the file name and location, as well as the format of the data, and will create a data structure that you can then use for plotting and analysis.

4. Can I create a temperature change animation in Matlab?

Yes, you can create a temperature change animation in Matlab by using the "animatedline" function. This function allows you to continuously add data points to a plot, creating a real-time animation of the temperature change over time. You can also customize the animation by adding title, axes labels, and changing the color or style of the line.

5. How can I perform statistical analysis on temperature change data in Matlab?

Matlab has various built-in functions for performing statistical analysis on temperature change data, including "mean", "std", and "corrcoef". You can also use the "fitlm" function to perform linear regression analysis on your data, which can help you determine the relationship between temperature and time. Additionally, there are many user-created toolboxes and functions available for more advanced statistical analysis in Matlab.

Similar threads

  • Advanced Physics Homework Help
Replies
1
Views
653
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Replies
6
Views
2K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
1
Views
1K
Replies
19
Views
1K
Back
Top