CVEN 302-501 1 Random Number Generation and

CVEN 302-501
Computer Applications in Engineering and Construction
Programming Assignment #2
Random Number Generation and Particle Diffusion
Date distributed : 9/9/2015
Date due
: 9/18/2015 at 8:00 a.m.
Upload your memorandum and program code (in one Word file) to turnitin.com by the date and
time listed above. Demonstrate your work in the memordandum text, and follow the directions in
the course syllabus.
Matlab has a built in random number generator, rand, that is frequently used in numerical simulations. The rand function generates uniformly distributed numbers (between 0 and 1). We are
going to test this random number generator and apply it to solve a diffusion problem.
1
Random Number Generation and Diffusion
Random number generation is an important task in many simulations of random processes, such
as diffusion, rainfall, structural failure, supply chain management, etc. One of the most important
tools in engineering and physics that uses random numbers is called Monte-Carlo simulation. For
certain studies, an exact result is unfeasible or impossible to obtain, in which case Monte-Carlo
simulations may be performed, generating a large number of repeated computations, each with
slightly different input data, to obtain statistical properties of the results. This has been used
frequently in problems with significant uncertainty in inputs.
One good example of a random process is the Brownian motion of molecules in a stagnant
environment, also called molecular diffusion. Such physics is also useful in simulating the spreading
of scalar substances (such as a pollutant) in a zero-mean-current environment.
2
Using Uniformly Distributed Random Numbers
You will first need to use the Matlab function rand to generate 4,000 random points (x1 , y1 ),
(x2 , y2 ), ..., (x4000 , y4000 ) between x = [−1, 1] and y = [−1, 1]. Store each x,y pair on a different
row of a 2-column array. For each element of the array, use rand to generate numbers between 0
and 1, and then rescale them to between -1 and 1. Use help rand to get information about this
function and its implementation.
After you successfully generate those 4,000 random points, plot them in a subplot (3,1,1) axis
window in Figure (1) using red dots and no lines. The variable x should be on the x-axis and y on
the y-axis.
CIVIL
3136 TAMU
College Station, Texas 77843-3136
(979) 845-4517
FAX (979) 862-8162
The second step is to evenly divide the range of [−1, 1] in the x-direction into 20 intervals so
each interval is 0.1 units in width. Use IF- and FOR-blocks to count how many points are plotted
in each interval. Plot the number of points versus their corresponding x location (at the center
of each interval) in subplot (3,1,2) in Figure (1). Figure (1) therefore has two subplots - the first
subplot should plot the 4,000 red dots, and the second subplot should plot 20 data points with
values organized as follows

−0.95

 −0.85

 ..
 .
N1
N2
..
.










 0.85 N19 
(1)
0.95 N20
where Ni indicates the number of particles in the ith interval (If done correctly, Ni should be close
to 200). Plot these 20 data points using the bar command. Use help bar for information about
this command. Give appropriate labels and a title to each subplot.
The third step is to use the hist command to generate a histogram with 20 evenly spaced
“containers,” and plot the result in a third subplot (3,1,3) in Figure (1). Use help hist for
information about this command. Compare the distribution using hist to what you obtained in
step 2.
Discuss whether the randomly generated numbers are evenly distributed and judge whether
the random number generator rand generates uniformly distributed random numbers. You may
increase the total number of points (for example, from 4,000 to 100,000) before you make your
conclusions.
Organize your results using the memorandum template from the Assignments page of the course
web site. In the body of the memorandum, you should briefly describe the methods used to classify
each point and include a copy of the final figure produced by your working computer code. You
may also discuss any problems with your code and ideas you may have to fix them. Include a listing
of your Matlab program in an Appendix. Be sure to include descriptive text with the computer
code or codes in the Appendix.
Your results should look similar to the results in Figure 1.
3
Random Walk and Particle Diffusion
Based on the 4,000 points you generated using the rand function in Section 2, in this section
we will simulate the spread of a theoretical oil spill based on the theory of random walk. You will
simulate 4,000 oil particles, and consider each x value in your results above as the initial location
on the x-axis of each oil particle.
For the random walk motion, assume at each time step (with a unit of minutes) each particle
can move a distance of 0.1 (with a unit of meters) in the positive or negative x-direction (we will
neglect the y-direction to simplify the problem). Let the direction (positive or negative) be random,
and use rand at each time step to generate a random number for each particle. Since the motion is
random, each particle should be given an equal chance to move left or right. For example, you may
move a particle to the right if the generated random number is greater than 0.5 and vice versa.
2
Random points generated using "rand"
1
y
0.5
0
-0.5
-1
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
number of points
300
x
Distribution of points
200
100
0
-1
number of points
300
x
Histogram of points
200
100
0
-1
x
Figure 1: Results for Section 2.
3
After 200 time steps, plot the particle locations (xi , yi ) and a histogram (calculated by hand,
not using hist) of their distribution using subplots in Figure (2). For the figure, make sure the
x-axis covers a large enough range to include all of the points. Figure 2 shows an example of what
this figure might look like.
Random Walk
1
y
0.5
0
-0.5
-1
-5
-4
-3
-2
0
x
1
2
3
4
time =
200
3
4
5
Distribution of points
1000
number of points
-1
800
600
400
200
0
-5
-4
-3
-2
-1
0
x
1
2
5
Figure 2: Results for Section 3.
Matlab is capable of displaying results as both images and movies. After getting your program
working successfully, you can plot the particle locations at evey time step and use pause to pause a
desired duration, such as 0.05 second, between adjacent plots so you can see the particles in motion.
The process discussed here is call diffusion.
4