CoCalc Public FilesLab 4 / Lab4-turnin.sagewsOpen with one click!
Author: Alexis Zukovski User
Views : 203
# Lab 4: # Name: Alexis Zukovski # 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 clearly label which question you are answering as you go and to # use enough comments that you and the grader can understand your code.
#exercise 19 var("x","y")
(x, y)
def lf(v): 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): l1=[1*v[0]+2*v[1],1*v[0]-1*v[1]] #compute new values vf=vector(l1) #vector of new values return vf
#exercise 20 lf2=lf1(lf(v)) lf2
(12*x + y, -3*x + 4*y)
#matrix is clearly [12 1] # [-3 4] #exercise 21 def bearmodel(v): #vector is input l1=([.57*v[0]+.5025*v[1],.33*v[0]+.917*v[1]]) #run through function v1=vector(l1) #make vector with answers from l1 return v1 #show v1
exercise_21=bearmodel(bearmodel(v)) exercise_21
(0.490725000000000*x + 0.747217500000000*y, 0.490710000000000*x + 1.00671400000000*y)
#matrix is clearly [.490725 .747217] # [.49071 1.006714] #exercise 22 mlistM=[[.57,.5025],[.33,.917]] mlistB=[[.45,.4],[.25,.8]] mat1=matrix(mlistM) mat2=matrix(mlistB) mat1 mat2
[0.570000000000000 0.502500000000000] [0.330000000000000 0.917000000000000] [0.450000000000000 0.400000000000000] [0.250000000000000 0.800000000000000]
#exercise 23 mat3=vector([13,20]) Mv=mat1*mat3 Bv=mat2*mat3 Mv Bv
(17.4600000000000, 22.6300000000000) (13.8500000000000, 19.2500000000000)
plot(vector(Mv))+plot(vector(Bv),color="red")
#exercise 24 mat1^2
[0.490725000000000 0.747217500000000] [0.490710000000000 1.00671400000000]
(mat2^2)*(mat1^3)
[0.465164405062500 0.866735053900000] [0.617288232112500 1.15679017462000]
mat1*mat2*mat1
[0.425711250000000 0.769727812500000] [0.500965500000000 0.983574575000000]
#exercise 25 a=mat1^2 b=(mat2^2)*(mat1^3) c=mat1*mat2*mat1 vec1=a*mat3 #yellow vec2=b*mat3 #prange vec3=c*mat3 #cyan plot(vector(vec1),color="yellow")+plot(vector(vec2),color="orange")+plot(vector(vec3),color="cyan") #plot all vectors
#exercise 26 vec1= vector([3,5,1]) vec2=vector([1,-1,6]) dotproduct= 0 values = srange(0,3) for i in values: dotproduct += vec1[i]*vec2[i] dotproduct #gonna run a for loop from 0 to 2 to find v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2] #take product of v1,v2,3 add to existing sum #exercise 29 --> A.dimensions()
4
#exercise 27 def Dot(vector1,vector2): #define function with 2 inputs dotproduct= 0 values = srange(0,vector1.length()) # dimension of both vectors should be same for i in values: dotproduct += vector1[i]*vector2[i] #find dot product of 2 vectors return dotproduct #return answer
vec1= vector([3,5,1]) vec2=vector([1,-1,6]) Dot(vec1,vec2)
4
#exercise 28 A=matrix([[13,-2,5], [2, 1,-7]]) B=matrix([[2,7],[1,-2],[4,1],[3,8]]) a=A.dimensions()[0] #rows in A b=B.dimensions()[1] #columns in B blank=zero_matrix(RR,a,b) blank
[0.000000000000000 0.000000000000000] [0.000000000000000 0.000000000000000]
#exercise 29 #helppp a=A.dimensions()[0] #rows in A b=B.dimensions()[1] #columns in B avalz = srange(0,a) bvalz= srange(0,b) for i in avalz: for j in bvalz: blank[i,j]= Dot(A.row(i),B.column(j)) blank
[ 44.0000000000000 100.000000000000] [-23.0000000000000 5.00000000000000]
#exercise 30 def MatFun(Mat1,Mat2): a=Mat1.dimensions()[0] #rows in A b=Mat2.dimensions()[1] #columns in B avalz = srange(0,a) bvalz= srange(0,b) for i in avalz: for j in bvalz: blank[i,j]= Dot(Mat1.row(i),Mat2.column(j)) return blank
A=matrix([[13,-2,5], [2, 1,-7]]) B=matrix([[2,7],[1,-2],[4,1],[3,8]]) MatFun(A,B)
[ 44.0000000000000 100.000000000000] [-23.0000000000000 5.00000000000000]
C= matrix([[7,-4],[3,12]]) D= matrix([[3,15],[-3,2]]) MatFun(C,D)
[ 33.0000000000000 97.0000000000000] [-27.0000000000000 69.0000000000000]
#exercise 31 def BestMatFun(Mat1,Mat2): a=Mat1.dimensions()[0] #rows in A b=Mat2.dimensions()[1] #columns in B avalz = srange(0,a) #all rows in mat 1 bvalz= srange(0,b) #all columns in mat 2 if Mat1.dimensions()[0] != Mat2.dimensions()[1]: #if rows of mat1 do not equal columns of mat 2... return ("Error") #show error and thats it else: for i in avalz: #for values in the row range for j in bvalz: #for the values in the columns blank[i,j]= Dot(A.row(i),B.column(j)) #put dot product into empty matrix return blank #show matrix
BestMatFun(C,D)
[ 44.0000000000000 100.000000000000] [-23.0000000000000 5.00000000000000]
E= matrix([[7,-4],[3,12]]) F= matrix([[3,15,-3,2]]) BestMatFun(E,F)
'Error'