mscroggs.co.uk
mscroggs.co.uk

subscribe

MATH0011: Mathematical Methods II

Code we wrote during lecture 4

 python 
# Commonly asked questions
# != means not equal
= 4
= 5
if a != b:
    print(a)
# ! is not a factorial. If you want to use a factorial:
import math
print(math.factorial(4))

"if a" is equivalent to "if a == True"
= True
if a:
    print("Hello")
if a == True:
    print("hello again")

"if not a" is equivalent to "if a == False"
= False
if not a:
    print("Hello")
if a == False:
    print("hello again")

## numpy
# Vectors
import numpy as np
= np.array([1,2,3])
= np.array([0,1,5])
print(a)
print(b)

# adding and subtracting vectors, multiplying by a scalar
print(a+b)
print(a-b)
print(2*a)

#dot and cross products
print(np.dot(a,b))
print(a.dot(b))
print(np.cross(a,b))

= np.array([1,2,3,4,5,6,7])
= np.array([0,0,1,1,0,1,0])
print(np.dot(a,b))
print(a+b)

# Matrices
import numpy as np
= np.array([[1,0],[1,1]])
= np.array([[4,0],[0,2]])
print(a)
print(b)

# adding matrices, multiplying by a scalar, and matrix-matrix multiplication
print(a+b)
print(4*b)
print(a.dot(b))

# Transposing a matrix
print(a)
print(a.T)

# matrix vector multiplication
matrix = np.array([[4,1,0],[0,2,0],[1,1,2]])
vector = np.array([1,0,1])
print(matrix.dot(vector))

# inverting a matrix
= np.array([[1,0],[1,1]])
a_inv = np.linalg.inv(a)
print(a)
print(a_inv)
print(a.dot(a_inv))

= np.array([[1,0,1],[5,5,5],[-1,2,0]])
print(np.linalg.inv(b))

# The following will raise an exception because the matrix is singular
= np.array([[1,2],[1,2]])
print(np.linalg.inv(c))

# Finding the determinant of a vector
= np.array([[1,0],[1,1]])
print(a)
print(np.linalg.det(a))

# Finding the eigenvectors and eigenvalues of a matrix
print(np.linalg.eig(a))
print(np.linalg.eig(a)[0]) # this prints eigenvalues
print(np.linalg.eig(a)[1]) # this prints eigenvectors

# Making a vector of 0s (and changing one 0 to a 5)
= np.zeros(10)
a[1] = 5
print(a)

# Making a matrix of 0s (and changing one 0 to a 3)
= np.zeros([3,10])
print(b)
b[0,1] = 3
print(b)

# Making a vector and matrix full of 1s
= np.ones(4)
print(a)
= np.ones([3,5])
print(b)

# Making an identity matrix
= np.eye(3)
print(a)

# Making a random vector and matrix
= np.random.rand(3)
print(a)
= np.random.rand(3,3)
print(b)


## matplotlib
import matplotlib.pylab as plt

# plot is given list of x coordinates and list of y coordinates
plt.plot([1,2,3],[2,1,1.5])
# save the figure as an image
plt.savefig("example.png")
# show the plot
plt.show()

# this should be called between plots to clear the plot
# (I added these after the lecture to make this work if you run it all at once)
plt.clf()

# changing the plot style
plt.plot([1,2,3],[2,1,1],linewidth=2,color="magenta",marker="o",markersize=40)
plt.plot([1,2,3],[2,1,1],"ro-")
plt.plot([3,1,3],[1,2,3],"bx")
plt.plot([0,2,4],[0,3,0],"k--")
plt.show()

plt.clf()

# Adding axis labels and a title
plt.plot([1,2,3],[2,1,1],"ro-")
plt.xlabel("Numbers")
plt.ylabel("Different numbers")
plt.title("This is the title")
plt.show()

plt.clf()

# Making the axes equal aspect
plt.plot([1,2,3],[20,1,1],"ro-")
plt.axis("equal")
plt.show()

plt.clf()

# Making the axes log-scaled
plt.plot([1,2,3],[20,1,1],"ro-")
plt.xscale("log")
plt.yscale("log")
plt.show()

plt.clf()

# Setting the limits of the x and y axes
plt.plot([1,2,3],[20,1,1],"ro-")
plt.ylim([-1,2])
plt.xlim([-10,10])
plt.show()

plt.clf()

# plot y=sin(x) between -5 and 5
import matplotlib.pylab as plt
from math import sin

= []
= []

= -5
while i <= 5:
    x.append(i)
    y.append(sin(i))
    i += 0.1

plt.plot(x,y,"r-")
plt.show()

plt.clf()

# plot a unit circle
import matplotlib.pylab as plt
import math
= []
= []

= 0
while a<=1.01:
    x.append(math.sin(2*math.pi*a))
    y.append(math.cos(2*math.pi*a))
    a += 0.01

plt.plot(x,y,"g-")
plt.axis("equal")
plt.show()

plt.clf()
© Matthew Scroggs 2012–2024