Читать книгу Applied Numerical Methods Using MATLAB - Won Y. Yang - Страница 24
1.1.8.2 Random Number with Normal (Gaussian) Distribution
ОглавлениеThe numbers in a matrix generated by the MATLAB function ‘ randn(M,N)
’ have normal (Gaussian) distribution with average m = 0 and variance σ2 = 1, as described by N(0,1). The random number x generated by ‘ rand()
’ has the probability density function
(1.1.5)
If you want another Gaussian number y with a general normal distribution N(m,σ2), transform the standard Gaussian number x as follows:
(1.1.6)
The probability density function of the new Gaussian number generated by this transformation is obtained by substituting x = (y − m)/σ into Eq. (1.1.5) and dividing the result by the scale factor σ (which can be seen in dx = dy/σ) so that the integral of the density function over the whole interval ( ‐
∞,+∞) amounts to 1.
(1.1.7)
%nm01f05b.m N=1e4; x=randn(N,1); % An Nx1 noise vector with N(0,1) subplot(223), M=20; hist(x,M) % Histogram having M=20 bins f=@(x,m,sgm)exp(-(x-m).̂2/2/sgm̂2)/sqrt(2*pi)/sgm; %Gaussian density ftn [Ns,Cs]=hist(x,20); dx=Cs(2)-Cs(1); % Bin width x_=[-5:0.01:5]; fx=f(x_,0,1); hold on, plot(x_,fx*dx*N,'r:') sgm=1/2; m=1; y=sgm*x+m; % An Nx1 noise vector with N(m,sgm̂2) subplot(224), hist(y,M) % Histogram [Ns,Cs]=hist(y,M); dy=Cs(2)-Cs(1); % Bin width y_=[-5:0.01:5]; fy=f(y_,m,sgm); hold on, plot(y_,fy*dy*N,'r:')
For practice, we make a vector consisting of 1000 standard Gaussian numbers, transform it to make a vector of numbers having normal distribution N(1,1/4), with mean m = 1 and variance σ2 = 1/4, and then draw the histograms for the two Gaussian number vectors (Figure 1.5b1,b2) by running the above block of MATLAB statements.