Week 6

Quiz

  1. Write a function to compute the first $N$ terms of the Fibonacci sequence $$ f_0 = 0, f_1 = 1, f_n = f_{n-2} + f_{n-1} $$.

  2. What is a bifurcation?

  3. What can a bifurcation diagram tell you about a mathematical model?

Answers:

In [2]:
def fibonacci(N):
    sequence = [0,1]
    for i in range(N-1):
        sequence.append(sequence[-2] + sequence[-1])
    return sequence
In [3]:
fibonacci(10)
Out[3]:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
  1. Change in behavior (of a mathematical model when a parameter is changed).
  2. What happens to the behavior of a model as you continuous change a parameter.

MTH 337 (3/5/24)

  • Numpy arrays
    • What are they
    • difference from traditional lists
    • How to make an numpy array
    • Accessing entries
    • Index slicing
    • editing arrays
    • 2-D Numpy array
      • indexing
      • creation
      • broadcasting
      • shape
      • operations
In [8]:
import matplotlib.pyplot as plt
import numpy as np
In [9]:
xlist=[1,2,5,9]
ylist = []
for x in xlist:
    ylist.append(x**2)
plt.plot(xlist,ylist)
plt.show()
In [10]:
xarr = np.array([1,2,5,7,9])
yarr = xarr**2
plt.plot(xarr,yarr)
plt.show()
In [11]:
xlist = [1,2,5,7,9]
ylist = xlist**2
plt.plot(xlist,ylist)
plt.show()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 2
      1 xlist = [1,2,5,7,9]
----> 2 ylist = xlist**2
      3 plt.plot(xlist,ylist)
      4 plt.show()

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
In [12]:
xlist[3]
Out[12]:
7
In [13]:
xarr[3]
Out[13]:
7
In [14]:
mylist = [2,3.5,True,"Cat"]
In [17]:
mybadarr = np.array([2,3.5,True,"Cat",(2,5)])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[17], line 1
----> 1 mybadarr = np.array([2,3.5,True,"Cat",(2,5)])

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (5,) + inhomogeneous part.
In [16]:
mybadarr
Out[16]:
array(['2', '3.5', 'True', 'Cat'], dtype='<U32')
In [18]:
numarr = np.array([1,3,4,5,7])
In [19]:
numarr[0] = 0.75
In [20]:
numarr
Out[20]:
array([0, 3, 4, 5, 7])
In [21]:
int(0.75)
Out[21]:
0
In [22]:
floatarr = np.array([1,2,3,5],dtype=float)
In [23]:
floatarr
Out[23]:
array([1., 2., 3., 5.])
In [24]:
floatarr[0]= 0.75
print(floatarr)
[0.75 2.   3.   5.  ]
In [26]:
[1,2,3]-[1,2,3]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[26], line 1
----> 1 [1,2,3]-[1,2,3]

TypeError: unsupported operand type(s) for -: 'list' and 'list'
In [28]:
np.array([1,2,3])-np.array([1,1,1])
Out[28]:
array([0, 1, 2])
In [29]:
np.array([1,2,3])+np.array([1,1,1])
Out[29]:
array([2, 3, 4])
In [30]:
np.array([1,2,3])*np.array([1,1,1])
Out[30]:
array([1, 2, 3])
In [31]:
np.array([1,2,3])**np.array([1,2,1])
Out[31]:
array([1, 4, 3])
In [32]:
np.array([1,2,3])-np.array([1,1])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[32], line 1
----> 1 np.array([1,2,3])-np.array([1,1])

ValueError: operands could not be broadcast together with shapes (3,) (2,) 
In [34]:
x = np.array([-2,-1,0,1,2,3,4,5])
y = 0.25*x**4 +2/3*x**3-5*x**2+2*x+3
plt.plot(x,y)
plt.show()
In [35]:
xsmooth = np.arange(-5,5,0.5)
print(xsmooth)
[-5.  -4.5 -4.  -3.5 -3.  -2.5 -2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5
  2.   2.5  3.   3.5  4.   4.5]
In [37]:
xsmooth = np.arange(-5,5,0.01)
ysmooth = 0.25 *xsmooth**4 + 2/3*xsmooth**3-5*xsmooth**2+2*xsmooth+3
plt.plot(xsmooth,ysmooth)
plt.show()
In [38]:
np.arange(2,8,1)
Out[38]:
array([2, 3, 4, 5, 6, 7])
In [39]:
np.zeros(4)
Out[39]:
array([0., 0., 0., 0.])
In [40]:
np.ones(5)
Out[40]:
array([1., 1., 1., 1., 1.])
In [41]:
np.ones(6,dtype=int)
Out[41]:
array([1, 1, 1, 1, 1, 1])
In [42]:
longarr=np.arange(0,100,2.5)
In [43]:
longarr[5]
Out[43]:
12.5
In [44]:
longarr[5:10]
Out[44]:
array([12.5, 15. , 17.5, 20. , 22.5])
In [45]:
longarr[5:20:2]
Out[45]:
array([12.5, 17.5, 22.5, 27.5, 32.5, 37.5, 42.5, 47.5])
In [46]:
longarr[:20:2]
Out[46]:
array([ 0.,  5., 10., 15., 20., 25., 30., 35., 40., 45.])
In [47]:
longarr[10::2]
Out[47]:
array([25., 30., 35., 40., 45., 50., 55., 60., 65., 70., 75., 80., 85.,
       90., 95.])
In [48]:
longarr[2:9:3] = 1
In [49]:
print(longarr)
[ 0.   2.5  1.   7.5 10.   1.  15.  17.5  1.  22.5 25.  27.5 30.  32.5
 35.  37.5 40.  42.5 45.  47.5 50.  52.5 55.  57.5 60.  62.5 65.  67.5
 70.  72.5 75.  77.5 80.  82.5 85.  87.5 90.  92.5 95.  97.5]
In [54]:
longarr[1:10:3] = [100,200,300]
print(longarr)
[  0.  100.   20.   30.  200.    1.   15.  300.    1.   22.5  25.   27.5
  30.   32.5  35.   37.5  40.   42.5  45.   47.5  50.   52.5  55.   57.5
  60.   62.5  65.   67.5  70.   72.5  75.   77.5  80.   82.5  85.   87.5
  90.   92.5  95.   97.5]
In [57]:
arr = np.arange(1,10,1)
print(arr)
[1 2 3 4 5 6 7 8 9]
In [59]:
np.roll(arr,2)
Out[59]:
array([8, 9, 1, 2, 3, 4, 5, 6, 7])
In [60]:
np.roll(arr,-2)
Out[60]:
array([3, 4, 5, 6, 7, 8, 9, 1, 2])
In [63]:
TwoDArr = np.array([[1,2,3],
                    [4,5,6],
                    [1,1,2]])
print(TwoDArr)
[[1 2 3]
 [4 5 6]
 [1 1 2]]
In [64]:
TwoDArr[1]
Out[64]:
array([4, 5, 6])
In [65]:
TwoDArr = np.array([[1,2,3],
                    [4,5,6],
                    [1,1,2,7]])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[65], line 1
----> 1 TwoDArr = np.array([[1,2,3],
      2                     [4,5,6],
      3                     [1,1,2,7]])

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.
In [ ]:
TwoDArr = np.array([[1,2,3],
                    [4,5,6],
                    [1,1,2]])
In [66]:
TwoDArr[0,1]
Out[66]:
2
In [68]:
BigArr = np.array([[1,2,3,4,5,6,7,8],
                   [9,1,2,3,4,5,2,5],
                   [2,2,2,2,2,2,2,2],
                   [3,4,3,4,3,4,3,4],])
In [69]:
BigArr[1,3::2]
Out[69]:
array([3, 5, 5])
In [70]:
BigArr[:2,3::2]
Out[70]:
array([[4, 6, 8],
       [3, 5, 5]])
In [71]:
np.zeros((2,3))
Out[71]:
array([[0., 0., 0.],
       [0., 0., 0.]])
In [72]:
np.zeros(2,3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[72], line 1
----> 1 np.zeros(2,3)

TypeError: Cannot interpret '3' as a data type
In [73]:
arr = np.arange(1,37,1)
In [74]:
arr.reshape((4,9))
Out[74]:
array([[ 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]])
In [76]:
BigArr.shape
Out[76]:
(4, 8)
In [77]:
BigArr
Out[77]:
array([[1, 2, 3, 4, 5, 6, 7, 8],
       [9, 1, 2, 3, 4, 5, 2, 5],
       [2, 2, 2, 2, 2, 2, 2, 2],
       [3, 4, 3, 4, 3, 4, 3, 4]])
In [ ]:
(nrow,ncol) = BigArr.shape
mysum = 0
for r in range(nrow):
    for c in range(ncol):
        mysum = mysum + BigArr[r,c]
In [78]:
BigArr.T
Out[78]:
array([[1, 9, 2, 3],
       [2, 1, 2, 4],
       [3, 2, 2, 3],
       [4, 3, 2, 4],
       [5, 4, 2, 3],
       [6, 5, 2, 4],
       [7, 2, 2, 3],
       [8, 5, 2, 4]])

Exercises:

  1. Write a function called ListSqrs(n) that creates a numpy array of the square numbers between 0 and $n^2$, do this in as few lines as possible.
  2. Write a function DrawParabola(a,b,c) that draws the parabola $y=ax^2+bx+c$ so that its vertex is centered and he graph has a width of 10 units and a height of 10 units.
  3. Write a function MyIdent(n) that retrns a numpy array that looks like an $n\times n$ identity matrix. $$ MyIdent(3)= \begin{bmatrix} 1& 0& 0\\ 0& 1& 0\\ 0& 0& 1 \end{bmatrix}$$
  4. Write a function SumEvenIdx(Arr) that takes in a 2d array and returns the sum of the entries in the arrays whose indecies add up to an even number
In [ ]: