help me please
이전 댓글 표시
[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
Matt Fig
2011년 6월 24일
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
2011년 6월 24일
If the simulation is being run 50 times, then what are the 100 values that are to be plotted ?
채택된 답변
추가 답변 (2개)
Sean de Wolski
2011년 6월 24일
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
Sean de Wolski
2011년 6월 24일
Must be the Fridays; Walter, I learned this engine from you!!
(The sort operation index)
Walter Roberson
2011년 6월 24일
Ah, I didn't notice it was the index you were pulling out.
Very well, carry on. ;-)
Walter Roberson
2011년 6월 24일
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
2011년 6월 24일
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
2011년 6월 24일
Ahh you're right. I'm thinking exclusive. Doh!
Walter Roberson
2011년 6월 24일
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
2011년 6월 24일
Your pub or mine, Sean?
Sean de Wolski
2011년 6월 24일
Yours, I'll be there in an hour!
plot(sum(sum(ceil(rand(10000,50,2)*6),3)==7))
southie
2011년 6월 24일
Matt Fig
2011년 6월 24일
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
2011년 6월 24일
for K = 1:1
data = sum(sum(ceil(rand(10000,50,2)*6),3)==7);
end
plot(data)
Walter Roberson
2011년 6월 24일
Without the ideal part:
plot(mean((ceil(6*rand(50,10000)) + ceil(6*rand(50,10000))) == 7,2))
카테고리
도움말 센터 및 File Exchange에서 Exploration and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!