This lab will introduce you to using m-files, simple programs written using Matlab. I have written two simple Matlab programs, threeballs.m and loopexp.m. The first program runs a simulation of the experiment from our last quiz, and the second program runs the simulation several times. Read through these files and experiment with the commands they contain. Use Matlab's help command and try to understand what each command in the programs does. There is also a tutorial on m-files and Matlab programming commands at http://www.math.ufl.edu/help/matlab-tutorial/matlab-tutorial.html#SEC13. You may refer to this document for additional help understanding the commands.
As you read through the lab, answer the questions printed in boldface
in a word processor. To use the loopexp and threeballs
programs, save them to the desktop, and then use the 'File->Set
Path' to add your desktop to the list of directories Matlab searches
for m-files. Now execute the following commands:
threeballs(3,2,1)
This runs the simulation once with three red balls, two green balls, and one blue ball, and prints out the letter r, g, or b for the result. Repeat the above command several times and observe what happens.
Now assign your birth date in the form mmddyy to a variable
called seed. For example, if you were born on July 20, 1979,
you would use:
seed=072079
Computers are incapable of producing random behavior without user input, and instead use complicated formulas called random number generators to simulate random sequences of behavior. The sequence of of events is completely determined by the starting (seed) value for the random number generator, so this value will be used to ensure that the simulation gives the same 'random' behavior each time.
Now run the following command:
[r,g,b]=loopexp(3,2,1,100,seed)
This runs the above simulation 100 times and stores the frequencies of final ball colors in the variables r, g, and b. What is the frequency that the third ball drawn is red? How is this related to the probability found in the last quiz?
Now find the command to run the simulation 100,000 times. What command did you use? What happens to the frequencies? Explain why this happens.
Now modify the above command to run the experiment 100,000 times as before, but with ten times as many balls of each color. What command did you use? Explain what happens to the probabilities.
Finally, modify the threeballs.m and loopexp.m programs to do all of the following:
Use 100,000 iterations of the experiment to approximate the probabilities of the different final ball colors when starting with four yellow, three red, two green, and one blue ball. Make a tree diagram and check that the approximated frequencies are very close to the exact values. What are the approximated frequencies of each final ball color given by the simulations of the modified experiment? What are the exact frequencies? Note that you will need to use a tree diagram to answer the last question; this tree diagram will be very large and may take up several pages. Why would anyone want to use a computer to simulate a probability experiment when they can calculate the exact values by hand?
As usual, you may work in small groups for this project, but you should each type up your own document answering the questions using your own value of the seed variable.