%LMFig030401_4th
% Larsen & Marx (2006) Figure 3.4.1. p. 162 
% Written by Eugene.Gallagher@umb.edu
% Revised 1/4/2011
pfail=0.905; 
k=1:21;pxk=pfail.^(k-1)*(1-pfail)
bar(k,pxk,1);xlim([0.5 21.5]);xlabel('Hour when monitor first fails, k');
ylabel('p_x(k)'),title('Figure 3.4.1');figure(gcf)
pause
% Now plot Figure 3.4.2
y=0.1*exp(-0.1*k);
bar(k,pxk,1);xlim([0.5 21.5]);
ax1=gca; % get the handle for the bar chart's axes;
h2=line(k,y,'Color','r','Linewidth',3);
xlabel('Hour when monitor first fails, k');
ylabel('p_x(k)'),title('Figure 3.4.2');
text(7.0943, 0.0579, 'y=0.1 e^{-0.1x}', 'FontSize',14)

% Probability that the monitor fails in the 1st 4 hours
% The book must be wrong in that the sum must be from 1 to 4 to produce
% 0.3292. This is NOT the same as the integral function to 4 decimal places
% Note that the discrete form is not a pdf if summed from 0
% but k=1:100;pxk=pfail.^(k-1)*(1-pfail);sum(pxk) sums to 1.0
k=1:4;P1X4=sum(pfail.^(k-1)*(1-pfail))
% Use the symbolic math toolbox for definite integral
syms x; s=int(0.1*exp(-0.1*x), 0, 4)
% This produces a string result, s, which can be evaluated to produce
% a numerical result
eval(s)
