You are on page 1of 4

C09 - 1D Transient Heat Transfer Fancy Plotting

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Trans_ID.m - script to demostrate a transient one dimesnional heat % transfer problem with convection on one side % Note: Script includes some fancy plotting along with normal plotting. % Fancy plotting subdivides a specified color map and also automatically % pulls out the time stamp for the plotted lines. % Disclaimer: % This is an example script to provide insight into programming; it is not to % be used for life safety calculations or any other formal calculations for % industry or academia. Under no circumstances will the author be liable in % any way for this scripts use, including but not limited to, any errors or % omissions in any content, or for any loss or damage of any kind incurred % as a result of the use of any content posted, e-mailed, transmitted or % otherwise made available through the use of this script.

%%%%%%%%%%%%%%%%%%%%%%%%%%%% inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %L -m Length of 1D space %n -"" Number of nodes % del_i -m Spacing of nodes % % T_hg -K Temperature of Hot gas % T_cg -K Temperature of Cold gas % T_2 -K Temperature of Cold side % t_2 -Seconds Stopping time %k -W/(m K) Thermal conductivity - conduction ciefficient % rho -kg.m^3 Density % Cp -J/kg K Spefic heat constant pressure % alpha -m^2/s Diffusivity %h -W/m^2 K Convection coefficient % pt_spc -"" Number of iterations betweem lines plotted % del_t -seconds Change in time each iteration % t(1) -seconds Initial time

%%%%%%%%%%%%%%%%%%%%%%%%%%%% Outputs %%%%%%%%%%%%%%%%%%%%%%%%%%%% % plots the transient temperature profiles vs. position % Prints the number of iterations used to command window

%%%%%%%%%%%%%%%%%%%%%%%%%%%% Example %%%%%%%%%%%%%%%%%%%%%%%%%%%% %L = 0.1 ; %m Length of 1D space %n = 50 ; %"" Number of nodes % del_i = L/(n-1) ; %m Spacing of nodes % % T_hg = 500 ; %K Temperature of Hot gas % T_cg = 250 ; %K Temperature of Cold gas % T_2 = 293 ; %K Temperature of Cold side % t_2 = 100 ; %Seconds Stopping time % % % properties for steel %k = 45.8 ; %W/(m K) Thermal conductivity - conduction ciefficient % rho = 7850 ; %kg.m^3 Density % Cp = 640 ; %J/kg K Spefic heat constant pressure % alpha = 1.26e-5 ; %m^2/s Diffusivity %h = 20 ; %W/m^2 K Convection coefficient % % pt_spc = 150 ; %"" Number of iterations betweem lines plotted % % for stabiltiy fround number should be less than 1/2

% del_t % t(1)

= 0.5*del_i^2/alpha =0

; %seconds Change in time each iteration ; %seconds Initial time

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Written by % 8 January 2008 % last modified 8 Janaury 2008 % Clear memory, close figures, clear command window for every run %Gets rid of old error messages, old figures, and doesn't leave extranius %variables to mess up the script clear all close all clc % Define input variables L = 0.1 n = 50 del_i = L/(n-1) T_hg T_cg T_2 t_2 = 500 = 250 = 293 = 100 ; %m Length of 1D space ; %"" Number of nodes ; %m Spacing of nodes ; %K Temperature of Hot gas ; %K Temperature of Cold gas ; %K Temperature of Cold side ; %Seconds Stopping time ; %W/(m K) Thermal conductivity - conduction ciefficient ; %kg.m^3 Density ; %J/kg K Spefic heat constant pressure ; %m^2/s Diffusivity ; %W/m^2 K Convection coefficient ; %"" Number of iterations betweem lines plotted

% properties for steel k = 45.8 rho = 7850 Cp = 460 alpha = 1.26e-5 h = 20 pt_spc = 150

% for stabiltiy fround number should be less than 1/2 del_t = 0.5*del_i^2/alpha ; %seconds Change in time each iteration t(1) =0 ; %seconds Initial time %Generate Storage matricies (Greatly increases run speed) T(1:n,1) = T_2 ; % Loop to generate x position matrix for j = 1:n x(j) = del_i*(j-1) end % Initiaze iteration variables m =1 mm =1 %Start While loop to run until convergence it met while t(m) < t_2 %Loop to calculate updated temperature based on governing equation for i = 1:n %Hot external surface if i == 1 T(i,m+1) = T(i,m) + h*del_t/(rho*Cp*del_i)*(T_hg - 2*T(i,m) + T_2); %Cold External Surface elseif i == n T(i,m+1) = T(i,m) + h*del_t/(rho*Cp*del_i)*(T_cg - 2*T(i,m) + T_2);

; ; %"" ; Time counter

%Internal nodes else T(i,m+1) = T(i,m) + k*del_t/(rho*Cp*del_i^2)*(T(i-1,m)-2*T(i,m)+T(i+1,m)); end % (if statement) end %(i loop) t(m+1) = t(m) + del_t ; %seconds Time

%Important to put all the (m+1) variables before the m = m +1 line m =m+1 ; %"" Update check counter mm = mm + 1 ;% plot counter %Conditional statement to check and make sure while loop does not run %indefinitely (Use Control C to stop infinite loop) if m == 20001 fprintf('Autobreak') %Print notice to announce auto stopping break end % (Check loop) end %( While loop) %Print the number of iteration used to command window fprintf('Number of Iterations was %0.2f \n\n',m)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Normal People plotting % Generate figure and plot results % figure(1) % plot(x,T(:,1:pt_spc:end)) % xlabel('Position - m') % ylabel('Temperature - K') % title('Temperatuer vs. Position') % % legend('Time = 0 s','Time = 24.8 s','Time = 49.6 s','Time = 74.4 s','Time = 99.165 s')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Fancy Plotting :) figure(1) hold on c h s % Color data C cc matter kk = round(length(t)/pt_spc)+1 = zeros(1, c) = cell(1, c) = colormap(jet) = round(length(C)/(c+1)) =1

; % find number of lines being plotted adding 1 for the zero tim ; % Handle storage ; % Prepare the string cell array

; %Set colormap ; %divide the color map into the number of lines being plotted p ; %counter

% Plot graphs for mmm = 1:pt_spc:length(t) h(kk) = plot(x, T(:,mmm), 'Color', C(kk*cc, :)) ; %plot graphs and store handle s{kk} = sprintf('Time = %0.2f', (kk-1)*pt_spc*del_t) ; %create text string of time stamp for each plotted line kk = kk+1 ; %update counter end % Create legend for the selected plots

legend(h, s) xlabel('Position') ylabel('Temperature - K') hold off

; %create legend with plot handles and strings

%End Script %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

You might also like