Random Numbers in Matlab – Part III

This is the final post in our series on random numbers in Matlab. In the first post, we discussed basic random number functions, and in the second post, we discussed the control of random number generation in Matlab and alternatives for applications with stronger requirements. In this post, we will demonstrate how to create probability distributions with the basic rand and randn functions of Matlab. This is useful in many engineering applications, including reliability analysis and communications.

How to Create More Distributions

Although the basic version of Matlab only provides the uniform and normal distributions, other distributions based on these functions can be generated. The log-normal, Rayleigh, and exponential distributions will be produced below as examples.

The log-normal distribution is easily.created by taking Euler’s number (e) to the power of a normal random variable (randn).

>> mu = 0;                %Mean of log(X)
>> sigma = 0.25;          %St. dev. of log(X)
>> N = 1000000;
>> nbins = 100;
>> X = exp(mu + sigma*randn(N,1));
>> hist(X, nbins);
>> xlabel('Value'); ylabel('Count'); 
>> title('Log-normal distribution')

lognormal Random Numbers in Matlab – Part III

The Rayleigh distribution is commonly used to model wireless communication channels and can be constructed easily in Matlab. A Rayleigh random variable is the square root of the sum of the squares of two independent normal distributions with zero mean (randn).

>> sigma = 0.5;        %Scale parameter
>> N = 1000000;
>> nbins = 100;
>> X1 = sigma*randn(N,1);
>> X2 = sigma*randn(N,1);
>> R = sqrt(X1.^2 + X2.^2);
>> hist(R, nbins);
>> xlabel('Value'); ylabel('Count'); 
>> title('Rayleigh distribution')

rayleigh Random Numbers in Matlab – Part III

The exponential distribution models the time between events in a Poisson process and can be created using the uniform distribution (rand).

>> lambda = 0.1;       %Rate parameter = 1/mean = 1/stdev
>> N = 1000000;
>> nbins = 100;
>> X = rand(N,1);
>> E = -log(X)/lambda;
>> hist(E, nbins);
>> xlabel('Value'); ylabel('Count'); 
>> title('Exponential distribution')

exponential Random Numbers in Matlab – Part III

As shown in this post, creating distributions on the basis of the simple Matlab functions rand and randn is easy and requires only a few lines of code. This is highly useful for a wide range of engineering and scientific applications. I hope these posts have helped you learn about the Matlab random number functions and will enable you to apply them to your own work.

lg share en Random Numbers in Matlab – Part III

This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.

Matlab Geeks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>