%LMFig070501_4th.m
% Based on LMFig060205_4th. Page 500 in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu 3/13/11
df=5;chi05=chi2inv(0.05,df);
chi99=chi2inv(0.99,df);
zmax=17.1;
z=0:.01:zmax;
fzz=chi2pdf(z,df);
plot(z,fzz,'linestyle','--','color','r','linewidth',3)
ylabel('Probability Density','FontSize',20)
xlabel('z','FontSize',20)
axis([0 zmax 0 0.17])
set(gca,'Ytick',[0:.05:0.15],'FontSize',18)
set(gca,'Xtick',[chi05 4:4:12 chi99],'FontSize',18)
ax=axis;
ax1=gca; % save the handle of the graph
title('Figure 7.5.1','FontSize',22)
hold on
fz=chi2pdf([chi05 chi99],df);
plot([chi05 chi05;chi99 chi99]',[0 fz(1);0 fz(2)]','-k','linewidth',1) 
% Fill in the upper tail with fill
y2=chi99:.001:ax(2);
fy2=chi2pdf(y2,df);
fymax=chi2pdf(ax(2),df);
fill([chi99 y2 ax(2) ax(2)],[0 fy2 fymax 0],[.8 .8 1])
% Fill in the lower tail with fill
y3=ax(1):.001:chi05;
fy3=chi2pdf(y3,df);
% fymin=chi2pdf(ax(1),df);
fymin=0;
fill([ax(1) ax(1) y3 chi05],[0 fymin fy3 0],[.8 .8 1])
t=sprintf('Area=0.05');
t1='Area=0.01';
text(chi05+.05,.02,t,'Color','b','FontSize',20);
text(13.25,.02,t1,'Color','b','FontSize',20);
figure(gcf)
hold off