Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
help with assigment

Subject: help with assigment

From: percy sears

Date: 24 Jun, 2011 19:48:04

Message: 1 of 8

hello i a new comer. can anyone help me with this assignment .
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 fifty times with n = 10000. Plot the 100 estimated probability
values along with the theoretical result of P[X+Y=7].
2.
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.

Subject: help with assigment

From: Wayne King

Date: 24 Jun, 2011 20:12:05

Message: 2 of 8

"percy sears" wrote in message <iu2plk$55e$1@newscl01ah.mathworks.com>...
> hello i a new comer. can anyone help me with this assignment .
> 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 fifty times with n = 10000. Plot the 100 estimated probability
> values along with the theoretical result of P[X+Y=7].
> 2.
> 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.

Hi Percy, You should show the code you've written and where you are getting stuck.
People on this forum are very generous in helping you with specific code, but are generally not receptive to posts that just contain the problem statement.

Wayne

Subject: help with assigment

From: percy sears

Date: 24 Jun, 2011 20:21:02

Message: 3 of 8

"Wayne King" <wmkingty@gmail.com> wrote in message <iu2r2l$9nn$1@newscl01ah.mathworks.com>...
> "percy sears" wrote in message <iu2plk$55e$1@newscl01ah.mathworks.com>...
> > hello i a new comer. can anyone help me with this assignment .
> > 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 fifty times with n = 10000. Plot the 100 estimated probability
> > values along with the theoretical result of P[X+Y=7].
> > 2.
> > 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.
>
> Hi Percy, You should show the code you've written and where you are getting stuck.
> People on this forum are very generous in helping you with specific code, but are generally not receptive to posts that just contain the problem statement.
>
> Wayne

For question 1

n=1000;
m=10000;


T=100;

for k=1:m
    
    P=0;
    X=ceil(6*rand(1,n));
    Y=ceil(6*rand(1,n));
    
for j=1:n
        if X(j)+Y(j) == 7;
            P=P+1;
        end
    end
    
    disp('Trial: ')
    k;
    disp('Success Rate: ')
    P=P/n;
    
    Xaxis(k) = k;
    Yaxis(k) = P;
    T=T+P;
end

disp('Total Average: ')
T/m;

plot(Xaxis,Yaxis);

clear;

Subject: help with assigment

From: percy sears

Date: 24 Jun, 2011 20:40:05

Message: 4 of 8

these are the codes i wrote for question 1
For question 1

n=1000;
m=10000;


T=100;

for k=1:m
    
    P=0;
    X=ceil(6*rand(1,n));
    Y=ceil(6*rand(1,n));
    
for j=1:n
        if X(j)+Y(j) == 7;
            P=P+1;
        end
    end
    
    disp('Trial: ')
    k;
    disp('Success Rate: ')
    P=P/n;
    
    Xaxis(k) = k;
    Yaxis(k) = P;
    T=T+P;
end

disp('Total Average: ')
T/m;

plot(Xaxis,Yaxis);

clear;

Subject: help with assigment

From: Wayne King

Date: 24 Jun, 2011 21:11:04

Message: 5 of 8

"percy sears" wrote in message <iu2rje$b98$1@newscl01ah.mathworks.com>...
> "Wayne King" <wmkingty@gmail.com> wrote in message <iu2r2l$9nn$1@newscl01ah.mathworks.com>...
> > "percy sears" wrote in message <iu2plk$55e$1@newscl01ah.mathworks.com>...
> > > hello i a new comer. can anyone help me with this assignment .
> > > 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 fifty times with n = 10000. Plot the 100 estimated probability
> > > values along with the theoretical result of P[X+Y=7].
> > > 2.
> > > 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.
> >
> > Hi Percy, You should show the code you've written and where you are getting stuck.
> > People on this forum are very generous in helping you with specific code, but are generally not receptive to posts that just contain the problem statement.
> >
> > Wayne
>
> For question 1
>
> n=1000;
> m=10000;
>
>
> T=100;
>
> for k=1:m
>
> P=0;
> X=ceil(6*rand(1,n));
> Y=ceil(6*rand(1,n));
>
> for j=1:n
> if X(j)+Y(j) == 7;
> P=P+1;
> end
> end
>
> disp('Trial: ')
> k;
> disp('Success Rate: ')
> P=P/n;
>
> Xaxis(k) = k;
> Yaxis(k) = P;
> T=T+P;
> end
>
> disp('Total Average: ')
> T/m;
>
> plot(Xaxis,Yaxis);
>
> clear;

Hi Percy, Welcome to MATLAB, I'll just comment on the first part of your problem.

> 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 fifty times with n = 10000. Plot the 100 estimated probability
> values along with the theoretical result of P[X+Y=7].

Each time you call

n = 10000;
X = ceil(6*rand(1,n));

you are getting a simulation of 10,000 rolls of a single die.

Y = ceil(6*rand(1,n));

is another (independent) 10,000 rolls.

The problem is asking you to form another random variable, which is the sum of the two rolls. So you have two vectors of equal size, how would you form their sum in MATLAB? That is the random variable you are looking for. To determine the probability, you have 10,000 trials. Think about how you would count the number of 7's you observe and divide that by the total number of trials.
Then you are asked to repeat that experiment 50 times (with a for loop).

With MATLAB array operations, each experiment should only be about 3-4 lines of code. Read the MATLAB Getting Started Guide for help on basic MATLAB array operations.

Wayne

Subject: help with assigment

From: Roger Stafford

Date: 24 Jun, 2011 21:51:04

Message: 6 of 8

"Wayne King" <wmkingty@gmail.com> wrote in message <iu2uh8$jj0$1@newscl01ah.mathworks.com>...
> Hi Percy, Welcome to MATLAB, I'll just comment on the first part of your problem.
> .....
> Each time you call
>
> n = 10000;
> X = ceil(6*rand(1,n));
>
> you are getting a simulation of 10,000 rolls of a single die.
>
> Y = ceil(6*rand(1,n));
>
> is another (independent) 10,000 rolls.
>
> The problem is asking you to form another random variable, which is the sum of the two rolls. So you have two vectors of equal size, how would you form their sum in MATLAB? That is the random variable you are looking for. To determine the probability, you have 10,000 trials. Think about how you would count the number of 7's you observe and divide that by the total number of trials.
> Then you are asked to repeat that experiment 50 times (with a for loop).
>
> With MATLAB array operations, each experiment should only be about 3-4 lines of code. Read the MATLAB Getting Started Guide for help on basic MATLAB array operations.
>
> Wayne
- - - - - - - - - - -
  He has performed the basic request properly, Wayne, except that the wrong counts were used. The P values placed in 'Yaxis' are correct for the n that was used. The quantity n should have been 10000 instead of 1000 and the count m should be 50 instead of 10000.

  The problem is stated somewhat ambiguously in the request: "Plot the 100 estimated probability values." There are only 50 joint probability estimates involved in determining when X+Y=7 and the plot that is asked for should show just fifty points.

Roger Stafford

Subject: help with assigment

From: percy sears

Date: 24 Jun, 2011 22:23:04

Message: 7 of 8

"Roger Stafford" wrote in message <iu30s8$p8f$1@newscl01ah.mathworks.com>...
> "Wayne King" <wmkingty@gmail.com> wrote in message <iu2uh8$jj0$1@newscl01ah.mathworks.com>...
> > Hi Percy, Welcome to MATLAB, I'll just comment on the first part of your problem.
> > .....
> > Each time you call
> >
> > n = 10000;
> > X = ceil(6*rand(1,n));
> >
> > you are getting a simulation of 10,000 rolls of a single die.
> >
> > Y = ceil(6*rand(1,n));
> >
> > is another (independent) 10,000 rolls.
> >
> > The problem is asking you to form another random variable, which is the sum of the two rolls. So you have two vectors of equal size, how would you form their sum in MATLAB? That is the random variable you are looking for. To determine the probability, you have 10,000 trials. Think about how you would count the number of 7's you observe and divide that by the total number of trials.
> > Then you are asked to repeat that experiment 50 times (with a for loop).
> >
> > With MATLAB array operations, each experiment should only be about 3-4 lines of code. Read the MATLAB Getting Started Guide for help on basic MATLAB array operations.
> >
> > Wayne
> - - - - - - - - - - -
> He has performed the basic request properly, Wayne, except that the wrong counts were used. The P values placed in 'Yaxis' are correct for the n that was used. The quantity n should have been 10000 instead of 1000 and the count m should be 50 instead of 10000.
>
> The problem is stated somewhat ambiguously in the request: "Plot the 100 estimated probability values." There are only 50 joint probability estimates involved in determining when X+Y=7 and the plot that is asked for should show just fifty points.
>
> Roger Stafford
correction to the problem the professor email to say it should be 100 times not 50 times as stated.

Subject: help with assigment

From: Wayne King

Date: 25 Jun, 2011 09:54:05

Message: 8 of 8

"southie" wrote in message <iu32o8$e4$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <iu30s8$p8f$1@newscl01ah.mathworks.com>...
> > "Wayne King" <wmkingty@gmail.com> wrote in message <iu2uh8$jj0$1@newscl01ah.mathworks.com>...
> > > Hi Percy, Welcome to MATLAB, I'll just comment on the first part of your problem.
> > > .....
> > > Each time you call
> > >
> > > n = 10000;
> > > X = ceil(6*rand(1,n));
> > >
> > > you are getting a simulation of 10,000 rolls of a single die.
> > >
> > > Y = ceil(6*rand(1,n));
> > >
> > > is another (independent) 10,000 rolls.
> > >
> > > The problem is asking you to form another random variable, which is the sum of the two rolls. So you have two vectors of equal size, how would you form their sum in MATLAB? That is the random variable you are looking for. To determine the probability, you have 10,000 trials. Think about how you would count the number of 7's you observe and divide that by the total number of trials.
> > > Then you are asked to repeat that experiment 50 times (with a for loop).
> > >
> > > With MATLAB array operations, each experiment should only be about 3-4 lines of code. Read the MATLAB Getting Started Guide for help on basic MATLAB array operations.
> > >
> > > Wayne
> > - - - - - - - - - - -
> > He has performed the basic request properly, Wayne, except that the wrong counts were used. The P values placed in 'Yaxis' are correct for the n that was used. The quantity n should have been 10000 instead of 1000 and the count m should be 50 instead of 10000.
> >
> > The problem is stated somewhat ambiguously in the request: "Plot the 100 estimated probability values." There are only 50 joint probability estimates involved in determining when X+Y=7 and the plot that is asked for should show just fifty points.
> >
> > Roger Stafford
> correction to the problem the professor email to say it should be 100 times not 50 times as stated.

So Percy, one experiment with 10,000 trials is:

n = 1e4;
X=ceil(6*rand(1,n));
Y=ceil(6*rand(1,n));
Z = X+Y;

Look at the histogram of that random variable, Z
hist(Z);

Now think about how you count the number of 7's in Z and divide that by number of tosses, 10,000.

Your professor wants you to repeat that basic experiment 100 times.

Wayne

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us