Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Assignnment 1

The document contains examples of creating and manipulating matrices in SageMath. In problem 7, it shows how to create matrices filled with specific values, like all 4s. It also shows finding the reduced row echelon form of matrices. In problem 8, it solves a system of equations directly, using the inverse matrix method, and Gaussian elimination. It also solves for a general value of λ. In problem 9, it uses Gaussian elimination to solve the system Ax=b, where A is a given 3x4 matrix and b is a given vector. In problem 10, it uses Gaussian elimination to prove that three given vectors are linearly independent by showing their coefficients must all be zero.

Uploaded by

Evan Grayson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Assignnment 1

The document contains examples of creating and manipulating matrices in SageMath. In problem 7, it shows how to create matrices filled with specific values, like all 4s. It also shows finding the reduced row echelon form of matrices. In problem 8, it solves a system of equations directly, using the inverse matrix method, and Gaussian elimination. It also solves for a general value of λ. In problem 9, it uses Gaussian elimination to solve the system Ax=b, where A is a given 3x4 matrix and b is a given vector. In problem 10, it uses Gaussian elimination to prove that three given vectors are linearly independent by showing their coefficients must all be zero.

Uploaded by

Evan Grayson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 1

### problem 7 a)
%display latex

#Sa= matrix(ZZ, [[4,4,4,4], [4,4,4,4], [4,4,4,4]]) or


Sa= matrix(ZZ, 3,4, lambda i,j: 4)
Sa

[4 4 4 4]
[4 4 4 4]
[4 4 4 4]

# Or we could utilize
Sa= ones_matrix(3,4)*4
Sa

[4 4 4 4]
[4 4 4 4]
[4 4 4 4]

# To get the reduced echelon form we can use the function


Sa.rref()

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

# b) RREF of a 3x4 matrix with Aij=i+j-1

Sb=matrix(3,4, lambda i,j: i+j+1)


Sb

[1 2 3 4]
[2 3 4 5]
[3 4 5 6]

# i and j start from 0 on the first row and column respectively so we


add +2 to the lambda function

Sb.rref()

[ 1 0 -1 -2]
[ 0 1 2 3]
[ 0 0 0 0]

# c) RREF of a 3x4 matrix with Aij= (-1)**j

Sc=matrix(3,4, lambda i,j: (-1)**(j+1))


Sc
[-1 1 -1 1]
[-1 1 -1 1]
[-1 1 -1 1]

Sc.rref()

[ 1 -1 1 -1]
[ 0 0 0 0]
[ 0 0 0 0]

### Problem 8

# p+2q+3r+s = 1
# 2p+q+8r+2s= 2
# p+6q-3r+5s= -2
# 2p-q+r-s= λ with λ= 0 for a,b,c

# a) solve directly

p,q,r,s =SR.var('p,q,r,s')

solve([p+2*q+3*r+s == 1,2*p+q+8*r+2*s == 2,p+6*q-3*r+5*s == -2,2*p-


q+r-s == 0],p,q,r,s)

[[p == (-18/65), q == (17/65), r == (51/130), s == (-11/26)]]

# b) solve using the inverse matrix

A= matrix(QQ, [[1,2,3,1], [2,1,8,2], [1,6,-3,5], [2,-1,1,-1]])


A

[ 1 2 3 1]
[ 2 1 8 2]
[ 1 6 -3 5]
[ 2 -1 1 -1]

B= vector(QQ, [[1], [2], [-2], [0]])


B

(1, 2, -2, 0)

A_inv= A.inverse()
A_inv
# we know that A_inv*A*X = A_inv*B with X=matrix([p], [q], [r],
[s])

[ -6/65 1/65 7/65 31/65]


[ 49/65 -19/65 -3/65 -4/65]
[17/130 4/65 -9/130 -6/65]
[-21/26 5/13 5/26 -1/13]
X= A_inv*B
X

(-18/65, 17/65, 51/130, -11/26)

# or we could use
A**(-1)*B

(-18/65, 17/65, 51/130, -11/26)

# c) solve using the Gaussian Elimination

# we first write the augmented matrix of the system and find its RREF

C= matrix(QQ, [[1,2,3,1],[2,1,8,2],[1,6,-3,5,],[2,-1,1,-1]])
C

[ 1 2 3 1]
[ 2 1 8 2]
[ 1 6 -3 5]
[ 2 -1 1 -1]

CR= C.rref()
CR

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

CB= C.augment(B, subdivide= true)


CB

[ 1 2 3 1| 1]
[ 2 1 8 2| 2]
[ 1 6 -3 5|-2]
[ 2 -1 1 -1| 0]

CBB= CB.rref()
CBB

[ 1 0 0 0|-18/65]
[ 0 1 0 0| 17/65]
[ 0 0 1 0|51/130]
[ 0 0 0 1|-11/26]

# since the first 4 rows are the Identity matrix, the solutions of the
system are row 5
CBB.column(4)

(-18/65, 17/65, 51/130, -11/26)


# d) for general λ, using one of the methods mentioned above

p,q,r,s,λ =SR.var('p,q,r,s,λ')
solve([p+2*q+3*r+s == 1,2*p+q+8*r+2*s == 2,p+6*q-3*r+5*s == -2,2*p-
q+r-s == λ],p,q,r,s,λ)

[[p == 31/65*r1 - 18/65, q == -4/65*r1 + 17/65, r == -6/65*r1 +


51/130, s == -1/13*r1 - 11/26, λ == r1]]
1 Problem 9
 
0 10 0 1 0
A = 0 00 1 1 0
0 10 0 0 1
 
2
b = −1
1
Find a solution to Ax = b, using Gaussian Elimination.
We create the augmented matrix A as
 
0 1 0 0 1 0 2
A = 0 0 0 1 1 0 −1
0 1 0 0 0 1 1
We will then perform Gaussian elimination in order to bring the matrix in
RREF.
For row 1 we use R1 , for row 2 we use R2 , and for row 3 we use R3

R3 = R3 − R1

 
0 1 0 0 1 0 2
A = 0 0 0 1 1 0 −1
0 0 0 0 −1 1 −1
Having finished with the first pivot, we move on to the second and third
rows with the following:
R1 = R1 + R3
R2 = R2 + R3
 
0 1 0 0 0 1 1
A = 0 0 0 1 0 1 −2
0 0 0 0 1 −1 1
Which is now in RREF format.

For Ax = b we have :
 
X1
  X2   
0 1 0 0 0 1  
X3  1
0 0 0 1 0 1 ·
  = −2
X4 
0 0 0 0 1 −1  
X5  1
X6
With back substitution we get:

1
   
X2 + X6 1
X4 + X6  = −2
X5 − X6 1
Since X2 , X4 , X5 are pivots, we can set the free variable as being equal to
a constant:
X6 = c
Therefore our solutions are

X2 = 1 − c

X4 = −2 − c
X5 = 1 + c

2
2 Problem 10
In order to prove that  
2
x1 = −1
3
 
1
x2 =  1 
−2
 
3
x3 = −3
8
Are linearly independent
For that we need:
λ1 · x1 + λ2 · x2 + λ3 · x3 = 0
      
2 1 3 0
λ1 −1 + λ2  1  + λ3 −3 = 0
3 −2 8 0
Which is the equivalent of
       
2λ1 λ2 3λ3 0
−λ1  +  λ2  + −3λ3  = 0
3λ1 −2λ2 8λ3 0
Which leads us to

2λ1 + λ2 + 3λ3 = 0
−λ1 + λ2 − 3λ3 = 0
3λ1 − 2λ2 + 8λ3 = 0
Out of which we get the new augmented Matrix
 
2 1 3 0
A = −1 1 −3 0
3 −2 8 0
We will use Gaussian elimination to get the RREF form of the matrix as
such:

R2 = R2 + R1 /2
R3 = R3 − 3R1 /2
With R1 is the first row of the table, R2 is the second row, etc.

3
 
2 1 3 0
A = 0 3/2 −3/2 0
0 −7/2 7/2 0
Likewise, to find the pivots we proceed with:

R1 = R1 − 2R2 /3
R3 = R3 + 7R2 /3
 
0 0 4 0
A = 0 3/2 −3/2 0
0 0 0 0
To turn the pivots into 1, we use:

R1 = R1 /2

R2 = 2R2 /3
Finally the RREF matrix will be
 
1 0 2 0
A = 0 1 −1 0
0 0 0 0
This leans that:
λ1 + 2λ3 = 0
λ2 − λ3 = 0
Therefore, by setting the free variable as λ3 = c, we get:

λ2 = c

λ1 = −2c
Since λ1 , λ2 , λ3 are not zero, we can discern that
 
2
x1 = −1
3
 
1
x2 =  1 
−2
 
3
x3 = −3
8
Are not linearly independent.

You might also like