MATLAB for uniform random variables

In summary: Then you can compute the Z array with a single line of code, which I'll leave for you to figure out. In summary, the problem involves creating random samples for X and Y, computing the corresponding Z values, plotting a histogram of the Z values, and estimating the moments of Z. The key is to use the correct Matlab functions and syntax to accomplish these tasks.
  • #1
electroissues
7
0

Homework Statement



Given 2 independent uniform random variables X,Y = U [0,1], consider the random variables Z = g (X,Y) for g = (x,y) = sqrt (-2ln(x) . cos(2piy). Since finding the distribution of g(X,Y) analytically is quite tough, I need to generate MATLAB program for

1 - 10,000 uniformly distributed random samples for X and Y
2 - For each sample of X and Y, compute Z= g(X,Y)
3 - Draw a histogram over the resulting samples in Z
4 - Estimate the moments mZ for n = 1,2...6.




The Attempt at a Solution



I tried for the first solution and I'm hopelessly stuck. MATLAB gives me an error saying "Attempted to access (0,1); index must be a positive integer or logical."

I don't understand where I'm going wrong.

Please help!
 
Physics news on Phys.org
  • #2
Please post exactly what Matlab command you have typed which produces that error.
 
  • #3
I tried to put the formula into MATLAB and this was my program

x= 0;
>> y= 1;
>> rand (1, 1000);
>> g= sqrt((-2*log(x)) * cos((2*pi*y)));
>> plot(g)

Actually, for this, I get a blank plot.
Please point out the mistake.
 
  • #4
Your error message came from an attempt to access the (0, 1) element of a an array.

Before starting to code, you should take a look at the Matlab documentation to learn how to use such functions as rand, log, and plot. In the code you show, x is 0, and your fourth line is attempting to take the log of 0

http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf
User documentation

I can't really give you much help, since your code is a VERY LONG way from doing what you need it to do.
 
  • #5
Thanks for your comments but I need to point out that's the question in the first place. So there is no chance I can take a different value.

How do you suggest I define the vectors then? One of the vector (x) is multiplied with the log and the second vector (y) is multiplied with cos (2pi). And the question says X (note the upper case) = U (0,1)
 
  • #6
electroissues said:
Thanks for your comments but I need to point out that's the question in the first place. So there is no chance I can take a different value.
In your first post you said that X is uniformly distributed in [0, 1]. Below, you have changed that to say that X is uniformly distributed in (0, 1). The first interval includes 0 (and 1, but that's not a problem); the second interval doesn't include 0.
electroissues said:
How do you suggest I define the vectors then? One of the vector (x) is multiplied with the log and the second vector (y) is multiplied with cos (2pi).
NO, it's NOT "multiplied with log" and NO, it's NOT "multiplied with cos (2pi)." These are functions, not numbers that you multiply by.
electroissues said:
And the question says X (note the upper case) = U (0,1)

You can define your vectors X and Y as one-dimensional arrays with 10,000 elements or as two-dimensional arrays with 100 rows and 100 columns. It doesn't make any difference, but you need to learn how to work with vectors and/or arrays in MATLAB. The first link I gave in my earlier post can show you how to work with arrays.
 
  • #7
Thanks, Mark. I read the documentation.

In my first post, the question has U= [0,1] with 10,000 random variables.

Can you please tell me how I shud use this in the rand function? Also, please help with the substitution for x and y if I define the rand as "rand [1, 10000]"

Thanks again.
 
  • #8
electroissues said:
Thanks, Mark. I read the documentation.

In my first post, the question has U= [0,1] with 10,000 random variables.
Yes, I'm aware of what you had in your first post. My comment was that in post #5 you said U(0, 1), so which is it?
electroissues said:
Can you please tell me how I shud use this in the rand function? Also, please help with the substitution for x and y if I define the rand as "rand [1, 10000]"
Here's from the documentation for rand: "r = rand(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1). rand(m,n) or rand([m,n]) returns an m-by-n matrix."

You can do this to create a 100 x 100 array of random numbers in the open interval (0, 1):
X = rand(100)

Or you can do this to create a vector of length 10000 of numbers from the same interval:
X = rand(1, 10000)

Whatever you do to create your X array, do the same to create the Y array.
 

1. What is a uniform random variable in MATLAB?

In MATLAB, a uniform random variable is a type of distribution that generates random numbers within a specified range with equal probability. This means that all values within the range have an equal chance of being selected.

2. How can I generate a uniform random variable in MATLAB?

To generate a uniform random variable in MATLAB, you can use the "rand" function. This function takes in two arguments, the lower and upper bounds of the range, and returns a random number within that range. For example, "rand(0,1)" will generate a random number between 0 and 1.

3. Can I specify a different distribution for my random variable in MATLAB?

Yes, MATLAB has a variety of built-in functions for generating random variables with different distributions, such as normal, exponential, and binomial. You can also create your own custom distribution using the "makedist" function.

4. How can I plot a histogram of my uniform random variable in MATLAB?

To plot a histogram of your uniform random variable in MATLAB, you can use the "histogram" function. This function takes in the vector of random numbers as an argument and automatically creates a histogram with default settings. You can also customize the histogram by specifying the number of bins or changing the bin edges.

5. Can I generate multiple uniform random variables at once in MATLAB?

Yes, you can generate multiple uniform random variables at once in MATLAB by using the "rand" function with matrix inputs. This will create a matrix of random numbers with the specified dimensions, where each element is a random number within the specified range. You can also use the "randn" function to generate a matrix of random numbers with a standard normal distribution.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
450
  • Set Theory, Logic, Probability, Statistics
Replies
30
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
8
Views
1K
  • Calculus and Beyond Homework Help
Replies
5
Views
882
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
850
  • Set Theory, Logic, Probability, Statistics
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
5
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
4K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
Back
Top