%%
set(0,'DefaultTextFontName','Times','DefaultTextFontSize',18,...
   'DefaultAxesFontName','Times','DefaultAxesFontSize',18,...
   'DefaultLineLineWidth',1,'DefaultLineMarkerSize',7.75)

%%

t = linspace(0,1,200);   % Create data to plot
y = sin(2*pi*t);
tau = linspace(0,1,10);
x = sin(2*pi*tau);

% subplot(2,2,1)
plot(t,y,tau,x,'ro')

grid on;   % optional

% Optionally add some text, a label, and a title
text(0.6,0.5,'sin(2\pi\itt\rm)')
xlabel('\itt')
title('Plotting a Function with MatLab')

print('-depsc2','-r600','plotfile3.eps') % Print to file
%%

% Script to plot -t log t function
f = @(t)-t.*log(t);
t = linspace(0,1.5,200);
y = f(t);
tau = linspace(0,1.5,7);
x = f(tau);

subplot(2,2,1)
plot(t,y,'k',tau,x,'ro'); grid on

xlabels = strvcat('0', ' ', '1');
ylabels = strvcat('0', ' ' );
set(gca,'XTick',[0 1/exp(1) 1],'XTickLabel',xlabels,...
     'YTick',[0 1/exp(1)],'YTickLabel',ylabels)
text('Interpreter','latex','String','$- \kern.8em t \log t$',...
   'Position',[1 1/exp(1)-.2 ])
text('Interpreter','latex','String','$\frac{\log e}{e}$',...
   'Position',[-.3 1/exp(1)])
text('Interpreter','latex','String','$1/e$',...
   'Position',[1/exp(1)-.09 -1.15])

print('-depsc2','-r600','plotfile4.eps')
%% Histograms
n = 50000;       % Number of simulations
X = rand(1,n);
Y = rand(1,n)*2;
Z = X+Y;
nbins = 40; % Number of bins for histogram
hstgrm = makedenshist(Z,nbins);
plothist(hstgrm)
% Now plot true density of Z
z = linspace(0,3,200);
f = @(z).5*(z.*(0<=z & z<1)+(1<=z & z<2)+(3-z).* ...
   (2<=z & z<=3));
hold on
plot(z,f(z),'k'); grid on
hold off
print('-depsc2','-r600','plotfile5.eps')
%%

function [hstgrm,varargout] = makedenshist(Z,nbins)
% Make a density histogram with nbins bins out of the data in Z.
% We return the 2-by-nbins array hstgrm, where
% hstgrm(1,:) = the list of bin centers, and
% hstgrm(2,:) = normalized histogram heights.
%
% The command
%
%    hstgrm = makedenshist(Z,nbins)
%
% always prints the minimum and maximum data samples,
% denoted by minZ and maxZ.  Alternatively, the command
%
%   [hstgrm,minZ,maxZ] = makedenshist(Z,nbins)
%
% returns these values to you without printing them.

hstgrm = zeros(2,nbins);         % Pre-allocate space

minZ = min(Z);                   % Determine range of data
maxZ = max(Z);
if nargout==3
   varargout{1} = minZ;
   varargout{2} = maxZ;
else
   fprintf('makedenshist: Data range = [ %g , %g ].\n',minZ,maxZ)
end

e = linspace(minZ,maxZ,nbins+1); % Set edges of bins

a = e(1:nbins);                  % Compute centers of bins
b = e(2:nbins+1);                % and store result in
hstgrm(1,:) = (a+b)/2;           % hstgrm(1,:)

H = histc(Z,e);                  % Get bin heights

H(nbins) = H(nbins)+H(nbins+1);  % Put any hits on right-most
                                 % edge into last bin

% Compute and store the normalized bin heights           
bw = (maxZ-minZ)/nbins;
hstgrm(2,:) = H(1:nbins)/(bw*length(Z));

%%

function plothist(hstgrm);
% Plot a histogram generated by makedenshist.
% Actually, as long as
% hstgrm(1,:) = the list of bin centers, and
% hstgrm(2,:) = normalized histogram heights,
% plothist will work for you.

bar(hstgrm(1,:),hstgrm(2,:),'hist')
h = findobj(gca,'Type','patch');
set(h,'FaceColor','w','EdgeColor','k')