59 lines
1.1 KiB
Matlab
59 lines
1.1 KiB
Matlab
%% a) 1000 normal distributed random numbers:
|
|
x = randn(1000, 1);
|
|
|
|
%% b) probability of 0<=x<0.5:
|
|
n = sum(x>=0.0 & x<0.5);
|
|
P = n/length(x);
|
|
fprintf('probability 0<=x<0.5 is %g\n', P);
|
|
|
|
%% c) probability for decreasing intervals:
|
|
upper = [0.0:0.01:4.0];
|
|
P = zeros(length(upper), 1);
|
|
for k=1:length(upper)
|
|
P(k) = sum((x>=0) & (x<upper(k)))/length(x);
|
|
end
|
|
subplot(1, 2, 1);
|
|
plot(upper, P, 'linewidth', 2);
|
|
ylim([0 0.5]);
|
|
xlabel('x_{upper}');
|
|
ylabel('P(0<=x<x_{upper})');
|
|
|
|
%% d) histogram with for loop:
|
|
x = randn(1000, 1);
|
|
bw = 0.5;
|
|
bins=[-5:bw:5];
|
|
n = zeros(length(bins), 1);
|
|
for k=1:length(bins)
|
|
n(k) = sum((x>=bins(k)-bw/2) & (x<bins(k)+bw/2));
|
|
end
|
|
p = n/sum(n)/bw;
|
|
subplot(1, 2, 2);
|
|
bar(bins, p);
|
|
|
|
%% e) gaussian pdf:
|
|
hold on;
|
|
xx = [bins(1):0.01:bins(end)];
|
|
gauss = exp(-0.5*xx.^2.0)/sqrt(2*pi);
|
|
plot(xx, gauss, 'r', 'linewidth', 2);
|
|
hold off;
|
|
xlim([-5 5])
|
|
xlabel('x');
|
|
ylabel('p(x)');
|
|
|
|
%% f) hist
|
|
x = randn(1000, 1);
|
|
bw = 0.5;
|
|
bins=[-5:bw:5];
|
|
n = hist(x, bins);
|
|
p = n/sum(n)/bw;
|
|
subplot(1, 2, 2);
|
|
bar(bins, p);
|
|
hold on;
|
|
plot(xx, gauss, 'r', 'linewidth', 2);
|
|
hold off;
|
|
xlim([-5 5])
|
|
xlabel('x');
|
|
ylabel('p(x)');
|
|
|
|
savefigpdf(gcf, 'normhist.pdf', 14, 6);
|