Exercises II
Exercises II
matplotlib
1 Exercise 7: Numpy practice (5 points)
Start up Python (best to use Spyder) and use it to answer the following ques-
tions. Use the following imports:
import numpy as np
import scipy.linalg as la
import matplotlib.pyplot as plt
6. What expression will yield the value of the 53th element of meshPoints?
What is this value?
7. Produce a plot of a sinusoid on the interval [−1, 1] using the command
plt.plot(meshPoints,np.sin(2*pi*meshPoints))
Please save this plot as a jpeg (.jpg) file and send it along with your work.
1
vec2 = np.cos( vec2 )
la.norm(vec3)
The matrix A has 5 row sums (one for each row), 5 column sums (one for
each column) and two diagonal sums. These 12 sums should all be exactly
the same, and you could verify that they are the same by printing them
and “seeing” that they are the same. It is easy to miss small differences
among so many numbers, though. Instead, verify that A is a magic
square by constructing the 5 column sums and computing the maximum
and minimum values of the column sums. Do the same for the 5 row sums,
and compute the two diagonal sums. Check that these six values are the
same. If the maximum and minimum values are the same, the flyswatter
2
principle says that all values are the same.
Hints: The function np.diag extracts the diagonal of a matrix, and the
function np.fliplr extracts the other diagonal.
13. The function np.random.rand can be used to construct and fill vectors and
matrices with random numbers. Use the help facility in Python to learn
how to construct a 10 × 10 matrix named M filled with random numbers.
14. The colon and index notation can also be used to refer to a subset of
elements of the array. With the start:increment:finish notation, we can
refer to a range of indices.
What commands would be needed to generate the four 5 × 5 sub-matrices
of M in the upper left quarter MUL, the upper right quarter MUR, the lower
left quarter MLL, and the lower right quarter MLR.
r = r0 + cos θ
x = r cos θ
y = r sin θ