help me please
Mostrar comentarios más antiguos
[EDIT: Fri Jun 24 23:04:46 UTC 2011 - Reformat - MKF]
Simulation of tossing a pair of fair dice can be done using the following two MATLAB lines
X=ceil(6*rand(1,n));
Y=ceil(6*rand(1,n));
where n is the number of tosses. Write a MATLAB script to determine the probability P[X+Y=7]. Use a “for” loop to run simulation one hundred times with n = 10000. Plot the 100 estimated probability values along with the theoretical result of P[X+Y=7].
That what i have done.
n=10000
m=100
B=0
for k=1:n
P=0
X=ceil(6*rand(1,n));
Y=ceil(6*rand(1,n));
Z=X+Y;
for i=1:m
if Z(i)==7;
P=P+1;
end
end
disp('trial');
k
disp('success rate');
P=P/n;
Xaxis(k) = k;
Yaxis(k) = P;
B=B+P;
end
disp('Total Average: ')
B/m
plot(Xaxis,Yaxis);
clear;
______________________________________________________________
Write a MATLAB script to do the following a. Create 10000 random variables uniformly distributed between 2 and 4. b. Create a histogram to approximate the actual probability density function. c. Superimpose the actual probability density function to the above histogram.
2 comentarios
Matt Fig
el 24 de Jun. de 2011
What have you done so far?
See this guide:
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Walter Roberson
el 24 de Jun. de 2011
If the simulation is being run 50 times, then what are the 100 values that are to be plotted ?
Respuesta aceptada
Más respuestas (2)
Sean de Wolski
el 24 de Jun. de 2011
X = rand(10000,50,6);
[v,v] = sort(X,3);
plot([sum(sum(v(:,:,1:2),3)==7);repmat(1/5*10000,1,50)]');
legend('Experimental','Ideal');
For loops are boring
11 comentarios
Sean de Wolski
el 24 de Jun. de 2011
Must be the Fridays; Walter, I learned this engine from you!!
(The sort operation index)
Walter Roberson
el 24 de Jun. de 2011
Ah, I didn't notice it was the index you were pulling out.
Very well, carry on. ;-)
Walter Roberson
el 24 de Jun. de 2011
Ideal should be 1/6 not 1/5:
(1,6), (2,5), (3,4), (4,3), (5,2), (6,1)
That's 6 possibilities out of (6*6), so probability is 1/6
Sean de Wolski
el 24 de Jun. de 2011
I disagree. You are guaranteed to get a number 1:6 (the first number); one of the next five numbers you choose will add to seven with the first.
Sean de Wolski
el 24 de Jun. de 2011
Ahh you're right. I'm thinking exclusive. Doh!
Walter Roberson
el 24 de Jun. de 2011
No.
>> mean(mean((ceil(6*rand(5000,10000)) + ceil(6*rand(5000,10000))) == 7,2))
ans =
0.1666153
You generate the first number; it is certain to be in range. 7 minus that number is going to be a single number in the range 1 to 6, which is exactly the possibilities for a die. There is therefore a 1 in 6 probability that the second dice will come out equal to the value needed to make the total be 7.
Walter Roberson
el 24 de Jun. de 2011
Your pub or mine, Sean?
Sean de Wolski
el 24 de Jun. de 2011
Yours, I'll be there in an hour!
plot(sum(sum(ceil(rand(10000,50,2)*6),3)==7))
southie
el 24 de Jun. de 2011
Matt Fig
el 24 de Jun. de 2011
percy, show what you have done so far! Don't put it in a comment or a new answer, edit your original post by clicking on the Edit link, then show your code.
Walter Roberson
el 24 de Jun. de 2011
for K = 1:1
data = sum(sum(ceil(rand(10000,50,2)*6),3)==7);
end
plot(data)
Walter Roberson
el 24 de Jun. de 2011
Without the ideal part:
plot(mean((ceil(6*rand(50,10000)) + ceil(6*rand(50,10000))) == 7,2))
Categorías
Más información sobre Exploration and Visualization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!