SharedLab 6 / Lab6-turnin.sagewsOpen in CoCalc
# Lab 6

# Name: Shanmitha Arun
# I worked on this code with:

# Please do all of your work for this week's lab in this worksheet. If
# you wish to create other worksheets for scratch work, you can, but
# this is the one that will be graded. You do not need to do anything
# to turn in your lab. It will be collected by your TA at the beginning
# of (or right before) next week’s lab.

# Be sure to put each exercise in a new cell with an exercise number at the top.
# Label axes on all graphs.


# 19
def lf(v): #define a function
l1=[2*v[0]+3*v[1],5*v[0]-v[1]] #compute new values
v1=vector(l1) #make vector of new values
return v1

def lf1(v):
lf1=[1*v[0]+2*v[1],1*v[0]-1*v[1]] #compute new values
v2=vector(lf1) #make vector of new values
return v2

#20
var("x","y") #symbolic variables
v=vector([x,y]) #create a vector
lf2=lf1(lf(v)) #take the function of another function

(x, y)
column_matrix(lf2) #make a matrix

[ 12*x + y] [-3*x + 4*y]
#21
def f(v):
l1=[0.57*v[0]+0.5025*v[1],0.33*v[0]+0.917*v[1]] #compute new values based on j(t+1) and a(t+1) equations
v1=vector(l1) #make vector of new values
return v1
var("x","y") #symbolic variables
v=vector([x,y]) #create a vector
v2=f(f(v)) #take the function of the function
column_matrix(v2) #make a matrix

(x, y) [0.490725000000000*x + 0.747217500000000*y] [ 0.490710000000000*x + 1.00671400000000*y]
#22
mlist=[[0.57,0.5025],[0.33,0.917]] #list
M=matrix(mlist) #matrix of list
M

[0.570000000000000 0.502500000000000] [0.330000000000000 0.917000000000000]
blist=[[0.45,0.4],[0.25,0.8]] #list
B=matrix(blist) #matrix of list
B

[0.450000000000000 0.400000000000000] [0.250000000000000 0.800000000000000]
#23
v=vector([13,20]) #create a vector
v

(13, 20)
M*v #multiply matrix M by vector

(17.4600000000000, 22.6300000000000)
plot(M*v,aspect_ratio=1) #plot

B*v #multiply matrix B by vector

(13.8500000000000, 19.2500000000000)
plot(B*v,aspect_ratio=1) #plot

#24
#24.1
M^2

[0.490725000000000 0.747217500000000] [0.490710000000000 1.00671400000000]
#24.2
(B^2)*(M^3)

[0.465164405062500 0.866735053900000] [0.617288232112500 1.15679017462000]
#24.3
M*B*M

[0.425711250000000 0.769727812500000] [0.500965500000000 0.983574575000000]
#25
r=(M^2)*v
r

(21.3237750000000, 26.5135100000000)
plot(r,aspect_ratio=1) #plot r

s=(B^2)*(M^3)*v
s

(23.3818383438125, 31.1605505098625)
plot(s,aspect_ratio=1) #plot s

t=M*B*M*v
t

(20.9288025000000, 26.1840430000000)
plot(t,aspect_ratio=1) #plot t

#26
v1=vector([3,5,1]) #vector 1
v2=vector([1,-1,6]) #vector 2
sum=0 #sum starts off as 0
for i in srange(0,3): #3 values in vector
sum=sum+v1[i]*v2[i] #multiply each value of v1 with corresponding value of v2
sum

4
#27
def dotproduct(v1,v2): #define a function
if len(v1)==len(v2): #make sure lengths are same to be multiplied
sum=0 #sum starts off as 0
for i in srange(0,len(v1)): #based on # of values in vector
sum=sum+v1[i]*v2[i] #multiply each value of v1 with corresponding value of v2
return sum
else:
print "cannot compute value because dimensions do not match" #if lengths do not match
dotproduct(v1,v2)

4
#28
A=matrix([[3,1],[1,4]]) #matrix A
B=matrix([[1,2],[4,5]]) #matrix B
emptymatrix=zero_matrix(RR,A.dimensions()[0],B.dimensions()[1]) #zero matrix
emptymatrix

[0.000000000000000 0.000000000000000] [0.000000000000000 0.000000000000000]
#29
for i in srange(0,A.dimensions()[0]): #iterates for each row of first vector
for j in srange(0,B.dimensions()[1]): #for each row, iterates for each column of second vector
emptymatrix[i,j] = dotfunction(A.row(i),B.column(j)) #adds dot product to the correct position of the empty matrix
emptymatrix

[7.00000000000000 11.0000000000000] [17.0000000000000 22.0000000000000]
#30
def mm(m1,m2): #function with 2 matrices as input
emptmat=zero_matrix(RR, m1.dimensions()[0], m2.dimensions()[1]) #empty matrix with correct dimensions
for i in srange(0,m1.dimensions()[0]): #iterates for each row of first vector
for j in srange(0,m2.dimensions()[1]): #for each row, iterates for each column of second vector
emptmat[i,j]=dotproduct(m1.row(i),m2.column(j)) #adds dot product to correct position of empty matrix
return emptmat
mm(A,B)

[7.00000000000000 11.0000000000000] [17.0000000000000 22.0000000000000]
g=matrix([[3,2],[8,2]])
h=matrix([[-2,3],[3,-5]])
mm(g,h)

[0.000000000000000 -1.00000000000000] [-10.0000000000000 14.0000000000000]
r=matrix([[3,2,8],[2,3,7]])
s=matrix([[1,2],[3,4],[5,2]])
mm(r,s)

[49.0000000000000 30.0000000000000] [46.0000000000000 30.0000000000000]
t=matrix([[2,3],[4,5],[1,7]])
y=matrix([[1,6,7],[2,3,4]])
mm(t,y)

[8.00000000000000 21.0000000000000 26.0000000000000] [14.0000000000000 39.0000000000000 48.0000000000000] [15.0000000000000 27.0000000000000 35.0000000000000]
#31
def mm(m1,m2):
if m1.dimensions()[1]==m2.dimensions()[0]: #ensures that dimensions can be multiplied together
emptmat=zero_matrix(RR,(m1.dimensions())[0], (m2.dimensions())[1]) #empty matrix with correct dimensions
for i in srange(0,m1.dimensions()[0]): #iterates for each row of first vector
for j in srange(0,m2.dimensions()[1]): #for each row, iterates for each column of second vector
emptmat[i,j]=dotproduct(m1.row(i),m2.column(j))
return emptmat
else:
print "Matrices cannot be multiplied together"

#32
M=matrix([[2,1],[3,6]])
type(M) #type of matrix

<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
type(M.dimensions())

<type 'tuple'>