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/exercises/normprobs.m

27 lines
921 B
Matlab

%% (a) generate normal distributed random numbers:
n = 10000;
x = randn( n, 1 );
%% (b)
nsig = sum((x>=-1.0)&(x<=1.0));
Psig = nsig/length(x);
fprintf('%d of %d data elements, i.e. %.2f%% are contained in the interval -1 to +1\n\n', ...
nsig, length(x), 100.0*Psig );
%% (c)
dx = 0.01;
xx = -10:dx:10; % x-values
pg = exp(-0.5*xx.^2)/sqrt(2*pi); % y-values Gaussian pdf
% integral over the whole range:
P = sum(pg)*dx;
fprintf( 'Integral over the Gaussian pdf is %.3f\n', P );
% integral from -1 to 1:
P = sum(pg((xx>=-1.0)&(xx<=1.0)))*dx; % we need to use xx, not the random numbers x!
fprintf( 'Integral over the Gaussian pdf from -1 to 1 is %.4f\n', P );
%% (d)
P = sum(pg((xx>=-2.0)&(xx<=2.0)))*dx;
fprintf( 'Integral over the Gaussian pdf from -2 to 2 is %.4f\n', P );
P = sum(pg((xx>=-3.0)&(xx<=3.0)))*dx;
fprintf( 'Integral over the Gaussian pdf from -3 to 3 is %.4f\n\n', P );