47 lines
1.4 KiB
Matlab
47 lines
1.4 KiB
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 );
|
|
|
|
%% (e) probability of small ranges
|
|
nr = 50;
|
|
xmax = 3.0
|
|
xs = zeros(nr, 1); % size of integration interval
|
|
Ps = zeros(nr, 1); % storage
|
|
for i = 1:nr
|
|
% upper limit goes from 4.0 down to 0.0:
|
|
xupper = xmax*(nr-i)/nr;
|
|
xs(i) = xupper;
|
|
% integral from 0 to xupper:
|
|
Ps(i) = sum(pg((xx>=0.0)&(xx<=xupper)))*dx;
|
|
end
|
|
plot( xs, Ps, 'linewidth', 3 )
|
|
xlim([0 xmax])
|
|
ylim([0 0.55])
|
|
xlabel('Integration interval')
|
|
ylabel('Probability')
|
|
fprintf('The probability P(0.1234) = %.4f\n\n', sum(x == 0.1234)/length(x) );
|
|
savefigpdf(gcf, 'normprobs.pdf', 12, 8);
|