function central1(p,n,N)
%CENTRAL1: Simulates a binomial random variable
%given its probability p and the number of
%trials N, then compares the results with 
%a standard normal distribution.
%p = probability of heads
%n = number of flips for each simulation
%N = number of simulations of Z_n
%
rng('shuffle')
mu = p; v = p-p^2; %expected value and variance
for k=1:N
    X = round(rand([1,n])+(p-1/2));    
    Z(k) = (sum(X)-n*mu)/(sqrt(v*n));
end
%
%Comparison with standard normal
[counts,centers]=hist(Z,50); %Histogram with 50 bins
hist(Z,50)
hold on
pause
scale = (centers(2)-centers(1))*N; %Area of histogram = binwidth*Total number of points
x = linspace(-4,4,1000);
f=scale/sqrt(2*pi)*exp(-x.^2/2);
plot(x,f,'r','LineWidth',2)
