how to calculate the geometric distribution in matlab

To calculate the geometric distribution in MATLAB, you can use the function geornd. Here is an example code snippet:

main.m
% define the parameters (p = probability of success)
p = 0.2; 
numTrials = 1000;

% generate 1000 random numbers from a geometric distribution with p
X = geornd(p, numTrials, 1);

% plot the histogram
histogram(X);

% calculate the mean and variance of X
mu = 1/p;
sigma2 = (1-p)/(p^2);
287 chars
14 lines

In this example, we generate 1000 random numbers from a geometric distribution with probability of success p=0.2. Then, we plot a histogram of the results and calculate the mean and variance of X.

gistlibby LogSnag