Results 1 to 13 of 13

Math Help - [SOLVED] Matlab: using law of cooling

  1. #1
    Newbie
    Joined
    Feb 2010
    Posts
    12

    [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?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Feb 2010
    Posts
    12
    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.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by beho86 View Post
    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
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Feb 2010
    Posts
    12
    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,
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    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;
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Feb 2010
    Posts
    12
    Hello CaptainBlack,
    It worked perfect, for some reason it stops at 30.00 not 300!!!
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Newbie
    Joined
    Feb 2010
    Posts
    12
    That worked fine now! I will keep working on the rest of the problem.
    Thank you guys for your help.
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by beho86 View Post
    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
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Newbie
    Joined
    Feb 2010
    Posts
    12

    Exclamation

    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.
    Follow Math Help Forum on Facebook and Google+

  12. #12
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by beho86 View Post
    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
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: March 28th 2010, 10:24 AM
  2. [SOLVED] Help w/ Matlab
    Posted in the Math Software Forum
    Replies: 33
    Last Post: March 3rd 2010, 04:49 PM
  3. [SOLVED] Matlab Help
    Posted in the Math Software Forum
    Replies: 2
    Last Post: January 28th 2010, 04:36 AM
  4. [SOLVED] Matlab plotting help:peicewise with loops
    Posted in the Math Software Forum
    Replies: 4
    Last Post: October 20th 2009, 10:19 PM
  5. [SOLVED] Newton's Law of Cooling
    Posted in the Calculus Forum
    Replies: 1
    Last Post: September 20th 2007, 06:49 AM

Search Tags


/mathhelpforum @mathhelpforum