help me please

10 views (last 30 days)
southie
southie on 24 Jun 2011
[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 Comments
Matt Fig
Matt Fig on 24 Jun 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
Walter Roberson on 24 Jun 2011
If the simulation is being run 50 times, then what are the 100 values that are to be plotted ?

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 24 Jun 2011
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.

More Answers (2)

Sean de Wolski
Sean de Wolski on 24 Jun 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 Comments
Matt Fig
Matt Fig on 24 Jun 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
Walter Roberson on 24 Jun 2011
for K = 1:1
data = sum(sum(ceil(rand(10000,50,2)*6),3)==7);
end
plot(data)

Sign in to comment.


Walter Roberson
Walter Roberson on 24 Jun 2011
Without the ideal part:
plot(mean((ceil(6*rand(50,10000)) + ceil(6*rand(50,10000))) == 7,2))

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!