Random Signals for Engineers using MATLAB and Mathcad Copyright 1999 Springer Verlag NY Example 1.4 We are given a set of N experimental tries whose individual outcome can be {0, 1}. What is the relative freqency of the outcome or event A = {1}. SOLUTION: We first generate N = 100 numbers with value 0 or 1 and call the result vector x and then generate a sequence of {1 0} event and call this sequence vector y x=rand(100,1); y=x >0.5; N In N trials it is relatively easy to count the number of 1's by n x j j 1 The relative frequency may be determined by f = sum(y)/100 f = 0.5300 It is interesting to note that f is not equal to 0.5 as would be expected from the setup of the problem. We can experiment with increasing N or changing the seed of the random number generator. More will be said about the latter option when we discuss random number generation. We can illustrate the effect of N on the ratio by defining a function that only counts the first i samples n i i x j j 1 The relative frequency can be plotted as a function of i or the number of samples taken z=cumsum(y)'./[1:100]; The following arrays are used to assist in plotting z. p=0.5*ones(100,1); u=1:100; We find that the ratio tends to 0.5 as expected. plot(u,z,u,p) ylabel('Average , Frequency Ratio'); xlabel('number of trials') 1 Average , Frequency Ratio 0.9 0.8 0.7 0.6 0.5 0.4 0 10 20 30 40 50 60 number of trials 70 80 90 100 These functions can be used in place of the Matlab built in function cumsum and y=x>0.5 notation: function y=partsum(x,n) xs = 0; for i= 1:n xs = xs+x(i); end y=xs; function y=ifeval(x,a) for i=1:size(x) if x(i)> a y(i)= 0; else y(i) = 1; end end