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/statistics/code/gaussianpdf.m

31 lines
832 B
Matlab

% plot Gaussian pdf:
dx=0.1;
x = [-4.0:dx:4.0];
p = exp(-0.5*x.^2)/sqrt(2.0*pi);
hold on
plot(x, p, 'linewidth', 10)
% show area of integral:
area(x((x>=x1)&(x<=x2)), p((x>=x1)&(x<=x2)), 'FaceColor', 'r' )
hold off
% compute integral between x1 and x2:
x1=1.0;
x2=2.0;
P = sum(p((x>=x1)&(x<x2)))*dx;
disp( [ 'The integral between ', num2str(x1, 1), ' and ', num2str(x2, 1), ' is ', num2str(P, 3) ] );
% draw random numbers:
%r = randn( 10000, 1 );
%hist(r,x,1.0/dx)
% check P:
Pr = sum((r>=x1)&(r<x2))/length(r);
disp( [ 'The probability of getting a number between ', num2str(x1, 1), ' and ', num2str(x2, 1), ' is ', num2str(Pr, 3) ] );
% infinite integral:
P = sum(p)*dx;
disp( [ 'The integral between -infinity and +infinity is ', num2str(P, 3) ] );
disp( [ 'I.e. the probability to get any number is ', num2str(P, 3) ] );