Part C: Dictionaries, Arrays, Functions, and Modules

Exercises

  1. A simple simulation engine:
    • Write a Python function IterateLogistic() that takes three parameters: Two real numbers and an integer.
    • Interpret the first two—x and r—as the current state and the control parameter value for the Logistic Map: f(x) = r x (1 - x).
    • The function should return the state after the given number of iterations, which is the third parameter, call it n.
    • This is all the function should do—the numerical iterations: xn = fn (x0).
    • Test your function interactively in iPython.
  2. Generating experimental data:
    • Write a Python program LogisticData.py that generates 10 data files, each with 50 Logistic Map iterates, at map parameter r = 4.0.
    • Each line in the files should list the iteration number n and then the iteration value xn.
    • Start each program run with a different initial condition x0. The initial conditions should be close: Use x0 = 0.3 + deltai, where deltai = i * 0.00005, i = 0, 1, 2, 3, ..., 9.
    • Name the 10 files "data1" to "data10".
      Hint: Use the program you wrote for the Part B exercises to generate the data files, adding the iteration function you wrote above.
  3. Storing and retrieving formatted data:
    • Write a Python module MatrixIO.py containing two functions.
    • One function should read a file containing a text representation of a matrix; that is, a certain number of lines each containing the same number of numbers separated by spaces.
    • The function should return an array containing this data. Note that the function should work for matrices of any size.
    • The second function should do the inverse: write a matrix of any size to a text file.
    • Use these in a Python program MatrixReadWrite.py that reads in file data7 from the previous exercise and then writes it out to a file data7.out.
      Hint: Use the matrix module written above by importing it.
  4. Processing experimental data:
    • Recall that each line in the data files you just generated has an iteration number and then a state value.
    • Assume that all files have the same length.
    • Write a Python program MaxSeparation.py that produces another file consisting of lines with iteration number and d_max, where d_max is the largest distance between all pairs of states taken from the different files at the same iteration number.
    • Store this series of numbers in a file.
    • What do you see? What is your interpretation of the d_max values as a function of iteration?

Table of Contents