Problem set #1 PHY256 Problems with Solutions

Problem 1.

In [24]:
#solution
import numpy as np
twopi = 2.0*np.pi
def my_atan2(x,y):  # here is my function
    ang = np.arctan2(y,x)
    return ang%twopi;

#tests for the function
print(my_atan2(-1,0))  # negative numbers and integers
print(my_atan2(0,0))   # origin
3.141592653589793
0.0

Problem 2.

a) For arange, linspace, array, and zeros you can specify what type of data type is in the array if you use the argument dtype=. If you give array integers, it will assume an integer array. linspace and zeros seem to by default give floats. *arange can also give integers by default.

syntax arange(start, stop+dx, dx)

syntax zeros(N) where $N$ is the number of elements in the array

syntax linspace(start, stop, num=N)

Python indexes arrays from 0 to $N$-1 where $N$ is the length of the array.

range is an interator. syntax start stop step, helpful for doing loops.

In [42]:
# checking types
x=np.array([0,1,2,3])
print(x)
print(type(x[0]))

x = np.linspace(0,4,2)
print(x)
print(type(x[0]))

x = np.arange(0,4,1)
print(x)
print(type(x[0]))

x = np.zeros(10)
print(x)
print(type(x[0]))
[0 1 2 3]
<class 'numpy.int64'>
[0. 4.]
<class 'numpy.float64'>
[0 1 2 3]
<class 'numpy.int64'>
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
<class 'numpy.float64'>
In [45]:
for i in range(0,10,2):
    print(i)
0
2
4
6
8
In [26]:
# b) solution
# syntax arange(start, stop+dx, dx)
x= np.arange(0,10.1,0.1)   # make the x array
x  # print the array
Out[26]:
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
        1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.1,
        2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ,  3.1,  3.2,
        3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,  4.1,  4.2,  4.3,
        4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ,  5.1,  5.2,  5.3,  5.4,
        5.5,  5.6,  5.7,  5.8,  5.9,  6. ,  6.1,  6.2,  6.3,  6.4,  6.5,
        6.6,  6.7,  6.8,  6.9,  7. ,  7.1,  7.2,  7.3,  7.4,  7.5,  7.6,
        7.7,  7.8,  7.9,  8. ,  8.1,  8.2,  8.3,  8.4,  8.5,  8.6,  8.7,
        8.8,  8.9,  9. ,  9.1,  9.2,  9.3,  9.4,  9.5,  9.6,  9.7,  9.8,
        9.9, 10. ])
In [31]:
# b) here is another way to do it!
x= np.linspace(0,10.0,101)
# syntax numpy.linspace(start, stop, num=N)
print(x)
[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.   1.1  1.2  1.3
  1.4  1.5  1.6  1.7  1.8  1.9  2.   2.1  2.2  2.3  2.4  2.5  2.6  2.7
  2.8  2.9  3.   3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.   4.1
  4.2  4.3  4.4  4.5  4.6  4.7  4.8  4.9  5.   5.1  5.2  5.3  5.4  5.5
  5.6  5.7  5.8  5.9  6.   6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9
  7.   7.1  7.2  7.3  7.4  7.5  7.6  7.7  7.8  7.9  8.   8.1  8.2  8.3
  8.4  8.5  8.6  8.7  8.8  8.9  9.   9.1  9.2  9.3  9.4  9.5  9.6  9.7
  9.8  9.9 10. ]

Problem 3.

An array is a set of numbers.

A list does not have to be numbers, usually it is the same type. You can have a list of strings.

A tuple could be a heterogeneous set

Tuples are imutable, whereas lists and arrays are mutable (can be changed).

In [48]:
#b)
xarr = np.array([3,2,1,6,10,15])
yarr = np.array([-0.1,0.3,5,0.5,1,10])
print(my_atan2(yarr,xarr))
# you can pass arrays but not lists or tuples to our atan routine
[1.60411732 1.42190638 0.19739556 1.48765509 1.47112767 0.98279372]

Problem 4.

In [46]:
# here is the solution!
def mkarr(N):
    A = np.zeros((N,N))  # allocate an NxN 2d array
    intlist = np.arange(0,N)
    for j in range(N):
        A[j] = intlist + j*N
    return A

A  = mkarr(10)
print(A)
[[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
 [10. 11. 12. 13. 14. 15. 16. 17. 18. 19.]
 [20. 21. 22. 23. 24. 25. 26. 27. 28. 29.]
 [30. 31. 32. 33. 34. 35. 36. 37. 38. 39.]
 [40. 41. 42. 43. 44. 45. 46. 47. 48. 49.]
 [50. 51. 52. 53. 54. 55. 56. 57. 58. 59.]
 [60. 61. 62. 63. 64. 65. 66. 67. 68. 69.]
 [70. 71. 72. 73. 74. 75. 76. 77. 78. 79.]
 [80. 81. 82. 83. 84. 85. 86. 87. 88. 89.]
 [90. 91. 92. 93. 94. 95. 96. 97. 98. 99.]]
In [47]:
%matplotlib inline
from matplotlib import pyplot as plt  #load plotting package

# show the matrix as an image with the routine imshow
plt.imshow(A)  # note that the top left corner has index 0,0
#plt.text(5,10,'top left',color='white')
plt.colorbar(fraction=0.03)
Out[47]:
<matplotlib.colorbar.Colorbar at 0x7f83351bb190>

Problem 5.

Write a subroutine that takes 3 arguments, a prefix string, a suffix string and an integer, and returns a string that starts with the prefix, ends with the suffix and has an integer in the middle that is comprised of six digits and instead of spaces has zeros. For example mysub(’a_’,’.txt’,6) should return ’a_000006.txt’. This is a way to make filenames for sequential outputs of a simulation or a sequential set of images that can later be turned into a movie!

In [38]:
# solution

# make a filename in the form frootnumbersuffix like j0001.txt
# inputs: 
#   fileroot:  a prefix string
#   suffix:  a suffix string
#   index: an integer
# outputs:
#    a string combining the three together
def mkfile(fileroot,suffix,index):
    string = fileroot
    num1 = '{0:d}'.format(index)
    dig = 1000000;  # 1 bigger than max number of digits!
    while (dig >1):
        if (index < dig): 
            string = string + '0'
        
        #print(dig,string)  #for testing!
        dig = dig/10

    string = string + num1 + suffix
    print(string)
    return string
    
# test it out
mkfile('a1_','.png',303)
mkfile('a1_','.png',3)

mkfile('bbbb1_','.png',43)
a1_0000303.png
a1_0000003.png
bbbb1_0000043.png
Out[38]:
'bbbb1_0000043.png'

Problem 6.

In [50]:
# solution
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
def myf(x,mu):
    alpha=3.0
    return mu*x**alpha*(1.0-x**alpha)

mu=3.35
x = np.linspace(0,1,101)
y1 = myf(x,mu) # first iterate
y2 = myf(y1,mu) # second iterate
plt.plot(x,y1, 'g-',label=r'$f(x)$')
plt.plot(x,y2, 'b-',label=r'$f^2(x)$')
plt.plot(x,x, 'k:',label=r'$x$')
plt.xlabel('x')
plt.ylabel('$x, f(x), f^2(x)$',fontsize=22)
plt.legend()
Out[50]:
<matplotlib.legend.Legend at 0x7f83351fadf0>

The plot in blue shows $f^2(x)$ for $\mu=3.35$ and the green shows $f(x)$ for the same $\mu$ value. By runing this a few times with different values of $\mu$ I found that there are only two maxima in $f^2$ if $\mu>3.2$. The intersections of the lines with the $y=x$ line show where there are fixed points. For $\mu=3.5$ there are 4 fixed points. Likely 2 are unstable and 2 are period 2 attracting orbits.

Problem 7. Passing functions to functions.

In [37]:
# solution
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
def fg(f,g,x):
    return f(g(x))
    
x = np.arange(0,10,0.1) # an x vector

def g(x):
    return 10*x*x*np.exp(-x)

y = fg(np.sinh,g,x)  # passing sinh as one argument and my function g above
plt.plot(x,y,'b-')
Out[37]:
[<matplotlib.lines.Line2D at 0x11a4da908>]