CompSciHW3

= Homework 3 - Due Friday., Feb. 19 =

1. Replace the re-used constants in the following code with appropriate names, and list their types:

example: P = 150000.0 - 30000. -> cost = 150000.00 # float P += P*0.03/12 - 1300. down = 30000.00 # float P += P*0.03/12 - 1300. payment = 1300.0 # float P += P*0.03/12 - 1300. rate = 0.03/12  # float ...                       P = cost - down P += P*rate - payment

a) x = 0.5    x = x*(1-x)*0.04     x = x*(1-x)*0.04

b) y = []    y.append([4,5,6])     y.append([5,6,4])     y.append([6,4,5])

c) def calc_E(x):      return x*x*200.0/2.0     def calc_F(x):       return -x*200.0

2. Write a function returning the pattern for each of the following: example: c_op_list = [ sqrt(2.0/kappa * 0.8)*a.dag, sqrt(2.0/kappa * 0.2)*a ] c_op_list = [ sqrt(2.0/kappa * 0.1)*a.dag, sqrt(2.0/kappa * 0.9)*a ]

answer (the names are arbitrary of course):

def mk_ops(p): return [ sqrt(2.0/kappa*p)*a.dag, sqrt(2.0/kappa * (1.0-p))*a ]

a) 5 equally spaced numbers from 0.0 to x:    [0.0, 0.1, 0.2, 0.3, 0.4]     [0.0, 0.5, 1.0, 1.5, 2.0]     [0.0, -0.2, -0.4, -0.6, -0.8]

b) Substitution into the polynomial:    (-3*5 + 2)*5 + 7     (-3*0 + 2)*0 + 7     (-3*10 + 2)*10 + 7

c) The or operation -- From two inputs, the function returns True or False:    True, True -> True     False, True -> True     True, False -> True     False, False -> False

3. Write a for-loop to create a list with 100 iterations of the Tinkerbell map for a=0.3, b=0.6000, c=2.0, d=0.27 (https://en.wikipedia.org/wiki/Tinkerbell_map): xp = x*(x+a) - y*(y-b) yp = 2*x*y + c*x + d*y # Remember to update x,y to the new values, xp,yp after each iteration!

4. Write a code to open the file "iterates.dat" for writing, write 100 strings to the file (you can use the iterates from  the last problem or any other string) and close it.

5. Write a code to open "iterates.dat" for reading and print out each line.

Experiment on your own with plotting commands!