Chapter 3 problems


In [1]:
from sympy import *
init_printing()

In [ ]:


In [2]:
A = Matrix([
[3,     3],
[2, S(3)/2]])
A


Out[2]:
$$\left[\begin{matrix}3 & 3\\2 & \frac{3}{2}\end{matrix}\right]$$

In [3]:
b = Matrix([6,5])

In [4]:
AUG = A.row_join(b)
AUG # the augmented matrix


Out[4]:
$$\left[\begin{matrix}3 & 3 & 6\\2 & \frac{3}{2} & 5\end{matrix}\right]$$

Alice


In [5]:
AUGA = AUG.copy()
AUGA[0,:] = AUGA[0,:]/3
AUGA


Out[5]:
$$\left[\begin{matrix}1 & 1 & 2\\2 & \frac{3}{2} & 5\end{matrix}\right]$$

In [6]:
AUGA[1,:] = AUGA[1,:] - 2*AUGA[0,:]
AUGA


Out[6]:
$$\left[\begin{matrix}1 & 1 & 2\\0 & - \frac{1}{2} & 1\end{matrix}\right]$$

In [7]:
AUGA[1,:] = -2*AUGA[1,:]
AUGA


Out[7]:
$$\left[\begin{matrix}1 & 1 & 2\\0 & 1 & -2\end{matrix}\right]$$

In [8]:
AUGA[0,:] = AUGA[0,:] - AUGA[1,:]
AUGA


Out[8]:
$$\left[\begin{matrix}1 & 0 & 4\\0 & 1 & -2\end{matrix}\right]$$

Bob


In [9]:
AUGB = AUG.copy()
AUGB[0,:] = AUGB[0,:] - AUGB[1,:]
AUGB


Out[9]:
$$\left[\begin{matrix}1 & \frac{3}{2} & 1\\2 & \frac{3}{2} & 5\end{matrix}\right]$$

In [10]:
AUGB[1,:] = AUGB[1,:] - 2*AUGB[0,:]
AUGB


Out[10]:
$$\left[\begin{matrix}1 & \frac{3}{2} & 1\\0 & - \frac{3}{2} & 3\end{matrix}\right]$$

In [11]:
AUGB[1,:] = -1*S(2)/3*AUGB[1,:]
AUGB


Out[11]:
$$\left[\begin{matrix}1 & \frac{3}{2} & 1\\0 & 1 & -2\end{matrix}\right]$$

In [12]:
AUGB[0,:] = AUGB[0,:] - S(3)/2*AUGB[1,:]
AUGB


Out[12]:
$$\left[\begin{matrix}1 & 0 & 4\\0 & 1 & -2\end{matrix}\right]$$

In [ ]:

Charlotte


In [13]:
AUGC = AUG.copy()
AUGC[0,:], AUGC[1,:] = AUGC[1,:], AUGC[0,:]
AUGC


Out[13]:
$$\left[\begin{matrix}2 & \frac{3}{2} & 5\\3 & 3 & 6\end{matrix}\right]$$

In [14]:
AUGC[0,:] = AUGC[0,:]/2
AUGC


Out[14]:
$$\left[\begin{matrix}1 & \frac{3}{4} & \frac{5}{2}\\3 & 3 & 6\end{matrix}\right]$$

In [15]:
AUGC[1,:] = AUGC[1,:] - 3*AUGC[0,:]
AUGC


Out[15]:
$$\left[\begin{matrix}1 & \frac{3}{4} & \frac{5}{2}\\0 & \frac{3}{4} & - \frac{3}{2}\end{matrix}\right]$$

In [16]:
AUGC[1,:] = S(4)/3*AUGC[1,:]
AUGC


Out[16]:
$$\left[\begin{matrix}1 & \frac{3}{4} & \frac{5}{2}\\0 & 1 & -2\end{matrix}\right]$$

In [17]:
AUGC[0,:] = AUGC[0,:] - S(3)/4*AUGC[1,:]
AUGC


Out[17]:
$$\left[\begin{matrix}1 & 0 & 4\\0 & 1 & -2\end{matrix}\right]$$

P3.3


In [18]:
# define agmented matrices for three systems of eqns. with unique sol'ns
A = Matrix([
        [ -1, -2, -2],
        [  3, 3, 0]])
        
B = Matrix([
        [ 1, -1, -2,  1],
        [-2,  3,  3, -1],
        [-1,  0,  1,  2]])

C = Matrix([
        [ 2, -2,  3, 2],
        [ 1, -2, -1, 0],
        [-2,  2,  2, 1]])

In [19]:
A


Out[19]:
$$\left[\begin{matrix}-1 & -2 & -2\\3 & 3 & 0\end{matrix}\right]$$

In [20]:
A.rref()


Out[20]:
$$\left ( \left[\begin{matrix}1 & 0 & -2\\0 & 1 & 2\end{matrix}\right], \quad \left ( 0, \quad 1\right )\right )$$

In [21]:
B


Out[21]:
$$\left[\begin{matrix}1 & -1 & -2 & 1\\-2 & 3 & 3 & -1\\-1 & 0 & 1 & 2\end{matrix}\right]$$

In [22]:
B.rref()


Out[22]:
$$\left ( \left[\begin{matrix}1 & 0 & 0 & -4\\0 & 1 & 0 & -1\\0 & 0 & 1 & -2\end{matrix}\right], \quad \left ( 0, \quad 1, \quad 2\right )\right )$$

In [23]:
C


Out[23]:
$$\left[\begin{matrix}2 & -2 & 3 & 2\\1 & -2 & -1 & 0\\-2 & 2 & 2 & 1\end{matrix}\right]$$

In [24]:
C.rref()


Out[24]:
$$\left ( \left[\begin{matrix}1 & 0 & 0 & - \frac{2}{5}\\0 & 1 & 0 & - \frac{1}{2}\\0 & 0 & 1 & \frac{3}{5}\end{matrix}\right], \quad \left ( 0, \quad 1, \quad 2\right )\right )$$

P3.4


In [25]:
# now for three systems of eqns. with infinitely many sol'ns
D = Matrix([
        [ -1, -2, -2],
        [  3, 6,   6]])
        
E = Matrix([
        [ 1, -1, -2,  1],
        [-2,  3,  3, -1],
        [-1,  2,  1,  0]])

F = Matrix([
        [ 2, -2, 3, 2],
        [ 0,  0, 5, 3],
        [-2,  2, 2, 1]])

Solving d)


In [26]:
D


Out[26]:
$$\left[\begin{matrix}-1 & -2 & -2\\3 & 6 & 6\end{matrix}\right]$$

In [27]:
D.rref()


Out[27]:
$$\left ( \left[\begin{matrix}1 & 2 & 2\\0 & 0 & 0\end{matrix}\right], \quad \left ( 0\right )\right )$$

In [28]:
D[0:2,0:2].nullspace()


Out[28]:
$$\left [ \left[\begin{matrix}-2\\1\end{matrix}\right]\right ]$$

In [29]:
# the solutions to the sytem of equations represented by D
# is of the form    point + nullspace
point = D.rref()[0][:,2]
nullspace = D[0:2,0:2].nullspace()

In [30]:
# the point is also called he particular solution
point


Out[30]:
$$\left[\begin{matrix}2\\0\end{matrix}\right]$$

In [31]:
# if A aug matrix is [A|b], then the point satisfies A*point = b.
print( D[0:2,0:2]*point == D[:,2] )
D[0:2,0:2]*point


True
Out[31]:
$$\left[\begin{matrix}-2\\6\end{matrix}\right]$$

Null space


In [32]:
# the nullspace of A in aug. matrix [A|b] is one dimensional and spanned by
n = nullspace[0]
n
# every vector n in the nullspace of A satisfies  A*n=0


Out[32]:
$$\left[\begin{matrix}-2\\1\end{matrix}\right]$$

In [33]:
# so solution to A*x=b is any (point+s*n) where s is any real number
# since  A*(point +s*n) = A*point + sA*n = A*point + 0 = b.
# verify claim for 20 values of s in range -5,-4,-3,-2,-1,0,1,2,3,4,5
for s in range(-5,6):
    print( D[0:2,0:2]*(point + s*n), 
           D[0:2,0:2]*(point + s*n) == D[:,2] )


Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True
Matrix([[-2], [6]]) True

Solving e)


In [34]:
E


Out[34]:
$$\left[\begin{matrix}1 & -1 & -2 & 1\\-2 & 3 & 3 & -1\\-1 & 2 & 1 & 0\end{matrix}\right]$$

In [35]:
E.rref()


Out[35]:
$$\left ( \left[\begin{matrix}1 & 0 & -3 & 2\\0 & 1 & -1 & 1\\0 & 0 & 0 & 0\end{matrix}\right], \quad \left ( 0, \quad 1\right )\right )$$

In [36]:
point_E = E.rref()[0][:,3]
nullspace_E = E[0:3,0:3].nullspace()[0]
s = symbols('s')
point_E + s*nullspace_E


Out[36]:
$$\left[\begin{matrix}3 s + 2\\s + 1\\s\end{matrix}\right]$$

Solving f)


In [37]:
F


Out[37]:
$$\left[\begin{matrix}2 & -2 & 3 & 2\\0 & 0 & 5 & 3\\-2 & 2 & 2 & 1\end{matrix}\right]$$

In [38]:
F.rref()


Out[38]:
$$\left ( \left[\begin{matrix}1 & -1 & 0 & \frac{1}{10}\\0 & 0 & 1 & \frac{3}{5}\\0 & 0 & 0 & 0\end{matrix}\right], \quad \left ( 0, \quad 2\right )\right )$$

In [39]:
point_F = F.rref()[0][:,3]
nullspace_F = F[0:3,0:3].nullspace()[0]
s = symbols('s')
point_F + s*nullspace_F


Out[39]:
$$\left[\begin{matrix}s + \frac{1}{10}\\s + \frac{3}{5}\\0\end{matrix}\right]$$

In [ ]: