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 个评论

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
If the simulation is being run 50 times, then what are the 100 values that are to be plotted ?

请先登录,再进行评论。

 采纳的回答

Matt Fig
Matt Fig 2011-6-24
Thanks for showing some work. Here is how one could approach the first task:
n = 10000;
m = 100;
PROB = zeros(1,m);
for ii = 1:m
X = ceil(6*rand(1,n));
Y = ceil(6*rand(1,n));
Z = X+Y;
PROB(ii) = sum(Z==7)/n;
end
plot(1:m,PROB,'b*')
hold on
line([1 m],mean(PROB)*ones(1,2),'color','r')
line([1 m],[1 1]./6,'color','k')
legend({'Trial Values';'Trials Mean';'Theory'})
The second task uses some skill learned in the first, so try to understand what I did and what went wrong in what you did. Also, look at the MATLAB HIST and HISTC functions.

更多回答(2 个)

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 个评论

Must be the Fridays; Walter, I learned this engine from you!!
(The sort operation index)
Ah, I didn't notice it was the index you were pulling out.
Very well, carry on. ;-)
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
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.
Ahh you're right. I'm thinking exclusive. Doh!
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.
Your pub or mine, Sean?
Yours, I'll be there in an hour!
plot(sum(sum(ceil(rand(10000,50,2)*6),3)==7))
could you show me using the for loop the professor specify that we use for loop
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.
for K = 1:1
data = sum(sum(ceil(rand(10000,50,2)*6),3)==7);
end
plot(data)

请先登录,再进行评论。

Without the ideal part:
plot(mean((ceil(6*rand(50,10000)) + ceil(6*rand(50,10000))) == 7,2))

Community Treasure Hunt

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

Start Hunting!

Translated by