# Probability of tossing a coin twice & MATLAB?

I'm watching this video at 21:20 min. The guy gave an example for tossing a coin four times. At 23:49 min, he said we can plot the probability of tossing a coin (e.i. $n$) for a large number in MATLAB by using this formula $$px(k) = \dbinom{n}{k} p^{k}(1-p)^{n-k} \qquad, k=0,1,...,n$$

Ok now I want to plot his example but I'm facing problems. For his example, $n=4 , k = 0,1,2,3,4$ (what does $k=0$ mean here?). This is my code in Matlab

clear all
clc
outcome = 2; % outcome # of a coin (Head & Tail)
n = 4; % tossing #
k = 0:n; % # of getting head or tail
p = 1/n^outcome; % the total probability of all outcomes (1/16)
i = 0; % iterator for the loop

for i = 0:size(k,2)
Px = nchoosek(n,k(i)) * p^k(i) * (1-p)^(n-k(i));
end

plot(k, Px)


What's wrong with my approach?

-
In the formula you gave (for Bernoulli trials), the parameter $p$ is the probability of a single outcome. For tossing a coin, this should be 1/2. In your code, it should be 1/outcome. –  Hugh Denoncourt Feb 15 '14 at 15:54
@HughDenoncourt, I'm still getting error. How can I apply the formula in case $k =0$? –  CroCo Feb 15 '14 at 16:02

This is the binomial distribution (you can look it up just about anywhere). $k$ is the number of successes (heads in this case), and it is certainly possible to get no successes in four attempts. The $p$ is the probability of success for each trial (it's assumed that each coin-flip has the same probability of success as all the others) and should be 1/2.
I can visualize the problem by drawing a tree of tossing the coin 4 times and one of the case is getting (TTTT), so in this case $k=0$ (I'm assuming $k$ to be the number of getting Head) but How can I apply the formula in case $k=0$? –  CroCo Feb 15 '14 at 16:15
@CroCo plug it in? You get $\binom40\cdot p^0\cdot (1-p)^{4-0} = (1-p)^4$. In matlab, nchoosek(4,0)*p^0*(1-p)^(n-0). They're all well-defined. –  tabstop Feb 15 '14 at 16:17