Intro. Computational Science Math 185 Lab 1 September 7, 2010 Dr

Intro. Computational Science
Math 185
Lab 1
September 7, 2010
Dr. Pendergrass
Print Name:
This lab simulates dice tossing using Matlab. Before you start the lab, open up Matlab and set the current
folder to the MATLAB directory in your network folder for this class:
\\hams-acad-fs\Pendergrass\Math185\yourUserName\MATLAB
Dice come in many shapes and sizes. We’ll start small, with a four-sided die. A fair four-sided die has four
possible outcomes when tossed: 1, 2, 3, 4. If it is truly fair, all possible outcomes ought to occur about equally
often. We know that we can use the randi command to produce random integers from 1 to 4. Might this be
a good way to simulate a fair die?
1. We begin by simulating twelve repeated tosses of a single die.
>>dieTosses = randi(4, 1, 12)
This produces a 1 by 12 row vector whose entries are random integers between 1 and 4. We think of the
entries as representing the outcomes of the successive tosses. What was the outcome of the seventh toss
in your simulation?
Answer:
Solution:
Answer varies due to randomness.
2. We’d like Matlab to be able to detect when a certain outcome occurred during the simulation. For
instance, when was the die equal to 4? Type the following command into Matlab (we want to see the
output, so don’t end it with semicolon)
>>myFours = (dieTosses == 4)
(The symbol == is called a relational operator in Matlab.) Clearly describe Matlab’s output.
Answer:
Solution:
The command dieTosses == 4 produces an array of the same size as dieTosses, whose entries are 1 if
the corresponding entry in dieTosses is 4, and 0 otherwise.
3. Matlab’s find command gives the positions of the non-zero entries in an array. You can use it to find
the actual times when your simulated dice landed on 4:
>>fourTimes = find(myFours)
Execute this command in Matlab, and write down your result.
Answer:
Solution:
Answer varies due to randomness.
4. Matlab’s sum command adds up the entries in an array. Execute the following command in Matlab,
and write the result:
Page 1 of 5
Math 185
Lab 1
Dr. Pendergrass
>>numFours = sum(myFours)
Answer:
Solution:
Answer varies due to randomness. Answer will be the number of times a 4 occurred in the simulation.
5. I think you can now see how to get Matlab to automatically count the number of times the die landed
on 3, 2, or 1 during the simulation, or to find the first times that the die landed on 3, 2, or 1. Now let’s
really put this to good use by simulating a very large number of dice tosses. Use what you have learned in
the previous problems to do the following:
(a) Simulate 10,000 tosses of a dice, and store the results in a row vector called dieTosses.
(b) Use the find command to find the first time that the die landed on 1, and store the result in a variable
called firsts(1).
(c) Similarly, find the first times that the die landed on 2, 3, and 4, and store them in the variables
firsts(2), firsts(3), and firsts(4) respectively.
(d) Count the number of times the die landed on 1, and store the result in a variable called counts(1).
(e) Similarly, create variables counts(2), counts(3), and counts(4) that contain the number of times
the die landed on 2, 3, and 4 respectively.
(f) Write down your answers below:
firsts(1)
firsts(2)
firsts(3)
firsts(4)
=
=
=
=
counts(1)
counts(2)
counts(3)
counts(4)
=
=
=
=
6. Make a bar chart of the vector counts you created in the last problem, and save it as a figure file called
lab1Die4Counts.fig in your network MATLAB folder.
7. Based on the evidence of your counts and bar chart, does it appear that we have a reasonable simulation
of a fair four-sided die? Explain in a brief paragraph.
Solution:
Since the die is fair, we expect the possible outcomes 1, 2, 3, and 4 to occur about the same number of
times. The bar chart shows that this is exactly what happens.
8. Now repeat what you did in questions 5 and 6 for a simulation of 10,000 tosses of a six -sided die. Use your
results to fill out the following table:
firsts(1)
firsts(2)
firsts(3)
firsts(4)
firsts(5)
firsts(6)
=
=
=
=
=
=
counts(1)
counts(2)
counts(3)
counts(4)
counts(5)
counts(6)
=
=
=
=
=
=
When you redo question 6, save your bar chart of the new counts vector in a file called lab1Die6Counts.fig.
Again, do your counts and bar chart give you confidence that the simulation is reasonable? Answer in a
brief paragraph.
Page 2 of 5
Math 185
Lab 1
Dr. Pendergrass
Solution:
Again we expect all six possible outcomes to occur about the same number of times. The bar chart shows
that this is exactly what happens.
9. Finally, type the following commands into Matlab:
>>save wkspaceLab1
>>getHistory;
Example Matlab solutions for the four- and six-sided die simulations are given on the following pages.
Page 3 of 5
Question 9 continues on next page. . .
Continuing question 9...
Lab 1
Dr. Pendergrass
Four-Sided Die Simulation:
1
2
3
4
5
% Lab01_4Sided . m
%
% This script records an example solution to the four - sided die simulation
% in Lab 1.
%
6
7
8
9
10
% clear out existing counts variable .
% now
clear counts ;
% % Questions 1 through 4
Don ’ t worry about this command for
11
12
13
% Simulate 12 tosses of a fair 4 - sided die
dieTosses = randi (4 , 1 , 12)
14
15
16
17
18
% Let ’ s find the times when the die landed on 4. First , make an indicator
% array that flags entries equal to 4 with a 1 , and all other entries with
% a 0
myFours = ( dieTosses == 4)
19
20
21
22
23
% To find
% myFours
% entries
fourTimes
the first time the die landed on 4 , use the find command on the
array . The find command returns the positions of non - zero
in an array
= find ( myFours )
24
25
26
27
28
% The indicator array ( myFours ) has a 1 in it for every toss that was 4 ,
% and a 0 in it for every other toss . So if we just sum the entries in the
% indicator array , we ’ ll get the number of times the die landed on 4.
numFours = sum ( myFours )
29
30
% % Questions 5 through 7
31
32
33
34
35
36
37
38
39
40
% Here we want to simulate 10000 tosses of a 4 - sided dice , and then do the
% following
%
%
1. Find the first time each possible outcome occurs , and
%
%
2. Find the total number of times each possible outcome occurs .
%
%
3. Make a bar graph of the counts for each possible outcome .
%
41
42
43
44
% Simulate 10000 rolls
dieTosses = randi (4 , 1 , 10000);
45
46
47
48
49
50
51
52
53
% Flag the 1 ’ s
myOnes = ( dieTosses == 1);
% Find the times on which a 1 appeared
oneTimes = find ( myOnes );
% Record the first tie a 1 appeared
firsts (1) = oneTimes (1)
% Calculate the total number of 1 ’ s that appeared
counts (1) = sum ( myOnes );
54
55
56
57
58
59
60
61
62
63
% Now do the same for the twos
% Flag the 2 ’ s
myTwos = ( dieTosses == 2);
% Find the times on which a 2 appeared
twoTimes = find ( myTwos );
% Record the first tie a 2 appeared
firsts (2) = twoTimes (1)
% Calculate the total number of 2 ’ s that appeared
counts (2) = sum ( myTwos );
64
65
66
67
68
69
70
71
72
73
74
75
% repeat for the 3 ’ s and 4 ’ s
% the 3 ’ s
myThrees = ( dieTosses == 3);
threeTimes = find ( myThrees );
firsts (3) = threeTimes (1)
counts (3) = sum ( myThrees );
% the 4 ’ s
myFours = ( dieTosses == 4);
fourTimes = find ( myThrees );
firsts (4) = fourTimes (1)
counts (4) = sum ( myFours );
76
77
78
79
% make a bar chard of the counts
figure ;
bar ( counts );
Page 4 of 5
Question 9 continues on next page. . .
Continuing question 9...
Lab 1
Six-Sided Die Simulation:
1
2
3
4
5
% Lab01_6Sided . m
%
% This script records an example solution to the six - sided die simulation
% in Lab 1.
%
6
7
8
9
% clear out existing counts variable .
% now
clear counts ;
Don ’ t worry about this command for
10
11
% % Question 8
12
13
14
15
16
17
18
19
20
21
% Here we want to simulate 10000 tosses of a 6 - sided dice , and then do the
% following
%
%
1. Find the first time each possible outcome occurs , and
%
%
2. Find the total number of times each possible outcome occurs .
%
%
3. Make a bar graph of the counts for each possible outcome .
%
22
23
24
% clear out the variables from the simulation of a four - sided die
clear all ;
25
26
27
% Simulate 10000 rolls
dieTosses = randi (6 , 1 , 10000);
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
% collect the data we need
% the 1 ’ s
myOnes = ( dieTosses == 1);
oneTimes = find ( myOnes );
firsts (1) = oneTimes (1)
counts (1) = sum ( myOnes );
% the 2 ’ s
myTwos = ( dieTosses == 2);
twoTimes = find ( myTwos );
firsts (2) = twoTimes (1)
counts (2) = sum ( myTwos );
% the 3 ’ s
myThrees = ( dieTosses == 3);
threeTimes = find ( myThrees );
firsts (3) = threeTimes (1)
counts (3) = sum ( myThrees );
% the 4 ’ s
myFours = ( dieTosses == 4);
fourTimes = find ( myThrees );
firsts (4) = fourTimes (1)
counts (4) = sum ( myFours );
% the 3 ’ s
myFives = ( dieTosses == 5);
fiveTimes = find ( myFives );
firsts (5) = fiveTimes (1)
counts (5) = sum ( myFives );
% the 4 ’ s
mySixes = ( dieTosses == 6);
sixTimes = find ( mySixes );
firsts (6) = sixTimes (1)
counts (6) = sum ( mySixes );
60
61
62
63
64
% make the bar chart of counts
% pull up a new figure window first , so we don ’ t overwrite the first one
figure ;
bar ( counts );
Page 5 of 5
Dr. Pendergrass