home
N = 1000;
x = rand(N,1);
y = rand(N,1);
D = (x.^2 + y.^2);
pi_mc = 4 * sum( D<=1 ) / N
plot( x, y, '.' )
hold on
ang = linspace(0,pi/2,1000);
plot( cos(ang), sin(ang), '--k' )
axis equal
xlim( [0 1] )
ylim( [0 1] )
figure
plot( x, y, 'xr' )
hold on
plot( cos(ang), sin(ang), '--k', 'markersize', 3 )
plot( cos(ang), sin(ang), '--k', 'linewidth', 3 )
figure
plot( x(D<=1), y(D<=1), 'xb' )
hold on
plot( x(D>1), y(D>1), 'xr' )
plot( cos(ang), sin(ang), '--k', 'linewidth', 3 )
home
clear
Y = randn( 1e4, 1 );
figure, plot( Y, '*' )
hold on
plot( Y( Y>=-1 & Y<=1 ), '*r' )
ind_1 = find( Y>=-1 & Y<=1 );
plot( ind_1, Y(ind_1), '*r' )
ind_1 = find( abs(Y)<=1 );
ind_2 = find( abs(Y)>1 & abs(Y)<=2 );
plot( ind_2, Y(ind_2), '*g' )
ind_3 = find( abs(Y)>2 & abs(Y)<=3 );
plot( ind_3, Y(ind_3), '*k' )
min( Y )
max( Y )
find( Y==min(Y) )
Y(1093)
figure, plot( Y(1094:1100), '*' )
% de volta ao pi por Monte Carlo
clear
N = 1000;
x = rand(N,1);
y = rand(N,1);
D = (x.^2 + y.^2);
pi_mc = 4 * sum( D<=1 ) / N
ang = linspace(0,pi/2,1000);
figure
plot( x(D<=1), y(D<=1), 'xb' )
hold on
plot( x(D>1), y(D>1), 'xr' )
plot( cos(ang), sin(ang), '--k', 'linewidth', 3 )
dir
exemplo_plota_pi_por_MC
close all
dir
plota_pi_MC( 100 )
plota_pi_MC( 1e4 )
plota_pi_MC( 1e6 )
tic, plota_pi_MC( 1e6 ), toc
tic, plota_pi_MC( 1e7 ), toc
tic, plota_pi_MC( 1e8 ), toc
close all
plota_pi_MC( 1e4 )
PI = plota_pi_MC( 1e4 )
for j=1:1e3
PI(j) = plota_pi_MC( 1e4 );
end
figure, plot( PI, '*' )
ystats
home
clear
p = [ .1 .7 .2 ]
C = cumsum( p )
r = rand
gera_inteiro( [0.1 0.7 0.2] )
Z = gera_inteiro( [0.1 0.7 0.2], 1e4 );
length( Z(Z==1) )
length( Z(Z==2) )
length( Z(Z==3) )
