1. Exercises
Write these functions. Test them. don't turn them in, but save them for your own future use. Save them in a textfile, and name the file something like myfunctions.py. Put that file in the same directory as whatever program you're writing (and testing and running). You can then use those functions by first importing the module (using import myfunctions and then using the functions name. If the module includes a function called factorial(num), you invoke it in your program (say, to find the factorial of 6) using myfunctions.factorial(6). (For more detailed information about modules, see "Module Coding Basics," Chapter 16 in Learning Python.)1.1. area of a circle
-
accepts: radius
-
returns: area
1.2. distance
-
accepts: two lists of numbers (a1,b1,c1,d1,e1,...) and (a1,b2,c2,d2,e2,...) of the same length, n
-
returns: the distance between these two points as if they represented points in n-dimensional space
-
Say for example that n=3.
-
The function would accept two points in three-dimensional space: (a1,b1,c1) and (a2,b2,c2). (If you prefer, think of them as (x1,y1,z1) and (x2,y2,z2).
-
The distance between the points is the square-root of the sum of the squared pairwise distances. That is, the distance between (0,1,2) and (3,4,5) is ((0-3)2 + (1-4)2 + (2-5)2''')'''0.5. That's about 5.2. (Python has a square-root function in the math module. Import math and use math.sqrt(9) for the square-root of 9.)
-
The distance between two points in n-dimensional space works the same way. Find pairwise distances between corresponding coordinates. Square the differences and sum the squares. The distance is the square-root of the summed squared differences.
1.3. linear interpolation
-
accepts: x1, y1, x2, y2, x3
-
returns: the y3 corresponding to the point on the line whose x-value is x3 and which includes (x1,y1) and (x2,y2)
1.4. mean
-
accepts: a list of numbers
-
returns: the mean of those numbers
1.5. standard deviation
-
accepts: a list of numbers
-
returns: the standard deviation from the mean
-
The is the square-root of the average squared deviation from the mean.
-
You take all the data and find their mean.
-
Then you take each datum and find its difference from the mean.
-
Square these differences and find their average. (How do you find the average of n numbers? Easy: sum thee numbers and divide by n. In many formulations of standard deviation, though, you divide by n-1, as a correction for the effects of very small sample size n. You should do that: divide by n-1 instead of n.)
-
That average is the variance. The square-root of variance is the standard deviation.
1.6. weighted mean
-
accepts: either two lists of the same length (a data list and a weight list) or a list of lists, each of which has two elements (a datum and a weight)
-
returns: the weighted mean of the data.
-
Sum all the weights.
-
Replace each weight by a relative weight: the original weight divided by the sum of all weights.
-
Multiply each datum by its weight. Now you have weighted data.
-
The mean of the weighted data is the weighted mean.
1.7. uniform deviate
-
acepts: center (c) and width (w)
-
returns: random number in an interval whose center is c and which is 2w wide. For example, if c=4.5 and w=.25, the function would return a random number in the interval from 4.25 to 4.75, with any number in that interval being as likely as any other.
1.8. normal deviate
-
accepts: mean (m) and variance (v, but note that the usual notation for variance is s-squared)
-
returns: a random number drawn from a normal probability distribution (also called a Gaussian distribution) whose mean is m and whose variance is v
-
Actually, this one is provided by the random module, which you import using import random. It's random.gauss(m,v).
1.9. sort
-
accepts: a list of numbers
-
returns: a list of the same numbers, rearranged to be increasing or decreasing in size
-
There are many sort algorithms, and we'll talk about a few of them when we discuss efficiency later in the semester.
-
Come up with one workable algorithm and implement it.
2. Assignment
No new assignment. Just finish up the last two, and start planning out your project model.Things to think about:
-
What are the actors?
-
What states (e.g., quantities) do these actors have?
-
How do these states change in one timestep?
-
How long should a timestep be?
-
How do the state-changes depend on the states of other actors (if at all)?
-
How should the program represent the changes? That is, how do you code the dynamic in Python?
