The binomial distribution is used to model the number of successes out of a fixed number of trials, with each trial having the same probability of success. Here is an example code in MATLAB to generate and plot the binomial distribution of 10 trials with success probability of 0.5:
main.m342 chars15 lines
In this code, we first define the parameters n
and p
. We then use the binopdf
function to generate the probability mass function of the binomial distribution for possible number of successes x
between 0 and n
. Finally, we use the stem
function to plot the distribution.
Note that in the stem
function, we use x
as the X-axis and y
as the Y-axis, as we want to plot the probability of each possible number of successes. We also add labels and a title to the plot, as well as turning on the grid for easier visualization.
gistlibby LogSnag