简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM