Problem 1 Instructions

Create the following matrices without explicitly initializing them. Instead create a matrix of ones or zeros and use slicing to create the requested pattern. Print the matrix after creating it. Note that to access diagonals, you will have to create the indices first ( diag_indices ) and then slice using them.

1.1

10x10 matrix whose second column is the powers of 3, diagonal is all 4's, and all other elements are zero


In [1]:
import numpy as np

m = np.zeros( (10,10) )
m[:,1] = 3
m[np.diag_indices(10)] = 4
print(m)


[[ 4.  3.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  4.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  3.  4.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  4.  0.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  4.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  4.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  4.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  4.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  0.  4.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  0.  0.  4.]]

1.2

12x12 matrix whose columns sum to 1, whose diagonals are 0 and off-diagonals are strictly positive. Use np.round(mat_name,2) to only have two decimals when printing.


In [2]:
import numpy as np

s = 12
m = np.ones( (s,s) )
m[np.diag_indices(s)] = 0
for i in range(s):
    m[:,i] /= np.sum(m[:,i])
print(np.round(m,2))


[[ 0.    0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09]
 [ 0.09  0.    0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09]
 [ 0.09  0.09  0.    0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09]
 [ 0.09  0.09  0.09  0.    0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09]
 [ 0.09  0.09  0.09  0.09  0.    0.09  0.09  0.09  0.09  0.09  0.09  0.09]
 [ 0.09  0.09  0.09  0.09  0.09  0.    0.09  0.09  0.09  0.09  0.09  0.09]
 [ 0.09  0.09  0.09  0.09  0.09  0.09  0.    0.09  0.09  0.09  0.09  0.09]
 [ 0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.    0.09  0.09  0.09  0.09]
 [ 0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.    0.09  0.09  0.09]
 [ 0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.    0.09  0.09]
 [ 0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.    0.09]
 [ 0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.09  0.  ]]

1.3

10x3 matrix where all elements are 1 except in the second column, where every other element is 0


In [3]:
import numpy as np

m = np.ones( (10,3) )
m[:-1:2,1] = 0
print(m)


[[ 1.  0.  1.]
 [ 1.  1.  1.]
 [ 1.  0.  1.]
 [ 1.  1.  1.]
 [ 1.  0.  1.]
 [ 1.  1.  1.]
 [ 1.  0.  1.]
 [ 1.  1.  1.]
 [ 1.  0.  1.]
 [ 1.  1.  1.]]

Problem 2 Instructions

Solve the following system of equations:

$$\begin{array}{ll} 3 x - 10 \sin y &= 4t\\ x - z &= 6t - 3 \\ 4t + 2z - 4\sin y &= x - 4\\ x + z + 2&= 3t \\ \end{array}$$

To answer, first rearrange the equations so that they are in standard form with constants on the right and all coefficients are explicit on the left (first cell). Solve in Python (second cell) and report the answer in LaTeX (last cell).

$$\begin{array}{ll} 3 x - 10 \sin y + 0z - 4t &= 0\\ 1x + 0\sin y - 1 z - 6t &= - 3 \\ -1x -4 \sin y + 2z + 4t &= - 4\\ 1x + 0\sin y + 1z - 3t&= -2 \\ \end{array}$$

In [4]:
import numpy as np
import numpy.linalg as  lin

A = np.array([[3, -10, 0, 4], [1, 0, -1, -6], [-1, -4, 2, 4], [1, 0, 1, -3]])
Ainv=  lin.inv(A)
B = np.array([0, -3, -4, -2])
x = Ainv.dot(B)
print(x)
print(x[0], np.arcsin(x[1]), x[2], x[3])


[ 2.  1. -1.  1.]
2.0 1.57079632679 -1.0 1.0
$$ x = 2,\, y = \frac{\pi}{2},\, z = -1,\, t = 1 $$

Problem 3 Instructions

Calculate the eigenvalues and eigenvectors for the following expressions. Solve in Python and then write out the eigenvalues/eigenvectors in LaTeX. When writing decimals, only report two significant figures.

3.1

$$A = \left[\begin{array}{lcr} 2 & 1 & 1\\ 2 & -4 & 2\\ 4 & 3 & -5\\ \end{array}\right]$$

In [5]:
import numpy as np
import numpy.linalg as  lin

A = np.array([[2, 1, 1], [2, -4, 2], [4, 3, -5]])
lin.eig(A)


Out[5]:
(array([ 3.13090791, -3.08053983, -7.05036808]),
 array([[ 0.77639311,  0.26691702, -0.03470413],
        [ 0.36237847, -0.74614174, -0.53196789],
        [ 0.51565064, -0.60994083,  0.84605307]]))
$$\lambda_1 = 3.1$$$$ v_1 = \left[0.78, 0.36, 0.52\right] $$$$\lambda_2 = -3.1$$$$ v_2 = \left[0.27, -0.75, -0.61\right] $$$$\lambda_3 = -7.1$$$$ v_3 = \left[-0.034, -0.53, 0.85\right] $$

3.2

$$A = \left[\begin{array}{lcr} 4 & 3 & 4\\ 6 & -4 & 3\\ 2 & -1 & -5\\ \end{array}\right] - \left[\begin{array}{lcr} 1 & 3 & 1\\ 3 & -4 & 1\\ 4 & 3 & -5\\ \end{array}\right]$$

In [6]:
A = np.array([[4, 3, 4], [6, -4, 3], [2, -1, -5]]) -  np.array([[1, 3, 1], [3, -4, 1], [4, 3, -5]])
lin.eig(A)


Out[6]:
(array([ 1.85978745+3.63561596j,  1.85978745-3.63561596j, -0.71957490+0.j        ]),
 array([[-0.16558262-0.52796719j, -0.16558262+0.52796719j, -0.57165827+0.j        ],
        [-0.24395557-0.37475939j, -0.24395557+0.37475939j,  0.41333336+0.j        ],
        [ 0.70276178+0.j        ,  0.70276178-0.j        ,  0.70877525+0.j        ]]))
$$\lambda_1 = 3.4 + 4.1 i$$$$ v_1 = \left[0.27 - 0.35 i, -0.031 - 0.21 i, 0.87\right] $$$$\lambda_2 = 3.4 - 4.1 i$$$$ v_2 = \left[0.27 + 0.35 i, -0.031 + 0.21 i, 0.87\right] $$$$\lambda_3 = 4.2$$$$ v_3 = \left[-0.74, 0.65, 0.15\right] $$

3.3

$$ A = \left[\begin{array}{lcr} 4 & 3 & 4\\ 6 & -4 & 3\\ 2 & -1 & -5\\ \end{array}\right] - \left(\frac{3 \sqrt{273} - 29}{8} \right)\left[\begin{array}{lcr} 0 & 1 & 0\\ 0 & 0 & 0\\ 0 & 0 & 0\\ \end{array}\right] $$

In [7]:
#approximately Hermitian since complex portions are miniscule, so use that instead!
A = np.array([[1,0,-2], [0,-1,3], [-2,2,1]]) + 1/8 *  (3 *  np.sqrt(273) - 29) * np.array([[0,1,0], [0,0,0], [0,0,0]])
lin.eigh(A)


Out[7]:
(array([-2.60387547,  0.10991626,  3.49395921]),
 array([[ 0.32798528, -0.73697623, -0.59100905],
        [-0.73697623, -0.59100905,  0.32798528],
        [ 0.59100905, -0.32798528,  0.73697623]]))
$$\lambda_1 = -2.6$$$$ v_1 = \left[0.33, -0.74, -0.59\right] $$$$\lambda_2 = 0.11$$$$ v_2 = \left[-0.74, -0.59, -0.33\right] $$$$\lambda_3 = 3.5$$$$ v_3 = \left[-0.59, 0.33, 0.74\right] $$