matlabrandomprobability-densityinverse-transform

Generate random variable with known PDF expression in MATLAB


I know the probability density function (PDF) expression of random variable r as 2r/R^2 where 0<=r<=R. Then, its CDF is r^2/R^2.

Can someone help me to generate random variable r in MATLAB following above distribution?


Solution

  • https://blogs.sas.com/content/iml/2013/07/22/the-inverse-cdf-method.html

    I use the same variables as they use

    f(x) = 2x/R^2

    F(x) = x^2/R^2

    solving for x in the equation F(x) = u

    u*R^2 = x^2

    x = sqrt(u * R^2) v -sqrt(u * R^2)

    in Matlab:

    N=1E5;
    R=1;
    u = rand(1,N);
    x = sqrt(u*R^2); 
    
    histogram(x)