Part C: Dictionaries, Arrays, Functions, and Modules
Exercises
- A simple simulation engine: Write a Python function
IterateLogistic()
that takes three parameters: two real numbers and an integer.
Interpret the first two as the current state and the control parameter
value for the one-dimensional map: f(x) = r x (1 - x). The function
should return the state after the given number of iterations, which is
the third parameter. Test your function interactively.
- 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. Start each program run with a different initial
condition. The initial conditions should be close: Use x_0 = 0.3 + delta_i,
where delta_i = i * .005, 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.
- 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.
- 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 and that these time values
agree line by line. Write a Python program
MaxSeparation.py that
produces another time series 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 new
times series in a file. What do you see? What is your interpretation of
the d_max values as a function of time?
Hint: Use the matrix module written above by
importing it.
-
Ordinary differential equation simulation:
- Write a Python program OneDEuler.py that integrates
one-dimensional ordinary differential equations using the Euler method.
(See this week's Lecture Notes for the Euler integration method.) Write
the integrator so that you can pass in any function that specifies a
one-dimensional ODE. Have the integrator take the current state and time
step size and return the state after one time step.
- Write a function Pitchfork() that can be passed to the
integrator to solve the ODE for pitchfork bifurcations:
dx/dt = r x - x3.
- Using a time step of dt = 0.03, run from various initial
conditions and parameter settings:
r = -1.0, 0.0, 1.0 and x0 = 2.0, 0.1, -2.0.
Print out a time series of 20 steps for each of the 9 possible combinations
of parameter r and initial condition x0.
Table of Contents