Empty plots in matlab

Viewed 54

I'm trying to prove that P(Xi) = lim (ni/n) when n->00. I'm trying to simulate a coin toss n times and count the number of tails and plot the resulting frequence f=numoftails/n for different values of n. I get empty plots though and I have no idea what I'm doing wrong.

I have tried several different implementations, none of which seem to work, I will show you my last one:

n = 100         
maxn = 1000
f = zeros (1,maxn-n)                      %initializing
while n < maxn
numoftails = 0;                           %iniztializing
    for c = 1:n
        temp = unidrnd(2)                 % 1 will be head, 2 will be tails
        if temp == 1
            numoftails = numoftails + 1   %counting the tails
        end
    end

f(n-99) = numoftails / n                  %frequency of tails for each n
plot (f(n-99))                          %I use f(n-99) so when I start  from 100 I start filling from f(1)

n = n + 1
end 
1 Answers

I would suggest plotting the entire vector f at the end, since all the probabilities will conveniently be stored in the cells/indices of this array/matrix. If you wish to plot the probabilities one at a time you can use the hold on command after your original plot line. Also plotting the points as a black markers/dots using 'k.' attribute can help with visibility. Currently it seems that maybe only one value was being plotted.

Method 1: Line Plot (plotting entire vector after loop)

Method 1: Line Plot

clf;
n = 100;         
maxn = 1000;
f = zeros (1,maxn-n);                      
while n < maxn
numoftails = 0;                           
    for c = 1:n
        temp = unidrnd(2);                 % 1 will be head, 2 will be tails
        if temp == 1
            numoftails = numoftails + 1;   %counting the tails
        end
    end

f(n-99) = numoftails / n;               
n = n + 1;
end 

plot(f); %or plot(f,'k.')%                   

Method 2: Scatter Plot (plotting within loop)

Method 2: Scatter Plot

clf;
n = 100;         
maxn = 1000;
f = zeros (1,maxn-n);                    
while n < maxn
numoftails = 0;                          
    for c = 1:n
        temp = unidrnd(2);                 %1 will be head, 2 will be tails
        if temp == 1
            numoftails = numoftails + 1;   %counting the tails
        end
    end

f(n-99) = numoftails / n;    

plot(n-99,f(n-99),"k."); 
hold on
n = n + 1;
end
Related