Advertisement
ksoltan

Coffee and Cream (Exercise 7)

Oct 25th, 2015
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 7.16 KB | None | 0 0
  1. function coffee_model()
  2. % Modeling of coffee cooling in cermaic mug. Cream is added at different
  3. % times during the cooling process.
  4. % Assumption: When cream is added, completely assimilates into the coffee
  5. % and becomes one mass, with the same specific heat of the coffee.
  6.     %% Coffee mug specifications
  7.     radius = 0.04; %m
  8.     height = 0.10; %m
  9.     V = pi * radius^2 * height; % cylindrical cup of coffee
  10.     k = 1.5; % W/m/K
  11.     A = height * 2 * pi * radius + pi * radius ^ 2; % sides and bottom
  12.     d = 0.007; % m
  13.     m_coffee = V * 1000; % 1000kg/m^3 density of coffee
  14.     m_mix = m_coffee;
  15.     m_cream = 1e-4 * 1000; % 100ml = 1e-4m^3 cream, 1000kg/m^3 density
  16.     c = 4186; % J/kg/K
  17.     h = 100; % W / m^2 / K
  18.    
  19.     %% Initial Temperatures
  20.     Tinit = 370; % K
  21.     Troom = 290; % K
  22.     Tcream = 275; % K
  23.     Tdrinkable = 320; % K
  24.    
  25.     %% Times
  26.     time_init = 0;
  27.     time_final = 60 * 60;
  28.    
  29.    %% Main function call
  30.    figure(4)
  31.    plot_time_drinkable()
  32.    get_time_to_add_cream()
  33.     %% Describes instantaneous change of the internal energy of the coffee
  34.     function res = coffee_U_change(t, U)
  35.         % Change in coffee temperature is caused by conduction of heat
  36.         % through the ceramic mug
  37.         temp_coffee = U / m_mix / c;
  38.         conduction = k * A / d * (Troom - temp_coffee);
  39.         res = conduction;
  40.     end
  41.     %% Returns the internal energy of the coffee cream mixture after
  42.     % cream is added
  43.     function final_energy = energy_coffee_cream(U)
  44.         % Changes the mass of the coffee to the mass of coffee cream
  45.         % mixture
  46.         final_temp = (m_coffee * (U / m_coffee / c) + m_cream * Tcream)...
  47.             / (m_coffee + m_cream);
  48.         m_mix = m_coffee + m_cream;
  49.         final_energy = final_temp * m_mix * c;
  50.     end
  51.  
  52.     %% Function that computes the time it takes for the coffee to reach drinkable
  53.     % temperatures
  54.     function res = time_to_drinkable(t_cream)
  55.         % Set a trigger event for the ode function to stop at
  56.         options = odeset('Events', @event_function);
  57.         m_mix = m_coffee;
  58.         % TE: time at which event happens
  59.         % VE: value of output at which event happens
  60.         [time_1, uu_1, TE_1, VE_1] = ode45(@coffee_U_change, [time_init, t_cream], ...
  61.            Tinit * m_coffee * c, options);
  62.         [time_2, uu_2, TE_2, VE_2] = ode45(@coffee_U_change, [t_cream, time_final], ...
  63.           energy_coffee_cream(uu_1(end)), options);
  64.         % Check which trigger actually stopped because it reached drinkable
  65.         % temperature
  66.         U_drinkable_before_cream = Tdrinkable * c * m_coffee;
  67.         U_drinkable_after_cream = Tdrinkable * c * m_mix;
  68.         % If the coffee has reached drinkable temperature during the
  69.         % addition of the cream, the time it reaches drinkable temp will be
  70.         % right after cream is added
  71.         res = time_2(1);
  72.         % Check to see if the trigger event is reasonably close to the
  73.         % drinkable internal energy of coffee
  74.         if abs(VE_1 - U_drinkable_before_cream) < 0.0001
  75.             % Hits the drinkable temperature before cream is added
  76.             res = TE_1;
  77.         end
  78.         if abs(VE_2 - U_drinkable_after_cream) < 0.0001
  79.             % Hits the drinkable temperature after the cream is added
  80.             res = TE_2;
  81.         end
  82.        
  83.         %% Trigger stop when the temperature of the coffee is at the drinkable temp
  84.         function [value, isterminal, direction] = event_function(t, U)
  85.            value = U / m_mix / c - Tdrinkable; % When value = 0, event is triggered
  86.            isterminal = 1; % stop function as soon as this event is reached
  87.            direction = 0; % At any zero (when value = 0) consider this event, not
  88.                           % only when the function is either increasing (1) or
  89.                           % decreasing (-1)
  90.         end
  91.     end
  92.  
  93.     %% Plot time it takes for the coffee to become drinkable vs time at which
  94.     % the cream was added
  95.     % At some point, the cream is added after the coffee gets to drinking
  96.     % temp by itself.
  97.     function plot_time_drinkable()
  98.         % If you add the cream after 330 s, the coffee is already at or
  99.         % below the drinkable temperature
  100.         t_cream = time_init + 1 : 1 : time_final - 1;
  101.         Time = zeros(1, length(t_cream));
  102.         for i = 1 : length(t_cream)
  103.            Time(i) = time_to_drinkable(t_cream(i));
  104.         end
  105.         plot(t_cream, Time, 'b*')
  106.         xlabel('Time at which cream is added (s)')
  107.         ylabel('Time to cool to drinkable temp (s)')
  108.         title('Cream Adding Time vs Coffee Cooling to Drinkable Temperature Time')
  109.     end
  110.    
  111.     %% Find time at which ading cream will generate fastest cooling process
  112.     % Res is the time at which it is most optimal to add cream aka when the
  113.     % cooling process is at a minimum
  114.     function res = get_time_to_add_cream()
  115.         [x, fval] = fminsearch(@time_to_drinkable, time_init + 1);
  116.         res = x;        
  117.     end
  118.  
  119. %% Plot the time curve of the coffee cooling with adding cream at different
  120.    % times during the process
  121.    function plot_adding_cream()
  122.         % If the time_cream is changed, make sure that T, Temp, extra_t and
  123.         % extra_temp's num of rows is enough.
  124.        time_cream = time_init + 1; % : 100 : time_final - 1;
  125.        T = zeros(1000, length(time_cream));
  126.        Temp = zeros(1000, length(time_cream));
  127.        % Because ode45 does not use a constant change in time, must fill in the
  128.        % matrix with extra points to fulfill dimensional equivalence
  129.        % Because each coffee starts at t=0 at Tinit, this is the point that is
  130.        % duplicated in the column vector for each different cream-adding
  131.        % simulation
  132.        extra_t = zeros(1000, 1);
  133.        extra_temp = Tinit * ones(1000, 1);
  134.        hold on
  135.        for i = 1 : length(time_cream)
  136.           % set mass of mix to be coffee; it will be changed to mass of mix
  137.           % later when cream is added. Must change back for new cream adding
  138.           m_mix = m_coffee;
  139.           t_cream = time_cream(i);
  140.           % Calculate internal energy of the coffee before cream addition
  141.           [t_1, u_1] = ode45(@coffee_U_change, [time_init, t_cream], ...
  142.                Tinit * m_coffee * c);
  143.            % Calculate cooling process after cream is added
  144.           [t_2, u_2] = ode45(@coffee_U_change, [t_cream, time_final], ...
  145.               energy_coffee_cream(u_1(end)));
  146.           % Combine time series into one column vector for each cream addition
  147.           T(:, i) = [extra_t(1 : length(T) - length([t_1; t_2])); t_1; t_2];
  148.           Temp(:, i) = [extra_temp(1 : length(T) - length([u_1; u_2]));...
  149.               u_1 / m_coffee / c; u_2 / m_mix / c];
  150.        end
  151.  
  152.        % Plot each simulation on one graph
  153.        % Possible to graph on different figures, but make sure temp_cream
  154.        % is a reasonable amount of figures
  155.        for i = 1 : length(T(1,:))
  156.           hold on
  157.           plot(T(:, i), Temp(:, i));
  158.           axis([time_init, time_final, Troom - 5, Tinit])
  159.        title('Temperature of Coffee in Ceramic Mug Over Time')
  160.        xlabel('Time (seconds)')
  161.        ylabel('Temperature (Kelvin)')
  162.        end
  163.     end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement