CoCalc Public FilesLab 7 / Lab7-turnin.ipynb
Author: Nushrat Esha
Views : 89
Compute Environment: Ubuntu 20.04 (Default)
In [ ]:
# Name: Nushrat Esha
# I worked on this code with: Xavier, Fuijia

# 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


In [29]:
#32
M=matrix([[0, 7], [4, 2]])
type(M)

<class 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
In [30]:
#33
M=matrix(RDF, [[0.57, 0.5025], [0.33, 0.917]])
eig_val=M.eigenvectors_right()
eig_val

[(0.3008632979519208, [(-0.8815235799988788, 0.4721399982059986)], 1), (1.186136702048079, [(-0.6320226312996823, -0.7749499296890257)], 1)]
In [33]:
eigvec_1=eig_val[0][1][0]
eigvec_2=eig_val[1][1][0]
plot(eigvec_1, legend_label="Eigvec 1", legend_color="green", color="green")+plot(eigvec_2, legend_label="Eigvec 2", legend_color="red", color="red")

In [46]:
#34
plot(eigvec_1, legend_label="Eigvec 1", legend_color="green", color="green")+plot(eigvec_2, legend_label="Eigvec 2", legend_color="red", color="red")+plot(M*eigvec_1, legend_label="Eigvec 3", legend_color="purple", color="purple")+plot(M*eigvec_2, legend_label="Eigvec 4", legend_color="blue", color="blue")

In [34]:
#35
show(eigvec_1.norm())
show(eigvec_2.norm())
ratio_eigvec1_eigvec2=eigvec_1.norm()/eigvec_2.norm()
ratio_eigvec1_eigvec2

$1.0$
$0.9999999999999999$
1.0000000000000002
In [42]:
#35
eigvec1_BB=M*eigvec_1
eigvec2_BB=M*eigvec_2

show(eigvec1_BB.norm())
show(eigvec2_BB.norm())

ratio_eigvec1_BB_eigvec2_BB=eigvec1_BB.norm()/eigvec2_BB.norm()
ratio_eigvec1_BB_eigvec2_BB

$0.30086329795192085$
$1.186136702048079$
0.25364976687124346
In [49]:
#36
# The eigenvalues of the black bear population and the lenghts found in question #35 are equal. The black bear matrix eignenvalues are 0.30 and 1.19 and this is equal to the value calculated above.

In [ ]:
#37
# 1. the average adult produces 35315 larvae per month
# 2. the fraction of adults that survive from one month to the next is 0.949
# 3. the fraction of larvae that survive to become juveniles is 0.00003
# 4. the fraction of juveniles in a given month that will still be juveniles the following month is 0.777

In [33]:
#38
M=matrix([[0,0,35315], [0.00003,0.777,0], [0,0.071,0.949]])
M

[ 0.000000000000000 0.000000000000000 35315.0000000000] [0.0000300000000000000 0.777000000000000 0.000000000000000] [ 0.000000000000000 0.0710000000000000 0.949000000000000]
In [3]:
#39
M1=matrix(RDF, [[0,0,35315], [0.00003,0.777,0], [0,0.071,0.949]])
e1=M1.eigenvectors_right()
show(e1)
# the long-term growth rate of the lionfish population is 1.1344782568718599. IF the population keeps growing this way, then the long-term population of the larvae would be 0.9999999988453299, of the juveniles is 8.935490586919178×10^-5, and of the adults is 3.212454346004658×10^-5.

$\left[\left(0.15026156162015658, \left[\left(0.9999999988453299,\,-4.78668582110772 \times 10^{-05},\,4.254893429043022 \times 10^{-06}\right)\right], 1\right), \left(0.44126018150798385, \left[\left(-0.9999999959297882,\,8.935490586919178 \times 10^{-05},\,-1.2494978895992127 \times 10^{-05}\right)\right], 1\right), \left(1.1344782568718599, \left[\left(0.999999995962624,\,8.392118765878533 \times 10^{-05},\,3.212454346004658 \times 10^{-05}\right)\right], 1\right)\right]$
In [4]:
#40 (first initial condition)
v1=vector([3,3,3])
for i in srange(0,100,1):
v1=M1*v1
print(v1[0]/(v1[0]+v1[1]+v1[2]))
print(v1[1]/(v1[0]+v1[1]+v1[2]))
print(v1[2]/(v1[0]+v1[1]+v1[2]))
# the long-term proportion of the larvae population is 0.9998, of the juvenile is 8.391x10^-5, and of the adults is 3.212x10^-5.

0.9998839677334621 8.391145043195256e-05 3.2120816106141214e-05
In [6]:
#40 (second initial condition)
v2=vector([3,4,5])
for i in srange(0,100,1):
v2=M1*v2
print(v2[0]/(v2[0]+v2[1]+v2[2]))
print(v2[1]/(v2[0]+v2[1]+v2[2]))
print(v2[2]/(v2[0]+v2[1]+v2[2]))
# the long-term proportion of the larvae population is 0.9998, of the juvenile is 8.391x10^-5, and of the adults is 3.212x10^-5.

0.9998839677334619 8.391145043195254e-05 3.212081610614121e-05
In [ ]:
#41
# The condition that the dominant eigenvalue matrix population model has to meet in order for the population to be constant is it must equal to 1. For the population to be declining, the value must be less than 1.

In [48]:
#42
L=matrix(RDF, [[0,0,35315], [0.00003, 0.7777, 0], [0, 0.021, 0.949]])          #change percentage of juveniles that transistion to adults
L.eigenvectors_right()
# The method that gives the biggest reduction in the growth rate for a particular change in the parameter is the change in the percentage of juveniles that became adults and the proportion of adults that survived. This dominant eigenvector decreases and it becomes an exponential decay that no longer approaches the eigen vectors.

[(0.032582273370903136, [(0.9999999991890564, -4.0262094033638584e-05, 9.22618528797416e-07)], 1), (0.6608610523412263, [(-0.9999999668611096, 0.0002567636871691776, -1.8713323812573237e-05)], 1), (1.0332566742878704, [(0.9999999926816775, 0.00011739078959314129, 2.925829439972153e-05)], 1)]
In [49]:
L=matrix(RDF, [[0,0,35315], [0.00003, 0.7777, 0], [0, 0.071, 0.949]])       #change amount of larvae that is born each month
L.eigenvectors_right()

[(0.14996906805565002, [(0.9999999988489848, -4.7791176822443295e-05, 4.24661101183727e-06)], 1), (0.4420366101490785, [(-0.999999995927695, 8.937525147188309e-05, -1.2516964699107188e-05)], 1), (1.1346943217952716, [(0.9999999959528739, 8.40349497093419e-05, 3.2130661679258686e-05)], 1)]
In [46]:
L=matrix(RDF, [[0,0,35315], [0.00003, 0.7777, 0], [0, 0.071, 0.543]])      #change the proportion of adults that survive each month
L.eigenvectors_right()

[(0.9634150587880164, [(-0.9999999865806556, -0.0001615377869366164, -2.728061860001746e-05)], 1), (0.17864247060599214 + 0.21485873278498735*I, [(0.9999999988576804, -4.437088503736946e-05 - 1.591411786664526e-05*I, 5.058543689704802e-06 + 6.084064350546512e-06*I)], 1), (0.17864247060599214 - 0.21485873278498735*I, [(0.9999999988576804, -4.437088503736946e-05 + 1.591411786664526e-05*I, 5.058543689704802e-06 - 6.084064350546512e-06*I)], 1)]
In [16]:
#43
@interact
def plotvectors(R=(-10,10,0.1), S=(-10,10,0.1), target=vector([3,-2.4])):
u=R*vector([1,2])
v=S*vector([1,0.5])
p_u=plot(u, color="red", aspect_ratio=1.0, legend_label="u", legend_color="black")
p_v=plot(v, color="green", legend_label="v", legend_color="black")
p_s=plot(R*u+S*v, color="blue", legend_label="sum", legend_color="black")
p_t=plot(target, color="black", legend_label="goal", legend_color="black")
show(p_u + p_v + p_s + p_t)

# (2,3) R is approximately 1.35 and S is approximately 0.65
# (-5,1.2) R is approximately 2.45 and S is approximately -7.5
# (-4,-8) R is approximately -4 and S is approximately 0
# (3,-2.4) R is approximately -1.5 and S is approximately 5.5


In [21]:
#44
@interact
def plotvectors(R=(-5,5,0.1), S=(-5,5,0.1), target=vector([3,-2.4])):
u=R*vector([1,2])
v=S*vector([1,0.5])
p_u=plot(u, color="red", aspect_ratio=1.0, legend_label="u", legend_color="black")
p_v=plot(v, color="green", legend_label="v", legend_color="black")
p_s=plot(R*u+S*v, color="blue", legend_label="sum", legend_color="black")
p_t=plot(target, color="black", legend_label="goal", legend_color="black")
show(p_u + p_v + p_s + p_t)

# (2,3) R is approximately 1.5 and S is approximately 3.5
# (-5,1.2) R is approximately 0.6 and S is approximately 0
# (-4,-8) R is approximately -4 and S is approximately 0
# (3,-2.4) R is approximately -1.3 and S is approximately 5

In [26]:
#45
@interact
def function(R=(-2,5,0.05), S=(0,5,0.05), target=vector([2,3])):
u=(vector([1,2]))
v=(vector([2,4]))
show(plot(R*u+S*v, color="orange")+plot(point(target)))
# The linear combinations of u and v will lie on the same line and this makes it impossible to show any vectors out of this line like the point in this exercise, (2,3), that cannot be reached. The linear combinations are the scalar multiples of u and v.

In [ ]: