26 lines
541 B
Matlab
26 lines
541 B
Matlab
% Gaussian density from histogram:
|
|
x = randn( 1000000, 1 );
|
|
[ n, c ] = hist( x, 100 );
|
|
n = n/sum(n)/(c(2)-c(1));
|
|
bar( c, n );
|
|
hold on;
|
|
% equation p(x):
|
|
xx = -5:0.01:5;
|
|
p=exp(-xx.^2/2.0)/sqrt(2.0*pi);
|
|
plot( xx, p, 'r', 'LineWidth', 3 )
|
|
|
|
% with mean=2 and sigma=0.5:
|
|
mu = 2.0;
|
|
sig = 0.5;
|
|
x = sig*x + mu;
|
|
[ n, c ] = hist( x, 100 );
|
|
n = n/sum(n)/(c(2)-c(1));
|
|
bar( c, n );
|
|
% equation:
|
|
p=exp(-(xx-mu).^2/2.0/sig^2)/sqrt(2.0*pi*sig^2);
|
|
plot( xx, p, 'r', 'LineWidth', 3 )
|
|
hold off;
|
|
xlabel( 'x' );
|
|
ylabel( 'p(x)' );
|
|
title( 'Gaussian distribution' );
|