clear all
close all

%misc
%to execute unix command - use exclamation point
%!df

%hist - depricated - uses bin centers
%replaced by histogram - uses bin edges

pauses=1;
pauses=0;

fn=1;

if 0

disp(strcat("figure ",num2str(fn)))
figname='basic histogram plot';
f(fn)=figure;%('Name',figname);
reset(f(fn)); %reset current figure properties to default, unfortunately gets rid of name!
f(fn).Name=figname;
reset(gca);

z = randn(1000,1);
%histogram changed - cant specify bin centers anymore - only edges
BinWidth=1;
BinEdges = (-3:BinWidth:3);
histogram(z,BinEdges);
xlabel('x')
ylabel('counts')
title(figname)
if pauses; pause; end

[fn,fighandle]=CreateFigure(fn,'fancy histogram plot from Green - using handles - set');
f(fn)=fighandle;
handleh=histogram(z,BinEdges);
title(f(fn).Name)
%next 2 lines not working, return is empty
set(handleh,'FaceColor',[0.8 0.8 0.8]);
handlex=xlabel('z');
set(handlex,'FontName','Times New Roman');
handley=ylabel('Counts');
set(handley,'FontName','Courier');
set(gca,'XLim',[-3,3],'YTick',(0:100:400));

%histogram uses bin edges
%hist uses bin centers!!!
BinCounts = histcounts(z,BinEdges);
for i=1:length(BinCounts)
    text(BinEdges(i),BinCounts(i), num2str(BinCounts(i)), ...
    'FontSize',18,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom');
end
if pauses; pause; end

if 1
[fn,fighandle]=CreateFigure(fn,'fancy histogram plot from Green - using dot');
f(fn)=fighandle;
handleh=histogram(z,BinEdges);
title(f(fn).Name)
%next 2 lines not working, return is empty
handleh.FaceColor=[0.8 0.8 0.8];
handlex=xlabel('z');
handlex.FontName='Times New Roman';
handley=ylabel('Counts');
handley.FontName='Courier';
%gca.XLim=[-3,3]; %cause problems in later figures!
%gca.YTick=(0:100:400); %cause problems in later figures!

%histogram uses bin edges
%hist uses bin centers!!!
BinCounts = histcounts(z,BinEdges);
for i=1:length(BinCounts)
    text(BinEdges(i),BinCounts(i), num2str(BinCounts(i)), ...
    'FontSize',18,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom');
end
end

[fn,fighandle]=CreateFigure(fn,'fancy histogram plot minimize handle use');
f(fn)=fighandle;
histogram(z,BinEdges,'FaceColor',[0.8 0.8 0.8]);
xlabel('z');
ylabel('Counts');
set(gca,'FontSize',18); %the gca.XLim and gca.YTick calls commented out
%above make this fail!
set(gca,'XLim',[-3,3],'YTick',(0:100:400));
BinCounts = histcounts(z,BinEdges); 
for bincnt=1:length(BinCounts)
    handlet=text(BinEdges(bincnt)+BinWidth/2,BinCounts(bincnt), ...
    num2str(BinCounts(bincnt)),...
    'HorizontalAlignment','center', ...
    'VerticalAlignment','bottom', ...
    'FontSize',18,...
    'FontName','Papyrus');
end
title(f(fn).Name,'FontSize',10);
if pauses; pause; end

[fn,fighandle]=CreateFigure(fn,'basic trig plot');
f(fn)=fighandle;

t = (0:0.01:2*pi);
x = cos(t); 
y = sin(t);

%plot(t,x,'k-',t,y,'k--');
plot(t,x,'-',t,y,'--');
grid; xlabel('t');
ylabel('Amplitude');
legend('cos(t)','sin(t)');
title(f(fn).Name)
if pauses; pause; end

[fn,fighandle]=CreateFigure(fn,'fancy trig plot from Green');
f(fn)=fighandle;
plot(t,x,'k-',t,y,'k--')
grid;
set(gca,'FontSize',18);
xlabel('t');
ylabel('Amplitude');
%symbol no longer supported, have to use TeX or LaTeX to get special
%symbols
set(gca,'GridLineStyle','--',...
'YTick',(-1:1),...
'XTick',(0:pi/2:2*pi),...
'FontName','Helvetica',...
'XTickLabel',[{'0'};{'\pi/2'};...
{'\pi'};{'3\pi/2'};{'2\pi'}],...
'XLim',[0,2*pi],'YLim',[-1.1 1.1]);
LineHandles = findobj('Type','Line');
set(LineHandles,'LineWidth',3);
text(pi,0,'sin(t)','FontSize',18,...
'VerticalAlignment','bottom');
text(pi/2,0,'cos(t)',...
'Fontsize',18,...
'HorizontalAlignment','right', ...
'VerticalAlignment','top');
title(f(fn).Name,'FontSize',10)

[fn,fighandle]=CreateFigure(fn,'fancy trig plot minimize handles');
f(fn)=fighandle;
plot(t,x,'k-',t,y,'k--','LineWidth',3)
xlabel('t');
ylabel('Amplitude');
set(gca,'FontSize',18)

%symbol no longer supported, have to use TeX or LaTeX to get special
%symbols

grid('on');

%cant reduce handle use by very much

set(gca,'XLim',[0,2*pi],'YLim',[-1.1 1.1], ...
'GridLineStyle','--','GridAlpha',1, ...
'FontSize',18,'FontName','Helvetica',...
'YTick',(-1:1),...
'XTick',(0:pi/2:2*pi),...
'XTickLabel',[{'0'};{'\pi/2'};...
{'\pi'};{'3\pi/2'};{'2\pi'}]);

text(pi,0,'sin(t)','FontSize',18,...
'VerticalAlignment','bottom');
text(pi/2,0,'cos(t)','Fontsize',18,...
'HorizontalAlignment','right', ...
'VerticalAlignment','top');
title(f(fn).Name,'FontSize',10)

if 0 %causes following stuff to die
[fn,fighandle]=CreateFigure(fn,'fancy trig plot dot notation');
f(fn)=fighandle;
plot(t,x,'k-',t,y,'k--','LineWidth',3)
xlabel('t');
ylabel('Amplitude');
set(gca,'FontSize',18)

%symbol no longer supported, have to use TeX or LaTeX to get special
%symbols

grid('on');

%cant reduce handle use by very much

gca.XLim=[0,2*pi];
gca.YLim=[-1.1 1.1];
gca.GridLineStyle='--';
gca.GridAlpha=1;
gca.FontSize=18;
gca.FontName='Helvetica';
gca.YTick=(-1:1);
gca.XTick=(0:pi/2:2*pi);
gca.XTickLable=[{'0'};{'\pi/2'};{'\pi'};{'3\pi/2'};{'2\pi'}];

text(pi,0,'sin(t)','FontSize',18,...
'VerticalAlignment','bottom');
text(pi/2,0,'cos(t)','Fontsize',18,...
'HorizontalAlignment','right', ...
'VerticalAlignment','top');
title(f(fn).Name,'FontSize',10)
end

end %from the if to skip to here, dont need to increment fn

[fn,fighandle]=CreateFigure(fn,'simple bessel plot from Green');
f(fn)=fighandle;

beta = (0:0.1:15);
n = [0:3,10];
for cnt=1:length(n)
J(cnt,:) = besselj(n(cnt),beta);
end
plot(beta,J);
xlabel('beta');
ylabel('J(n,beta)');
legend('n=0','n=1','n=2','n=3','n=10')
title(f(fn).Name)

[fn,fighandle]=CreateFigure(fn,'fancy bessel plot from Green');
f(fn)=fighandle;

beta = (0:0.1:15); 
n = [0:3,10];
for cnt=1:length(n)
J(cnt,:) = besselj(n(cnt), beta);
end

%Wouldn?t it be nice if MATLAB sequenced through line styles rather than 
%colors when plotting multiple curves? With Handle Graphics, we can tell 
%MATLAB to do just that.
%Although thebelow  properties do not appear when you type get(0), you can
%set the default of any graphic object property in the root. You simply
%concatenate Default with an object name, such as Axes, followed by the
%desired property, such as ColorOrder. Thus, by typing
%set(0,’DefaultAxesColorOrder’,[0 0 0]), every plot will only sequence
%through a single color, black, which is identified by the RGB triple
%[0 0 0]. In a similar manner, the next line provide us with our sequencing
%line styles. The last lines set future text and axes font sizes as well as
%line object line widths.
set(0,'DefaultAxesColorOrder',[0 0 0],...
'DefaultAxesLineStyleOrder',...
('-|--|-.|:|.'),...
'DefaultAxesFontSize',18,...
'DefaultTextFontSize',16,...
'DefaultLineLineWidth',3);

%Now, the plot command of plots the individual Bessel function curves with
%sequencing line styles, each black and with a LineWidth of 3.
plot(beta,J);

%How to obtain Greek characters. The axis labels both require ? as well as
%some subscripting
%MATLAB will generally produce Greek characters if you sim-ply spell them
%out and precede them with a \ character (e.g.,\beta produces ?). Each text
%object has a property called Interpreter that tells MATLAB how to read the
%string argument. By default, the Interpreter is set to tex.
%The Position property in allows the axis labels to be positioned to 
%complement axis tick labels. Although this is a two dimensional plot and
%the z-axis is irrel-evant, the position property still requires three
%arguments to specify the x-, y-, and z-positions, respectively.
xlabel('\beta','Position',[6.5 -0.6 1.0]);

%Notice that the underscore causes subscripting (this is a problem when
%what if you want to print an underscore)
%The rotation of the y-axis label is set to zero so you do not need to
%crane your neck to read the text.
ylabel('J_n(\beta)','Rotation',0,'Position',[-1.5 0.4 1.0]);

%set various axes properties.
%As it does not really add anything to the plot, the plot bounding box is
%turned off with the Box property.
%Many of the tick labels are set blank (‘ ‘) to reduce crowding.
set(gca,'box','off','XTick',(0:15),...
'XTickLabel',[{'0'}; {'1'};{'2'};...
{'3'};{' '};{' '};{' '};{' '};...
{' '};{' '};{'10'};{' '};{' '};...
{' '};{' '};{'15'}],...
'YTick',(-.4:.2:1),...
'YTickLabel',[{' '},{' '},{'0.0'},...
{' '},{' '},{' '},{' '},{'1.0'}],...
'XLim',[0 15],'YLim',[-0.5 1.1]);

%Replace the original legend command, placing text objects near the
%maximum amplitudes of their respective Bessel functions.
for cnt = 1:length(n)
[mx,ind] = max(J(cnt,:));
text(beta(ind)+0.2,mx+0.05,['n = ',num2str(n(cnt))]);
end
title(f(fn).Name)

[fn,fighandle]=CreateFigure(fn,'fancier - almost perfect - bessel plot from Green');
f(fn)=fighandle;

%going for perfection!!!
%The really astute and detail-oriented reader will note that the symbols in
%Fig. 6, while good, are not quite right. They are too vertical and do not
%follow the italic bend like Jn(?). It turns out that we can fix this, too,
%since MATLAB also has a LATEX interpreter.
beta = (0:0.1:15); 
n = [0:3,10];
for cnt=1:length(n)
J(cnt,:) = besselj(n(cnt), beta);
end
set(0,'DefaultAxesColorOrder',[0 0 0],...
'DefaultAxesLineStyleOrder',...
('-|--|-.|:|.'),...
'DefaultAxesFontSize',18,...
'DefaultTextFontSize',16,...
'DefaultLineLineWidth',3);
plot(beta,J);

xlabel('$\beta$','Position',[6.5 -0.6 1.0],...
'Interpreter','latex');
ylabel('$J_n(\beta)$','Rotation',0,...
'Position',[-1.5 0.4 1.0],...
'Interpreter','latex');

grid on
set(gca,'GridLineStyle','--','GridAlpha',1);

set(gca,'box','off','XTick',(0:15),...
'XTickLabel',[{'0'}; {'1'};{'2'};...
{'3'};{' '};{' '};{' '};{' '};...
{' '};{' '};{'10'};{' '};{' '};...
{' '};{' '};{'15'}],...
'YTick',(-.4:.2:1),...
'YTickLabel',[{' '},{' '},{'0.0'},...
{' '},{' '},{' '},{' '},{'1.0'}],...
'XLim',[0 15],'YLim',[-0.5 1.1]);

%more LATEX
for cnt = 1:length(n)
[mx,ind] = max(J(cnt,:));
text(beta(ind)+0.2,mx+0.05, ...
    ['$n = ',num2str(n(cnt)),'$'],'Interpreter','latex');
end

%use latex to make title with math!!!
LaTeX_Expr = 'm\,\frac{\partial ^2}{\partial t^2} x\left(t\right)+J\,\frac{\partial ^2}{\partial t^2} \mathrm{theta}\left(t\right)';
title(['$$' LaTeX_Expr '$$'], 'Interpreter','latex')



