close all % generator slucajnih celih brojeva po uniformnog raspodeli help randi % [1,6] celi brojevi u opegu od 1 do 6 % promenljiva k ce biti matrica dimenzija: % 1 - jedan red % 10 - 10 kolona k = randi ([1,6],1,10) % simulira 10 bacanja kockice % ako je ; na kraju reda ne ispisuje se rezultat na ekranu k = randi ([1,6],1,600000); % 600000 bacanja % crtanje histograma help hist help histc help bar % zakon raspodele za kockicu Hk = histc (k,[1,2,3,4,5,6]); Pk = Hk / length (k); figure (1) bar (1:6,Pk,'hist') l=line([1,6],[1/6,1/6]); set(l,'Color','r') set(l,'LineWidth',4) title('Zakon raspodele') xlabel('elemntrani ishod') ylabel('verovatnoca ishoda') grid % gustina verovatnoce za Gausovu slucajnu promenljivu n = 1000000; % broj elemenata uzorka g = randn(1,n); % generisanje Gausove raspodele - rezultat matrica dimenzija 1 x n [Hg,xg] = hist (g,100); % histogram u 100 tacaka dx = xg(2) - xg(1); % dx je sirina klase (been) fg = Hg / (n*dx); % izracunavanje funkcije gustine verovatnoce figure(2) bar(xg,fg,'hist') x = -4.5:1/100:4.5; y = 1/sqrt(2*pi)*exp(-x.^2/2); hold on plot(x,y,'Color','r','LineWidth',3) title('Gausova raspodela') hold off grid % linearna transformacija Gausove raspodele X = g; a = 4; b = 10; Y = a*X + b; [h,x] = hist (Y,100); dx = x(2) - x(1); f = h / (n*dx); figure(3) bar(x,f,'hist') title('Gausova raspodela posle transformacije') grid Xsr = mean(X) Xvar = var(X) Xstd = std(X) Ysr = mean(Y) Yvar = var(Y) Ystd = std(Y) % kvadratna transformacija Gausove raspodele Y=X.^2; [hy xy]=hist(Y,100); fy=hy/(n*(xy(2)-xy(1))); figure(4) subplot(221),plot(X(1:1000)) subplot(222),bar(xg,fg,'hist') subplot(223),plot(Y(1:1000)) subplot(224),bar(xy,fy,'hist') % sinusna transformacija uniformne raspodele n=100000; X=2*pi*rand(1,n); [hx xx]=hist(X,100); fx=hx/(n*(xx(2)-xx(1))); Y=sin(2*pi*1000+X); [hy xy]=hist(Y,100); fy=hy/(n*(xy(2)-xy(1))); figure(5) subplot(221),plot(X(1:1000)) subplot(222),bar(xx,fx) subplot(223),plot(Y(1:1000)) subplot(224),bar(xy,fy) % transformacija Dekartovih koordinata (x, y) u polarne koordinate (r,w) % transformacija jakobijanom 2 x 2 promenljive n=100000; X=randn(1,n); Y=randn(1,n); R=sqrt(X.^2+Y.^2); W=atan(X./Y); [hr xr]=hist(R,100); fr=hr/(n*(xr(2)-xr(1))); [hw xw]=hist(W,100); fw=hw/(n*(xw(2)-xw(1))); figure(6) subplot(211),bar(xr,fr) title('Raspoela promenljive W') subplot(212),bar(xw,fw) title('Raspoela promenljive R') % transformacija raspodele kroz limiter % y = x, za x>-a i x+a n=100000; X=randn(1,n); Y=zeros(size(X)); a=2; for i=1:n if(X(i)>=a) Y(i)=a; elseif(X(i)<=-a) Y(i)=-a; else Y(i)=X(i); end end [hx xx]=hist(X,100); fx=hx/(n*(xx(2)-xx(1))); [hy xy]=hist(Y,100); fy=hy/(n*(xy(2)-xy(1))); figure(7) subplot(211),bar(xx,fx) title('Raspoela promenljive X') subplot(212),bar(xy,fy) title('Raspoela promenljive Y') % raspodela zbira dve nezavisne slucajne n=100000; X=rand(1,n); Y=rand(1,n); Z=X+Y; [h,x]=hist(Z,100); dx=x(2)-x(1); f=h/(n*dx); figure(1) bar(x,f) title('Raspodela zbira') % raspodela zbira dve nezavisne slucajne n=100000; X=rand(1,n); Y=X; Z=X+Y; [h,x]=hist(Z,100); dx=x(2)-x(1); f=h/(n*dx); figure(9) bar(x,f) title('Raspodela zbira')