Chapter 23

MATLAB Code Chapter 1 number=0; for i=1:4 % set up simulation for 4 coin tosses if rand(1,1)bincenters(k)-0.5/2 ... & x(i)2 count=count+1; end end probest=count/M

randn(’state’,0) M=100; meanest=0; x=randn(M,1); for i=1:M meanest=meanest+(1/M)*x(i); end meanest

randn(’state’,0)

3 M=100; meanest=0; x=randn(M,1); for i=1:M meanest=meanest+(1/M)*x(i)^2; end meanest

A=[0.1:0.1:5]’; for k=1:length(A) error=0; for i=1:1000 w=randn(1,1); if A(k)/2+w3 % check to see if sinusoid sample exceeds 3 s(k,i)=3; % if yes, then clip elseif s(k,i)0 event(ntrial)=1; % event occurs if one or more birthdays same end end prob=sum(event)/10000 Chapter 4

5 Chapter 5 for i=1:M u=rand(1,1); if u0.2 & u0.8 x(i,1)=3; end end Chapter 6 % PMFdata.m % % This program generates the outcomes for N trials % of an experiment for a discrete random variable. % Uses the method of Section 5.9. % It is a function subprogram. % % Input parameters: % % N - number of trials desired % xi - values of x_i’s of discrete random variable (M x 1 vector) % pX - PMF of discrete random variable (M x 1 vector) % % Output parameters: % % x - outcomes of N trials (N x 1 vector) % function x=PMFdata(N,xi,pX) M=length(xi);M2=length(pX); if M~=M2 message=’xi and pX must have the same dimension’ end for k=1:M ; % see Section 5.9 and Figure 5.14 for approach used here if k==1 bin(k,1)=pX(k); % set up first interval of CDF as [0,pX(1)] else

6

CHAPTER 23. MATLAB CODE bin(k,1)=bin(k-1,1)+pX(k); % set up succeeding intervals % of CDF end end u=rand(N,1); % generate N outcomes of uniform random variable for i=1:N % determine which interval of CDF the outcome lies in % and map into value of xi if u(i)>0&u(i)bin(k-1)&u(i)