% LMEx030508_4th.m
% Larsen & Marx 2006, p. 183 Introduction to Mathematical Statistics,
% 4th edition
% Written by E. Gallagher, EEOS, UMASS/Boston
% What is the median lifespan for a lightbulb with an average lifespan of
% 1000 hours (with lifetime exponentially distributed)
% Use Matlab's symbolic math to solve the definite integral symbolically
syms y m; int(0.001*exp(-0.001*y),y,0,m)
% use Matlab's minimization routine to find the x that solves the 
% equation.
medx=fzero('1-exp(-0.001*x)-0.5',0)
y=0:1e4;
lambda=1000;
fyy=exppdf(y,lambda);  % exponential pdf
plot(y,fyy,'r','LineWidth',2)
ylabel('Probability density','FontSize',16)
xlabel('Hour when light bulb fails','FontSize',16);
title('Example 3.5.8','FontSize',20);figure(gcf)
pause
ax1=gca; % get the handle for the bar chart's axes;
hold on
axis([0 1e4 0 1e-3])
xf=medx:100:1e4;yf=exppdf(xf,lambda); ;
fill([medx xf 1e4]',[0 yf 0]',[.8 .8 1])
s=sprintf('Median = %6.2f h',medx)
text(medx+60,exppdf(medx,lambda),s, 'FontSize',18)
text(1000+60,exppdf(1000,lambda),'Mean = 1000 h', 'FontSize',18)
% This plot simply puts a vertical line on the graph
plot([1000 1000],[0 exppdf(1000,lambda)],'--k','LineWidth',1);
figure(gcf);pause
hold off