This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/linearalgebra/code/gaussian.m
2014-11-12 18:39:02 +01:00

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' );