CoCalc Shared FilesLab 3 / Lab3-turnin.sagews
Author: Jamie Lee
Views : 85
# Lab 3:Linear Algebra

# Name: Jamie Lee
# 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

# For lab 3, please do the linear algebra exercises 1-18 only

#1
vector([3, 10])

(3, 10)
#2
v=vector([-2, 7, 5])

type(v)

<type 'sage.modules.vector_integer_dense.Vector_integer_dense'>
#3
a=vector([4, 2])
b=vector([-1, 5])

#3.1
a+b

(3, 7)
#3.2
6*a

(24, 12)
#3.3
4*a+3*b

(13, 23)
#3.4
-b

(1, -5)
#3.5
-a+b

(-5, 3)
#3.6
3.14*a

(12.5600000000000, 6.28000000000000)
#4
v=vector([1, 2, 4])
plot(v)
thickness=4

3D rendering not yet implemented
#5
#5.3.1
plot(a, color= "red")+plot(b, color= "blue")+plot(a+b, color= "purple")

#5.3.2
plot(a, color= "red")+plot(b, color= "blue")+plot(6*a, color= "purple")

#5.3.3
plot(a, color= "red")+plot(b, color= "blue")+plot(4*a+3*b, color= "purple")

#5.3.4
plot(a, color= "red")+plot(b, color= "blue")+plot(-b, color= "purple")

#5.3.5
plot(a, color= "red")+plot(b, color= "blue")+plot(-a+b, color= "purple")

#5.3.6
plot(a, color= "red")+plot(b, color= "blue")+plot(3.14*a, color= "purple")

#6
def lineartest(f):
var("x, y, a")
if f(a*x) == a*f(x) and f(x+y) == f(x)+f(y):
return True
else:
return False

#7
f= 4*x
print(lineartest(f))

True
f= 8+x
print(lineartest(f))

False
f= x-10
print(lineartest(f))

False
#8
def f(v):
l1= [4*v[0]+2*v[1], 3*v[0]-v[1]]
v1= vector(l1)
return(v1)

a= 1
b= 2
f ([1, 2])
f ([0, 0])
f ([2, 5])

(8, 1) (0, 0) (18, 1)
#9
def p(v):
l2= [3*v[0]+5*v[1], 3*v[0]-2*v[1]]
v2= vector(l2)
return(v2)
a1 = vector ([1,2])
a2 = vector ([0,3])
a3 = vector ([3,1])
b1 = p(a1)
b2 = p(a2)
b3 = p(a3)
plot(b1, color = "red") + plot(b2, color = "green") + plot(b3, color = "blue") + plot(a1, color = "red", linestyle = "--") + plot(a2, color = "green", linestyle = "--") + plot(a3, color = "blue", linestyle = "--")

#10
def p(v):
l1= [3*v[0]-8*v[1]-12*v[2], -3*v[0]+2*v[1]+5*v[2], 5*v[0]-6*v[1]+v[2]]
v1= vector(l1)
return(v1)

p(vector([1, 2, 3])) #test

(-49, 16, -4)
#11
def bears(p):
pop = [0.57*p[0]+0.5025*p[1], 0.33*p[0]+0.917*p[1]]
p1= vector(pop)
return(p1)

bears(vector([1,1]))

(1.07250000000000, 1.24700000000000)

#12
bears(vector([13, 20]))

(17.4600000000000, 22.6300000000000)

#13
x1 = vector ([1,0])
x2 = vector ([0,1])
y1 = bears(x1)
y2 = bears(x2)
plot(x1, color = "red", aspect_ratio = 1) + plot(x2, color = "blue", aspect_ratio = 1) + plot(y1, color = "red", aspect_ratio = 1, linestyle = "--") + plot(y2, color = "blue", aspect_ratio = 1, linestyle = "--")

#The result shows the previous vectors increased by 13 and 20, respectively.

#14
x1 = vector([1, 0])
x2 = vector([0, 1])
y1 = 13*bears(x1)
y2 = 20*bears(x2)
plot(x1, color = "red", aspect_ratio=1) + plot(x2, color = "blue", aspect_ratio = 1) + plot(y1, color = "red", aspect_ratio = 1, linestyle = "--") + plot(y2, color = "blue", aspect_ratio = 1, linestyle = "--")

#15
x1 = vector([1, 0])
x2 = vector([0, 1])
y1 = 1*bears(x1)
y2 = 2*bears(x2)
plot(x1, color = "red", aspect_ratio=1) + plot(x2, color = "blue", aspect_ratio = 1) + plot(y1, color = "red", aspect_ratio = 1, linestyle = "--") + plot(y2, color = "blue", aspect_ratio = 1, linestyle = "--")

x1 = vector([1, 0])
x2 = vector([0, 1])
y1 = 2*bears(x1)
y2 = 1*bears(x2)
plot(x1, color = "red", aspect_ratio=1) + plot(x2, color = "blue", aspect_ratio = 1) + plot(y1, color = "red", aspect_ratio = 1, linestyle = "--") + plot(y2, color = "blue", aspect_ratio = 1, linestyle = "--")

#16.a
def ani(x):
dog = [0.4*x[0]-3.43*x[1], 2*x[0]+4.3*x[1]]
cat = vector(dog)
return cat

x1 = vector([1, 0])
x2 = vector([0, 1])
y1 = ani(x1)
y2 = ani(x2)
plot(x1, color = "red", aspect_ratio=1) + plot(x2, color = "blue", aspect_ratio = 1) + plot(y1, color = "red", aspect_ratio = 1, linestyle = "--") + plot(y2, color = "blue", aspect_ratio = 1, linestyle = "--")

x1 = vector([1, 0])
x2 = vector([0, 1])
y1 = 5*ani(x1)
y2 = 10*ani(x2)
plot(x1, color = "red", aspect_ratio=1) + plot(x2, color = "blue", aspect_ratio = 1) + plot(y1, color = "red", aspect_ratio = 1, linestyle = "--") + plot(y2, color = "blue", aspect_ratio = 1, linestyle = "--")

#16.b
def sea(x):
fish = [-0.4*x[0]+3.43*x[1], -2*x[0]-4.3*x[1]]
shark = vector(fish)
return shark

x1 = vector([1, 0])
x2 = vector([0, 1])
y1 = sea(x1)
y2 = sea(x2)
plot(x1, color = "red", aspect_ratio=1) + plot(x2, color = "blue", aspect_ratio = 1) + plot(y1, color = "red", aspect_ratio = 1, linestyle = "--") + plot(y2, color = "blue", aspect_ratio = 1, linestyle = "--")

x1 = vector([1, 0])
x2 = vector([0, 1])
y1 = 5*sea(x1)
y2 = 10*sea(x2)
plot(x1, color = "red", aspect_ratio=1) + plot(x2, color = "blue", aspect_ratio = 1) + plot(y1, color = "red", aspect_ratio = 1, linestyle = "--") + plot(y2, color = "blue", aspect_ratio = 1, linestyle = "--")

#17
def bears(p):
pop = [0.57*p[0]+0.5025*p[1], 0.33*p[0]+0.917*p[1]]
p1= vector(pop)
return(p1)

xb = bears(vector([1, 0]))
yb = bears(vector([0, 1]))

#18
m = column_matrix([xb, yb])
m

[0.570000000000000 0.502500000000000] [0.330000000000000 0.917000000000000]