help me please

10 ビュー (過去 30 日間)
southie
southie 2011 年 6 月 24 日
[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
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
Walter Roberson 2011 年 6 月 24 日
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 件)

Sean de Wolski
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 件のコメント
Matt Fig
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
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
Walter Roberson 2011 年 6 月 24 日
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