function heat_exchanger
%built by keith regner 3/2017 with R2014b (8.4 OSX)
clear 
close all

%% description
% This demo solves for the hot (solid red lines) and cold (solid blue 
% lines) fluid temperatures as a function of normalized position x/L along 
% the length of a heat exchanger for both counterflow (left plot) and 
% parallel flow (right plot) configurations. Here, L is the length of the 
% heat exchanger. Adjust the hot and cold fluid inlet temperatures Th,i 
% and Tc,i, the overall heat transfer coefficient U, the length of the 
% heat exchanger L, and the hot and cold fluid heat capacity rates Ch and 
% Cc using the slider bars. The total heat transferred by the fluids for 
% counterflow and parallel flow qCF and qPF and the hot and cold fluid 
% outlet temperatures Th,o and Tc,o are indicated in the schematic.
% 
% NOTE: The outer wall is assumed to be perfectly insulating and the wall 
% thickness between the fluids is assumed to be thin. The parameter P is 
% the ?perimeter? dimension used to calculate the total surface area As 
% (e.g., for a concentric tube heat exchanger P = ?D where D is the 
% diameter of the inner tube).


%% parameters and slider bar limits
b.P = pi*.01;
b.x = 0:1/100:1;

b.Cc = 22.1;
slideminCc = 1;
slidemaxCc = 1001;

b.Tci = 293;
slideminTci = 200;
slidemaxTci = 400;

b.Ch = 196.01;
slideminCh = 1;
slidemaxCh = 1001;

b.Thi = 333;
slideminThi = 300;
slidemaxThi = 600;

b.L = 2;
slideminL = 1;
slidemaxL = 101;

b.U = 135;
slideminU = 1;
slidemaxU = 1001;

%% solve for temperature profiles
[b.Thc,b.Tcc,b.Thp,b.Tcp,b.qc,b.qp] = fluid_temp(b,b.Cc,b.Ch,...
    b.Tci,b.Thi,b.U,b.L);

%% figure and axis formatting
b.fig = figure('Units','inches','Position',[3 .25 14 9],...
    'Name','heat_exchanger','NumberTitle','off');
b.ax1 = axes('Units','inches','Position',[1.1 .6 4.5 3.6],'FontSize',12,...
    'LineWidth',1.5,'Box','on','XLim',[0 1],'YLim',[200 600]);
hold on
b.ax1.XLabel.String = '{\itx}/{\itL}';
b.ax1.YLabel.String = 'temperature [K]';
b.ax2 = axes('Units','inches','Position',[8.7 .6 4.5 3.6],'FontSize',12,...
    'LineWidth',1.5,'Box','on','XLim',[0 1],'YLim',[200 600]);
hold on
b.ax2.XLabel.String = '{\itx}/{\itL}';
b.ax2.YLabel.String = 'temperature [K]';

%% plot solution
b.h_Thp = plot(b.ax2,b.x,b.Thp,'Color',[1 0 0],...
    'LineWidth',1.5);
b.h_Tcp = plot(b.ax2,b.x,b.Tcp,'Color',[0 0 1],...
    'LineWidth',1.5);
legend(b.ax2,{'hot fluid','cold fluid'},...
    'Location','NorthEast','Box','off','FontSize',12);

b.h_Thc = plot(b.ax1,b.x,b.Thc,'Color',[1 0 0],...
    'LineWidth',1.5);
b.h_Tcc = plot(b.ax1,b.x,b.Tcc,'Color',[0 0 1],...
    'LineWidth',1.5);
legend(b.ax1,{'hot fluid','cold fluid'},...
    'Location','NorthEast','Box','off','FontSize',12);


%% user control
%total q txt boxes
b.txtqc = annotation('textbox','Units','inches','Position',...
    [2 7.1 2.5 .3],'LineStyle','none','String',...
    ['{\itq}_C_F = ' num2str(round(b.qc,0)) ' W'],'FontSize',14);
b.txtqp = annotation('textbox','Units','inches','Position',...
    [9.65 7.1 2.5 .3],'LineStyle','none','String',...
    ['{\itq}_P_F = ' num2str(round(b.qp,0)) ' W'],'FontSize',14);
%outlet temp txt boxes counter
b.txtThoc = annotation('textbox','Units','inches','Position',...
    [3.95 7.25 2.5 .3],'LineStyle','none','String',...
    ['{\itT}_h_,_o = ' num2str(round(b.Thc(end),0)) ' K'],...
    'Color',[1 0 .6],'FontSize',14);
b.txtTcoc = annotation('textbox','Units','inches','Position',...
    [1.5 5.45 2.5 .3],'LineStyle','none','String',...
    ['{\itT}_c_,_o = ' num2str(round(b.Tcc(1),0)) ' K'],...
    'Color',[.6 0 1],'FontSize',14);
%outlet temp txt boxes parallel
b.txtThop = annotation('textbox','Units','inches','Position',...
    [11.5 7.25 2.5 .3],'LineStyle','none','String',...
    ['{\itT}_h_,_o = ' num2str(round(b.Thp(end),0)) ' K'],...
    'Color',[1 0 .6],'FontSize',14);
b.txtTcop = annotation('textbox','Units','inches','Position',...
    [11.5 5.45 2.5 .3],'LineStyle','none','String',...
    ['{\itT}_c_,_o = ' num2str(round(b.Tcp(end),0)) ' K'],...
    'Color',[.6 0 1],'FontSize',14);
%cold fluid
b.txtCc = annotation('textbox','Units','inches','Position',...
    [6.4 5.4 1.5 .3],'LineStyle','none','String',...
    ['{\itC}_c = ' num2str(round(b.Cc,0)) ' W/K'],'FontSize',14,'Color',...
    [0 0 1]);
b.slCc = uicontrol('Parent',b.fig,'Style','slide','Units','inches',...
    'Position',[6.4 5.2 1.5 .2],'Min',slideminCc,'Max',slidemaxCc,...
    'Value',b.Cc,'SliderStep',[1/1000 1/20],'Tag','sliderCc',...
    'Callback',{@slider_Cc,b},'BackgroundColor',b.fig.Color);
b.txtTci = annotation('textbox','Units','inches','Position',...
    [6.4 4.65 1.5 .3],'LineStyle','none','String',...
    ['{\itT}_c_,_i = ' num2str(round(b.Tci,0)) ' K'],'FontSize',14,...
    'Color',[0 0 1]);
b.slTci = uicontrol('Parent',b.fig,'Style','slide','Units','inches',...
    'Position',[6.4 5 1.5 .2],'Min',slideminTci,'Max',slidemaxTci,...
    'Value',b.Tci,'SliderStep',[1/200 1/10],'Tag','sliderTci',...
    'Callback',{@slider_Tci,b},'BackgroundColor',b.fig.Color);
%hot fluid
b.txtCh = annotation('textbox','Units','inches','Position',...
    [6.4 8.1 1.5 .3],'LineStyle','none','String',...
    ['{\itC}_h = ' num2str(round(b.Ch,0)) ' W/K'],'FontSize',14,'Color',...
    [1 0 0]);
b.slCh = uicontrol('Parent',b.fig,'Style','slide','Units','inches',...
    'Position',[6.4 7.9 1.5 .2],'Min',slideminCh,'Max',slidemaxCh,...
    'Value',b.Ch,'SliderStep',[1/1000 1/20],'Tag','sliderCh',...
    'Callback',{@slider_Ch,b},'BackgroundColor',b.fig.Color);
b.txtThi = annotation('textbox','Units','inches','Position',...
    [6.4 7.35 1.5 .3],'LineStyle','none','String',...
    ['{\itT}_h_,_i = ' num2str(round(b.Thi,0)) ' K'],'FontSize',14,...
    'Color',[1 0 0]);
b.slThi = uicontrol('Parent',b.fig,'Style','slide','Units','inches',...
    'Position',[6.4 7.7 1.5 .2],'Min',slideminThi,'Max',slidemaxThi,...
    'Value',b.Thi,'SliderStep',[1/200 1/10],'Tag','sliderThi',...
    'Callback',{@slider_Thi,b},'BackgroundColor',b.fig.Color);
%U and length
b.txtU = annotation('textbox','Units','inches','Position',...
    [6.3 6.75 2.0 .3],'LineStyle','none','String',...
    ['{\itU} = ' num2str(round(b.U,0)) ' W/m^2-K'],'FontSize',14);
b.slU = uicontrol('Parent',b.fig,'Style','slide','Units','inches',...
    'Position',[6.4 6.55 1.5 .2],'Min',slideminU,'Max',slidemaxU,...
    'Value',b.U,'SliderStep',[1/1000 1/20],'Tag','sliderU',...
    'Callback',{@slider_U,b},'BackgroundColor',b.fig.Color);
b.txtL = annotation('textbox','Units','inches','Position',...
    [6.6 6 1.5 .3],'LineStyle','none','String',...
    ['{\itL} = ' num2str(round(b.L,1)) ' m'],'FontSize',14);
b.slL = uicontrol('Parent',b.fig,'Style','slide','Units','inches',...
    'Position',[6.4 6.35 1.5 .2],'Min',slideminL,'Max',slidemaxL,...
    'Value',b.L,'SliderStep',[1/100 1/10],'Tag','sliderL',...
    'Callback',{@slider_L,b},'BackgroundColor',b.fig.Color);

%% illustration
%counter
annotation('textbox','Units','inches','Position',...
    [1.1 4.7 4.5 .1],'BackgroundColor',[.6 .6 .6]);
annotation('arrow','Units','inches','Position',...
    [1.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [1.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [1.5 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [1.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [1.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.5 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.5 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.5 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [5.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [5.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [5.5 4.7 .15 -.15],'HeadStyle', 'none');

annotation('textbox','Units','inches','Position',...
    [1.1 6.5 4.5 .1],'BackgroundColor',[.6 .6 .6]);
annotation('textbox','Units','inches','Position',...
    [1.1 8.3 4.5 .1],'BackgroundColor',[.6 .6 .6]);
annotation('arrow','Units','inches','Position',...
    [1.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [1.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [1.45 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [1.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [1.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.45 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [2.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.45 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [3.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.45 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [4.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [5.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [5.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [5.45 8.56 .15 -.15],'HeadStyle', 'none');

%parallel
annotation('textbox','Units','inches','Position',...
    [8.7 4.7 4.5 .1],'BackgroundColor',[.6 .6 .6]);
annotation('arrow','Units','inches','Position',...
    [8.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [8.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.5 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.5 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.5 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.1 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.3 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.5 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.7 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.9 4.7 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [13.1 4.7 .15 -.15],'HeadStyle', 'none');

annotation('textbox','Units','inches','Position',...
    [8.7 6.5 4.5 .1],'BackgroundColor',[.6 .6 .6]);
annotation('textbox','Units','inches','Position',...
    [8.7 8.3 4.5 .1],'BackgroundColor',[.6 .6 .6]);
annotation('arrow','Units','inches','Position',...
    [8.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [8.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.45 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [9.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.45 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [10.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.45 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [11.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.05 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.25 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.45 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.65 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [12.85 8.56 .15 -.15],'HeadStyle', 'none');
annotation('arrow','Units','inches','Position',...
    [13.05 8.56 .15 -.15],'HeadStyle', 'none');

%x-axis labels
annotation('arrow','Units','inches','Position',...
    [1.1 4.4 .4 0]);
annotation('arrow','Units','inches','Position',...
    [1.1 4.4 0 .1],'HeadStyle','none');
annotation('textbox','Units','inches','Position',...
    [1.5 4.2 .5 .3],'LineStyle','none','String',...
    '{\itx}','FontSize',14);

annotation('arrow','Units','inches','Position',...
    [8.7 4.4 .4 0]);
annotation('arrow','Units','inches','Position',...
    [8.7 4.4 0 .1],'HeadStyle','none');
annotation('textbox','Units','inches','Position',...
    [9.1 4.2 .5 .3],'LineStyle','none','String',...
    '{\itx}','FontSize',14);

%fluid arrows
annotation('arrow','Units','inches','Position',...
    [0.7 7.025 .4 0],'Color',[1 0 0]);
annotation('arrow','Units','inches','Position',...
    [0.7 7.45 .4 0],'Color',[1 0 0]);
annotation('arrow','Units','inches','Position',...
    [0.7 7.875 .4 0],'Color',[1 0 0]);
annotation('arrow','Units','inches','Position',...
    [6.0 5.225 -.4 0],'Color',[0 0 1]);
annotation('arrow','Units','inches','Position',...
    [6.0 5.65 -.4 0],'Color',[0 0 1]);
annotation('arrow','Units','inches','Position',...
    [6.0 6.175 -.4 0],'Color',[0 0 1]);

annotation('arrow','Units','inches','Position',...
    [5.2 7.025 .4 0],'Color',[1 0 .6]);
annotation('arrow','Units','inches','Position',...
    [5.2 7.45 .4 0],'Color',[1 0 .6]);
annotation('arrow','Units','inches','Position',...
    [5.2 7.875 .4 0],'Color',[1 0 .6]);
annotation('arrow','Units','inches','Position',...
    [1.5 5.225 -.4 0],'Color',[.6 0 1]);
annotation('arrow','Units','inches','Position',...
    [1.5 5.65 -.4 0],'Color',[.6 0 1]);
annotation('arrow','Units','inches','Position',...
    [1.5 6.175 -.4 0],'Color',[.6 0 1]);

annotation('arrow','Units','inches','Position',...
    [8.3 7.025 .4 0],'Color',[1 0 0]);
annotation('arrow','Units','inches','Position',...
    [8.3 7.45 .4 0],'Color',[1 0 0]);
annotation('arrow','Units','inches','Position',...
    [8.3 7.875 .4 0],'Color',[1 0 0]);
annotation('arrow','Units','inches','Position',...
    [8.3 5.225 .4 0],'Color',[0 0 1]);
annotation('arrow','Units','inches','Position',...
    [8.3 5.65 .4 0],'Color',[0 0 1]);
annotation('arrow','Units','inches','Position',...
    [8.3 6.175 .4 0],'Color',[0 0 1]);

annotation('arrow','Units','inches','Position',...
    [12.8 7.025 .4 0],'Color',[1 0 .6]);
annotation('arrow','Units','inches','Position',...
    [12.8 7.45 .4 0],'Color',[1 0 .6]);
annotation('arrow','Units','inches','Position',...
    [12.8 7.875 .4 0],'Color',[1 0 .6]);
annotation('arrow','Units','inches','Position',...
    [12.8 5.225 .4 0],'Color',[.6 0 1]);
annotation('arrow','Units','inches','Position',...
    [12.8 5.65 .4 0],'Color',[.6 0 1]);
annotation('arrow','Units','inches','Position',...
    [12.8 6.175 .4 0],'Color',[.6 0 1]);

%U arrows
annotation('arrow','Units','inches','Position',...
    [6.2 6.55 -.8 0],'Color',[0 0 0],'HeadStyle','ellipse',... 
    'HeadLength',4,'HeadWidth',4);
annotation('arrow','Units','inches','Position',...
    [8.1 6.55 .8 0],'Color',[0 0 0],'HeadStyle','ellipse',... 
    'HeadLength',4,'HeadWidth',4);

%q arrows
annotation('arrow','Units','inches','Position',...
    [3.35 7.1 0 -1.2],'Color',[0 0 0]);
annotation('arrow','Units','inches','Position',...
    [10.95 7.1 0 -1.2],'Color',[0 0 0]);

%titles
annotation('textbox','Units','inches','Position',...
    [2.4 8.6 2.5 .3],'LineStyle','none','String',...
    'counter-flow','FontSize',20);
annotation('textbox','Units','inches','Position',...
    [10.2 8.6 2.5 .3],'LineStyle','none','String',...
    'parallel flow','FontSize',20);

set(findall(b.fig,'-property','FontUnits'),'FontUnits','normalized')
set(findall(b.fig,'-property','Units'),'Units','normalized')

end

%% callback for slider
%Tci
function [] = slider_Tci(varargin)
[H,b] = varargin{[1,3]};
h2 = findobj('Tag','sliderCc');
h3 = findobj('Tag','sliderThi');
h4 = findobj('Tag','sliderCh');
h5 = findobj('Tag','sliderU');
h6 = findobj('Tag','sliderL');
Tci = H.Value;
Cc = h2.Value;
Thi = h3.Value;
Ch = h4.Value;
U = h5.Value;
L = h6.Value;
[Thc,Tcc,Thp,Tcp,qc,qp] = fluid_temp(b,Cc,Ch,Tci,Thi,U,L);

set(b.txtThoc,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thc(end),0)) ' K']);
set(b.txtTcoc,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcc(1),0)) ' K']);
set(b.txtThop,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thp(end),0)) ' K']);
set(b.txtTcop,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcp(end),0)) ' K']);
set(b.txtqc,'String',['{\itq}_C_F = ' num2str(round(qc,0)) ' W']);
set(b.txtqp,'String',['{\itq}_P_F = ' num2str(round(qp,0)) ' W']);
set(b.txtTci,'String',['{\itT}_c_,_i = ' num2str(round(Tci,0)) ' K']);
set(b.h_Thp,'YData',Thp);
set(b.h_Tcp,'YData',Tcp);
set(b.h_Thc,'YData',Thc);
set(b.h_Tcc,'YData',Tcc);
end

%Cc
function [] = slider_Cc(varargin)
[H,b] = varargin{[1,3]};
h1 = findobj('Tag','sliderTci');
h3 = findobj('Tag','sliderThi');
h4 = findobj('Tag','sliderCh');
h5 = findobj('Tag','sliderU');
h6 = findobj('Tag','sliderL');
Tci = h1.Value;
Cc = H.Value;
Thi = h3.Value;
Ch = h4.Value;
U = h5.Value;
L = h6.Value;
[Thc,Tcc,Thp,Tcp,qc,qp] = fluid_temp(b,Cc,Ch,Tci,Thi,U,L);

set(b.txtThoc,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thc(end),0)) ' K']);
set(b.txtTcoc,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcc(1),0)) ' K']);
set(b.txtThop,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thp(end),0)) ' K']);
set(b.txtTcop,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcp(end),0)) ' K']);
set(b.txtqc,'String',['{\itq}_C_F = ' num2str(round(qc,0)) ' W']);
set(b.txtqp,'String',['{\itq}_P_F = ' num2str(round(qp,0)) ' W']);
set(b.txtCc,'String',['{\itC}_c = ' num2str(round(Cc,0)) ' W/K']);
set(b.h_Thp,'YData',Thp);
set(b.h_Tcp,'YData',Tcp);
set(b.h_Thc,'YData',Thc);
set(b.h_Tcc,'YData',Tcc);
end

%Thi
function [] = slider_Thi(varargin)
[H,b] = varargin{[1,3]};
h1 = findobj('Tag','sliderTci');
h2 = findobj('Tag','sliderCc');
h4 = findobj('Tag','sliderCh');
h5 = findobj('Tag','sliderU');
h6 = findobj('Tag','sliderL');
Tci = h1.Value;
Cc = h2.Value;
Thi = H.Value;
Ch = h4.Value;
U = h5.Value;
L = h6.Value;
[Thc,Tcc,Thp,Tcp,qc,qp] = fluid_temp(b,Cc,Ch,Tci,Thi,U,L);

set(b.txtThoc,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thc(end),0)) ' K']);
set(b.txtTcoc,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcc(1),0)) ' K']);
set(b.txtThop,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thp(end),0)) ' K']);
set(b.txtTcop,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcp(end),0)) ' K']);
set(b.txtqc,'String',['{\itq}_C_F = ' num2str(round(qc,0)) ' W']);
set(b.txtqp,'String',['{\itq}_P_F = ' num2str(round(qp,0)) ' W']);
set(b.txtThi,'String',['{\itT}_h_,_i = ' num2str(round(Thi,0)) ' K']);
set(b.h_Thp,'YData',Thp);
set(b.h_Tcp,'YData',Tcp);
set(b.h_Thc,'YData',Thc);
set(b.h_Tcc,'YData',Tcc);
end

%Ch
function [] = slider_Ch(varargin)
[H,b] = varargin{[1,3]};
h1 = findobj('Tag','sliderTci');
h2 = findobj('Tag','sliderCc');
h3 = findobj('Tag','sliderThi');
h5 = findobj('Tag','sliderU');
h6 = findobj('Tag','sliderL');
Tci = h1.Value;
Cc = h2.Value;
Thi = h3.Value;
Ch = H.Value;
U = h5.Value;
L = h6.Value;
[Thc,Tcc,Thp,Tcp,qc,qp] = fluid_temp(b,Cc,Ch,Tci,Thi,U,L);

set(b.txtThoc,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thc(end),0)) ' K']);
set(b.txtTcoc,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcc(1),0)) ' K']);
set(b.txtThop,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thp(end),0)) ' K']);
set(b.txtTcop,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcp(end),0)) ' K']);
set(b.txtqc,'String',['{\itq}_C_F = ' num2str(round(qc,0)) ' W']);
set(b.txtqp,'String',['{\itq}_P_F = ' num2str(round(qp,0)) ' W']);
set(b.txtCh,'String',['{\itC}_h = ' num2str(round(Ch,0)) ' W/K']);
set(b.h_Thp,'YData',Thp);
set(b.h_Tcp,'YData',Tcp);
set(b.h_Thc,'YData',Thc);
set(b.h_Tcc,'YData',Tcc);
end

%U
function [] = slider_U(varargin)
[H,b] = varargin{[1,3]};
h1 = findobj('Tag','sliderTci');
h2 = findobj('Tag','sliderCc');
h3 = findobj('Tag','sliderThi');
h4 = findobj('Tag','sliderCh');
h6 = findobj('Tag','sliderL');
Tci = h1.Value;
Cc = h2.Value;
Thi = h3.Value;
Ch = h4.Value;
U = H.Value;
L = h6.Value;
[Thc,Tcc,Thp,Tcp,qc,qp] = fluid_temp(b,Cc,Ch,Tci,Thi,U,L);

set(b.txtThoc,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thc(end),0)) ' K']);
set(b.txtTcoc,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcc(1),0)) ' K']);
set(b.txtThop,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thp(end),0)) ' K']);
set(b.txtTcop,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcp(end),0)) ' K']);
set(b.txtqc,'String',['{\itq}_C_F = ' num2str(round(qc,0)) ' W']);
set(b.txtqp,'String',['{\itq}_P_F = ' num2str(round(qp,0)) ' W']);
set(b.txtU,'String',['{\itU} = ' num2str(round(U,0)) ' W/m^2-K']);
set(b.h_Thp,'YData',Thp);
set(b.h_Tcp,'YData',Tcp);
set(b.h_Thc,'YData',Thc);
set(b.h_Tcc,'YData',Tcc);
end

%L
function [] = slider_L(varargin)
[H,b] = varargin{[1,3]};
h1 = findobj('Tag','sliderTci');
h2 = findobj('Tag','sliderCc');
h3 = findobj('Tag','sliderThi');
h4 = findobj('Tag','sliderCh');
h5 = findobj('Tag','sliderU');
Tci = h1.Value;
Cc = h2.Value;
Thi = h3.Value;
Ch = h4.Value;
U = h5.Value;
L = H.Value;
[Thc,Tcc,Thp,Tcp,qc,qp] = fluid_temp(b,Cc,Ch,Tci,Thi,U,L);

set(b.txtThoc,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thc(end),0)) ' K']);
set(b.txtTcoc,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcc(1),0)) ' K']);
set(b.txtThop,'String',...
    ['{\itT}_h_,_o = ' num2str(round(Thp(end),0)) ' K']);
set(b.txtTcop,'String',...
    ['{\itT}_c_,_o = ' num2str(round(Tcp(end),0)) ' K']);
set(b.txtqc,'String',['{\itq}_C_F = ' num2str(round(qc,0)) ' W']);
set(b.txtqp,'String',['{\itq}_P_F = ' num2str(round(qp,0)) ' W']);
set(b.txtL,'String',['{\itL} = ' num2str(round(L,1)) ' m']);
set(b.h_Thp,'YData',Thp);
set(b.h_Tcp,'YData',Tcp);
set(b.h_Thc,'YData',Thc);
set(b.h_Tcc,'YData',Tcc);
end

%% calculate fluid temperature
function [Thc,Tcc,Thp,Tcp,qc,qp] = fluid_temp(b,Cc,Ch,Tci,Thi,U,L)
lamc = b.P*U/Cc;
lamh = b.P*U/Ch;
x = b.x*L;
C1p = lamc*(Tci - Thi);
C2p = (Tci*lamh + Thi*lamc)/(lamc + lamh);
Tcp = ((C1p*exp(-x*(lamh + lamc)))/(lamc + lamh)) + C2p;
Thp = (-C1p*exp(-x*(lamh + lamc))*lamh/(lamc*(lamc +...
    lamh))) + C2p;
C1c = (lamc*(lamc - lamh)*(Thi - Tci))/(lamh - lamc*...
    exp(-L*(lamh - lamc)));
C2c = (Tci*lamh - Thi*lamc*exp(-L*(lamh - lamc)))/...
    (lamh - lamc*exp(-L*(lamh - lamc)));
Tcc = ((C1c*exp(-x*(lamh - lamc))/(lamc - lamh))) + C2c;
Thc = ((C1c*exp(-x*(lamh - lamc))*lamh)/(lamc*...
    (lamc - lamh))) + C2c;
Tcop = Tcp(end);
Thop = Thp(end);
Tcoc = Tcc(1);
Thoc = Thc(end); 
DT1p = Thi - Tci;
DT2p = Thop - Tcop;
DT1c = Thi - Tcoc;
DT2c = Thoc - Tci;
DTlmp = (DT2p - DT1p)/(log(DT2p/DT1p));
DTlmc = (DT2c - DT1c)/(log(DT2c/DT1c));
qc = U*b.P*L*DTlmc;
qp = U*b.P*L*DTlmp;
end

