Читать книгу Applied Numerical Methods Using MATLAB - Won Y. Yang - Страница 23
1.1.8.1 Random Number Having Uniform Distribution
ОглавлениеThe numbers in a matrix generated by the MATLAB function ‘ rand(M,N)
’ have uniform probability distribution over the interval [0,1], as described by U(0,1). The random number x generated by ‘ rand()
’ has the probability density function:
(1.1.1)
whose value is 1 over [0,1] and 0 elsewhere. The average of this standard uniform number x is
(1.1.2)
and its variance or deviation is
(1.1.3)
If you want another random number y with uniform distribution U(a,b), transform the standard uniform number x as follows:
(1.1.4)
For practice, we make a vector consisting of 1000 standard uniform numbers, transform it to make a vector of numbers with uniform distribution U(−1,+1), and then draw the histograms showing the shape of the distribution for the two uniform number vectors (Figure 1.5a1,a2) by running the following block of MATLAB statements.
Figure 1.5 Distribution (histogram) of noise generated by the rand()
/ randn()
command.
%nm01f05a.m N=1e4; x=rand(N,1); % An Nx1 noise vector with U(0,1) mx=mean(x); sgm2=sum((x-mx).̂2)/(N-1), var(x) subplot(221), M=20; hist(x,M) % Histogram having M=20 bins a=-1; b=1; y=(b-a)*x+a; %Eq.(1.1.4): 1000x1 noise vector with U(-1,1) subplot(222), hist(y,M) % Histogram