Borrar filtros
Borrar filtros

Hello, I wish for someone to help me explain (with a code example) how i can generate a random variable using Monte Carlo simulation.with loop for and if ,Am just a basic user of matlab,I will be grateful to get a response

2 visualizaciones (últimos 30 días)
Hello, I wish for someone to help me explain (with a code example) how i can generate a random variable using Monte Carlo simulation.with loop for and if ,Am just a basic user of matlab,I will be grateful to get a response

Respuesta aceptada

Walter Roberson
Walter Roberson el 31 de Ag. de 2020
N = 100000;
count = 0;
R = 5;
for K = 1 : N
r = R * rand(1,2);
if sqrt(r(1)^2 + r(2)^2) <= R
count = count + 1;
end
end
pi_approximation = 4 * (count / N);
  4 comentarios
Walter Roberson
Walter Roberson el 31 de Ag. de 2020
N = 10000;
hits = 0;
misses = 0;
R = 5;
subplot(1,2,1)
axes equal
h = animatedline('Linestyle', 'none', 'marker', '*');
for K = 1 : N
r = R * rand(1,2);
if sqrt(r(1)^2 + r(2)^2) <= R
hits = hits + 1;
addpoints(h,r(1), r(2));
if mod(count, 25); drawnow; end
else
misses = misses + 1;
end
end
pi_approximation = 4 * (count / N);
subplot(1,2,2)
bar([hits, misses]);
xticks([1 2]);
xticklabels({'hits', 'misses'});

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Monte-Carlo en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by