Tutorial 2.5

Generic ECE - University of Houston

Han Q. Le (c) Copyrighed

The objective of this tutorial to see how lists and arrays are used in common examples. Practice by creating your own similar example and have fun,

1. Multi-object personal data record

Exercise 1.5 Create a list of your personal data as shown below

If you have problem of recording with the microphone, chances are system doesn’t allow other apps to access the microphone. Google search how to allow apps access your microphone for your OS (e. g. Windows or Mac). Do same with camera.

Tutorial_2.5_answer_1.gif

Answer (replace the below with your info - do at home)

The takeaways: the concept of data record and database structure.

Google search datarecord and database

Tutorial_2.5_answer_2.gif

Tutorial_2.5_answer_3.gif

2. Learn to import file: gif and graphics

Do the exercise in Tutorial_2_answer section 3.3, exercise 3.8. Make sure you have output gif file “emwave.gif” already on your laptop and bring the file to the class. Then, you will follow instructions what to do with it in class. This is what one frame of the file should look like:

Out[2]=

Tutorial_2.5_answer_4.gif

2.1 Import file data as List

In[3]:=

Tutorial_2.5_answer_5.png

In[45]:=

Tutorial_2.5_answer_6.png

Out[45]=

Tutorial_2.5_answer_7.png

In[4]:=

Tutorial_2.5_answer_8.gif

2.2 Add/modify graphics

In[67]:=

Tutorial_2.5_answer_9.png

In[68]:=

Tutorial_2.5_answer_10.png

Out[68]=

Tutorial_2.5_answer_11.png

                         

3. Exercise with list, array: sound

3.1 Make a tune

Given:

Tutorial_2.5_answer_12.gif

Answer

In[1]:=

Tutorial_2.5_answer_13.gif

Out[9]=

Tutorial_2.5_answer_14.gif

In[2]:=

Tutorial_2.5_answer_15.gif

Out[2]=

Tutorial_2.5_answer_16.png

The takeaways: How to get and look up reference info, data.

3.2 Use function Map.

We can define a function to generate a sound signal. Then use Map to play from a list.

Tutorial_2.5_answer_17.gif

Out[51]=

Tutorial_2.5_answer_18.gif

In[13]:=

Tutorial_2.5_answer_19.png

In[14]:=

Tutorial_2.5_answer_20.gif

Out[15]=

Tutorial_2.5_answer_21.gif

In[3]:=

Tutorial_2.5_answer_22.gif

Out[3]=

Tutorial_2.5_answer_23.png

4. Exercise with list, array: illustration

See this graphics on the course webpage: social_media _market _share. A few students were able to do it couple years ago with less than one week intense-training on Mathematica that they had never learned before (but they were proficient in Python and programming in general). Do you think you can do it? Just write your thinking how you would go about to do something like it.

5. Exercise with list, array: graphics 1

to be done in class

In[1]:=

Tutorial_2.5_answer_24.png

Out[1]=

                      Tutorial_2.5_answer_25.gif

In[2]:=

Tutorial_2.5_answer_26.gif

Out[3]=

                       Tutorial_2.5_answer_27.gif

In[32]:=

Tutorial_2.5_answer_28.png

In[8]:=

Tutorial_2.5_answer_29.gif

Out[9]=

                  Tutorial_2.5_answer_30.gif

In[10]:=

Tutorial_2.5_answer_31.gif

Out[11]=

                      Tutorial_2.5_answer_32.gif

In[16]:=

Tutorial_2.5_answer_33.gif

Out[17]=

                    Tutorial_2.5_answer_34.gif

The takeaways: Use indexed variables - as opposed to fixed numbers

6. Exercise with list, array: graphics 2 - animation

6.1 A spiral of spheres

Tutorial_2.5_answer_35.gif

Out[25]=

                          Tutorial_2.5_answer_36.gif

6.2 Animation with a time parameter

In[38]:=

Tutorial_2.5_answer_37.png

Out[38]=

In[3                                         9]:=

Tutorial_2.5_answer_39.png

Out[39]=

                                            In[40]:= spircube.gif

Tutorial_2.5_answer_41.png

Out[4                                  0]= sphereRandm.gif

The takeaways: Use indexed variables for animation, manipulation

In[88]:=

Tutorial_2.5_answer_43.png

Out[88]=

                           Tutorial_2.5_answer_45.png

7. Exercise with list, array: graphics 3
Monte Carlo demo of Poisson distribution

7.1 Problem set up:

First, a description of the background.

Tutorial_2.5_answer_46.gif

https://www.youtube.com/watch?v=Sh1ZeWhAtRI

Exercise 7.1

Let’s assume that all the raindrops were lemondrops and gumdrops, and the kids all stood outside with mouths open wide. Assume that all the kids have mouth of the same size: 3-in diameter, what is the mouth area (in Tutorial_2.5_answer_47.png)?

Answer

In[118]:=

Tutorial_2.5_answer_48.gif

Out[119]=

Tutorial_2.5_answer_49.png

or 7 Tutorial_2.5_answer_50.png.

Exercise 7.2

Let the lemon/gumdrops density is Tutorial_2.5_answer_51.png per 10 minutes. If we have 100 kids all stand outside with mouth open wide for 15 minutes, how many lemon or gumdrops would each kid catch? Wait, will everyone get exactly the same number of candies? No, some will get more, some will get less.
We should ask, what is the average number of candies that each kid can catch?

Answer

In[120]:=

Tutorial_2.5_answer_52.gif

Out[121]=

Tutorial_2.5_answer_53.png

7.2 Monte Carlo simulation of a random process

The average is 10.6 candies.  But does that mean every kid would get exactly 10.6 lemon/gumdrops? If we count the number of “raindrops” each kids get, and tabulate on a histogram, do they all get 10, 11? or would some get 5, other would get 15 etc... Is it possible for some to get 20?

How do you answer the questions above. Or could it be that it doesn’t have an answer? If you have several groups of 10,000 kids each. Would there be any resemplance of their histograms? or will it be totally random for each group and all the histograms are different?
In the follow, you should follow the guidelines to gain an understanding and see if you can answer those questions at the end.

7.2.1 Background

One way to study this problem is the Monte Carlo method:

Tutorial_2.5_answer_54.gif

Tutorial_2.5_answer_55.gif

7.2.2 Approach

We can use the computer to simulate 100 kids’ mouths and rain drops. First, set up the mouths:

Exercise 7.3

Let's make the mouth to be a circle (disk). Let' s assume the kids line up in 10 rows x 10 columns. Generate a graphics for the mouths, using Disk:

In[126]:=

Tutorial_2.5_answer_56.png

Answer

In[127]:=

Tutorial_2.5_answer_57.gif

Out[127]=

Tutorial_2.5_answer_58.gif
Tutorial_2.5_answer_59.gif Tutorial_2.5_answer_60.gif Tutorial_2.5_answer_61.gif Tutorial_2.5_answer_62.gif Tutorial_2.5_answer_63.gif Tutorial_2.5_answer_64.gif Tutorial_2.5_answer_65.gif Tutorial_2.5_answer_66.gif Tutorial_2.5_answer_67.gif Tutorial_2.5_answer_68.gif
Tutorial_2.5_answer_69.gif Tutorial_2.5_answer_70.gif Tutorial_2.5_answer_71.gif Tutorial_2.5_answer_72.gif Tutorial_2.5_answer_73.gif Tutorial_2.5_answer_74.gif Tutorial_2.5_answer_75.gif Tutorial_2.5_answer_76.gif Tutorial_2.5_answer_77.gif Tutorial_2.5_answer_78.gif
Tutorial_2.5_answer_79.gif Tutorial_2.5_answer_80.gif Tutorial_2.5_answer_81.gif Tutorial_2.5_answer_82.gif Tutorial_2.5_answer_83.gif Tutorial_2.5_answer_84.gif Tutorial_2.5_answer_85.gif Tutorial_2.5_answer_86.gif Tutorial_2.5_answer_87.gif Tutorial_2.5_answer_88.gif
Tutorial_2.5_answer_89.gif Tutorial_2.5_answer_90.gif Tutorial_2.5_answer_91.gif Tutorial_2.5_answer_92.gif Tutorial_2.5_answer_93.gif Tutorial_2.5_answer_94.gif Tutorial_2.5_answer_95.gif Tutorial_2.5_answer_96.gif Tutorial_2.5_answer_97.gif Tutorial_2.5_answer_98.gif
Tutorial_2.5_answer_99.gif Tutorial_2.5_answer_100.gif Tutorial_2.5_answer_101.gif Tutorial_2.5_answer_102.gif Tutorial_2.5_answer_103.gif Tutorial_2.5_answer_104.gif Tutorial_2.5_answer_105.gif Tutorial_2.5_answer_106.gif Tutorial_2.5_answer_107.gif Tutorial_2.5_answer_108.gif
Tutorial_2.5_answer_109.gif Tutorial_2.5_answer_110.gif Tutorial_2.5_answer_111.gif Tutorial_2.5_answer_112.gif Tutorial_2.5_answer_113.gif Tutorial_2.5_answer_114.gif Tutorial_2.5_answer_115.gif Tutorial_2.5_answer_116.gif Tutorial_2.5_answer_117.gif Tutorial_2.5_answer_118.gif
Tutorial_2.5_answer_119.gif Tutorial_2.5_answer_120.gif Tutorial_2.5_answer_121.gif Tutorial_2.5_answer_122.gif Tutorial_2.5_answer_123.gif Tutorial_2.5_answer_124.gif Tutorial_2.5_answer_125.gif Tutorial_2.5_answer_126.gif Tutorial_2.5_answer_127.gif Tutorial_2.5_answer_128.gif
Tutorial_2.5_answer_129.gif Tutorial_2.5_answer_130.gif Tutorial_2.5_answer_131.gif Tutorial_2.5_answer_132.gif Tutorial_2.5_answer_133.gif Tutorial_2.5_answer_134.gif Tutorial_2.5_answer_135.gif Tutorial_2.5_answer_136.gif Tutorial_2.5_answer_137.gif Tutorial_2.5_answer_138.gif
Tutorial_2.5_answer_139.gif Tutorial_2.5_answer_140.gif Tutorial_2.5_answer_141.gif Tutorial_2.5_answer_142.gif Tutorial_2.5_answer_143.gif Tutorial_2.5_answer_144.gif Tutorial_2.5_answer_145.gif Tutorial_2.5_answer_146.gif Tutorial_2.5_answer_147.gif Tutorial_2.5_answer_148.gif
Tutorial_2.5_answer_149.gif Tutorial_2.5_answer_150.gif Tutorial_2.5_answer_151.gif Tutorial_2.5_answer_152.gif Tutorial_2.5_answer_153.gif Tutorial_2.5_answer_154.gif Tutorial_2.5_answer_155.gif Tutorial_2.5_answer_156.gif Tutorial_2.5_answer_157.gif Tutorial_2.5_answer_158.gif

The left handside is the model of the right handside that is easier for counting (lest the right hand side swallow lemon/gumdrops before we finish counting).

Next, let’s simulate lemongumdrops:

Exercise 7.4

Calculate the expected number of candies falling in a square of 30 x 30 Tutorial_2.5_answer_159.png in 15 minutes using the density given below. Denote that as ndrop, then generate ndrop number of points (each point is a candy) with {x, y} coordinates in that 30x30 square, using RandomReal[].

Tutorial_2.5_answer_160.png

Answer

In[134]:=

Tutorial_2.5_answer_161.png

Out[137]=

Tutorial_2.5_answer_162.gif

Exercise 7.5

Now, let’s see who catch what by putting mouths and lemongum raindrops together:

In[138]:=

Tutorial_2.5_answer_163.png

Out[138]=

Tutorial_2.5_answer_164.gif

Count the number of candies falling in each mouth for all mouths.

Answer

We will count for each mouth by using this:

In[143]:=

Tutorial_2.5_answer_165.gif

Out[146]=

Tutorial_2.5_answer_166.gif Tutorial_2.5_answer_167.gif

There we have it. Some are very lucky with many, some are the opposite.

7.3 Histogram of candies - demo only

Tutorial_2.5_answer_168.gif

Out[147]=

Tutorial_2.5_answer_169.gif Tutorial_2.5_answer_170.gif

We see that the lucky one could catch 6 times more than the unlucky one. The mean is:

In[148]:=

Tutorial_2.5_answer_171.png

Out[148]=

Tutorial_2.5_answer_172.png

OK, this is what you should do. Use the code below (you don’t have to understand it at this point - later in the course, you will - just use it for now) be simulate and obtain histograms for 400 kids each time (improve our statistics with larger sampling population). Do it several times (your choice) and add themt to get more statistics until you feel confident in your answers:
- What is the chance for a kid to get only 4 lemon/gumdrops? (of course, under the same condition: Tutorial_2.5_answer_173.png per 10 minutes and the kid stands for 15 minutes.
- what is the chance to get exactly 10? (many think it is about 50%-50% and that is very wrong).
- what is the chance to get 15 and 20, respectively
- show your histograms.

It’s much faster if we use square mouth instead of round mouth. Here is the code for 2500 kids (50 by 50)

In[52]:=

Tutorial_2.5_answer_174.gif

your answer is in array catch1. The total is in total. It is up to you to plot. You can copy and paste the code above to run in your file, but you cannot copy and paste the code to plot the histogram below, because the purpose of this exercise is for you to remember the syntax of Histogram function by typing it yourself even if you have to look at the example character by character. Of course, you should always use the Help part of Mathematica.

In[60]:=

Tutorial_2.5_answer_175.png

Out[60]=

Tutorial_2.5_answer_176.gif

This distribution is called Poisson distribution. You can read up an interesting application during WW2 when the British tried to determine if German V-1 and V-2 bombers were sufficiently accurate navigationally to target high-value facilities: https://www.britannica.com/topic/Poisson-distribution

This is another demo: you can see the distribution reaches certain shape as n-> 10,000. That shape is Tutorial_2.5_answer_177.png

In[1]:=

Tutorial_2.5_answer_178.png

Out[1]=

8. Exercise with list, array: strings

Tutorial_2.5_answer_180.gif

In[85]:=

Tutorial_2.5_answer_181.png

In[86]:=

Tutorial_2.5_answer_182.png

Out[86]=

Tutorial_2.5_answer_183.png

In[235]:=

Tutorial_2.5_answer_184.png

Out[235]=

Tutorial_2.5_answer_185.png

In[237]:=

Tutorial_2.5_answer_186.png

Out[237]=

Tutorial_2.5_answer_187.png

In[236]:=

Tutorial_2.5_answer_188.png

Out[236]=

Tutorial_2.5_answer_189.png

In[22]:=

Tutorial_2.5_answer_190.png

Out[22]=

Tutorial_2.5_answer_191.png

In[127]:=

Tutorial_2.5_answer_192.gif

Out[129]=

Full
lu Flu Full




many
am an ay ma my na ya amy
any man may nam nay yam many myna
glorious
go io ir is li lo lu oi
or os ru si so ur us gil
goo gos gus isl log loo lou lug
oil org our rig rio rug sir sol
sou sui girl giro gris grus igor logo
logs lois loos lori lour lugs luis oils
orig oslo ours rigs rios roil rugs silo
slog slug slur soil solo soul sour uris
girls giros igloo logos loris louis lours roils
igloos lugosi glorious




morning
go in io ir mi mo ni no
oi om on or gin inn ion min
mir moi mon non nor org rig rim
rio rom ron gino giro grim grin igor
iron ming minn miro morn norm orig orin
ring groin minor morin ringo morning

have
ah av ea eh ha he va ave
eva have





seen
en es ne se ens nee see sen
seen






Created with the Wolfram Language