#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
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.
# 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]))
for i in range(0,10,2):
print(i)
# b) solution
# syntax arange(start, stop+dx, dx)
x= np.arange(0,10.1,0.1) # make the x array
x # print the array
# b) here is another way to do it!
x= np.linspace(0,10.0,101)
# syntax numpy.linspace(start, stop, num=N)
print(x)
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).
#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
# 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)
%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)
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!
# 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)
# 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()
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.
# 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-')