Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
3632 views
# Lab 7 # Name: Sarah Ahmed # I worked on this code with: Priyanka Bhakta # 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. # Use enough comments that you and the grader can understand your code. # Label axes on all graphs.
#Lab 7: 32-42
#32 M=matrix([[2,3], [4,5]]) #create matrix type(M) #find type of object (should be matrix)
<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
#Eigenvectors example M=matrix(RDF, [[3,1],[-3,7]]) M.eigenvectors_right() #shows that the Eigenvectors are [-.707, -.707] for Eigenvalue 4 and [-.316, -.949] for Eigenvalue 6.
[(4.0, [(-0.7071067811865475, -0.7071067811865475)], 1), (6.0, [(-0.3162277660168379, -0.9486832980505138)], 1)]
#33 M=matrix(RDF, [[.57,.5025], [.33,.917]]) #create matrix as from example M #show matrix Mlist=M.eigenvectors_right() #assign variable to the list of eigenvectors Mlist #show list of eigenvectors evec1=[-.882, .472] #assign variable to coordinates for first eigenvector e1=vector(evec1) #create vector out of eigenvector coordinates evec2=[-.632, -.775] #repeat above two steps for second eigenvector e2=vector(evec2) plot(e1, color="pink") + plot (e2, color="lightskyblue", aspect_ratio=1) #plot eigenvectors
[ 0.57 0.5025] [ 0.33 0.917] [(0.3008632979519208, [(-0.8815235799988788, 0.4721399982059986)], 1), (1.186136702048079, [(-0.6320226312996823, -0.7749499296890257)], 1)]
#34 M1=M*e1 #scale values of first eigenvector M2=M*e2 #scale values of second eigenvector e3=vector(M1) #create vector out of scaled values for each eigenvector e4=vector(M2) plot(e1, color="pink") + plot(e2, color="lightskyblue") + plot(e3, color="lavender") + plot(e4, color="gainsboro") #plot old and new eigenvectors "The first vector (pink) got shorter AKA scaled down AKA decresed while the second vector (light sky blue) got longer AKA scaled up AKA increased."
#35 norm1=e1.norm() #find lengths of eigenvectors using norm norm2=e2.norm() norm3=e3.norm() norm4=e4.norm() "Norms in order of e1, e2, e3, e4" norm1;norm2;norm3;norm4 #list lengths "Ratios of new vector over old vector in order e3/e1 and e4/e2" norm3/norm1 #divide lengths: new over old vector norm4/norm2
'Norms in order of e1, e2, e3, e4' 1.00035393736417 1.00002449969988 0.30103014017868707 1.1861742448861592 'Ratios of new vector over old vector in order e3/e1 and e4/e2' 0.30092363206153844 1.1861451846851174
#36 M=matrix(RDF, [[.57,.5025], [.33,.917]]) #define matrix M.eigenvectors_right() #find eigenvectors and corresponding eigenvalues norm3/norm1 #divide lengths norm4/norm2 "The eigenvalues corresponding to the eigenvectors are nearly exactly the same when found from code and when computed from the ratios."
[(0.3008632979519208, [(-0.8815235799988788, 0.4721399982059986)], 1), (1.186136702048079, [(-0.6320226312996823, -0.7749499296890257)], 1)] 0.30092363206153844 1.1861451846851174 'The eigenvalues corresponding to the eigenvectors are the same when found from code and when computed from the ratios.'
#37 LF=matrix(RDF, [[0,0,35315], [.00003, .777, 0], [0,.071,.949]]) #define matrix LF #show matrix
[ 0.0 0.0 35315.0] [ 3e-05 0.777 0.0] [ 0.0 0.071 0.949]
#37 con't "1. The average adult produces .35315 larvae per month." "2. .949 of the total adults survive month to month." "3. .00003 of the total larvae survive to become juveniles per month." "4. .777 of the total juveniles will still be juveniles in the following month."
'1. The average adult produces .35315 larvae per month.' '2. .949 of the total adults survive month to month.' '3. .00003 of the total larvae survive to become juveniles per month.' '4. .777 of the total juveniles will still be juveniles in the following month.'
#38 LF #already did above
[ 0.0 0.0 35315.0] [ 3e-05 0.777 0.0] [ 0.0 0.071 0.949]
#39 LFev=LF.eigenvalues() #find eigenvalues of lionfish matrix sorted(LFev) #sort eigenvalues from smallest to largest "The dominant eigenvalue is 1.1344, meaning the lionfish population will increase over time."
[0.15026156162015658, 0.44126018150798385, 1.1344782568718599] 'The dominant eigenvalue is .00000102, meaning the lionfish population will decrease over time.'
#ignore def LFpop(v): larvnew=0*v[0]+0*v[1]+35315*v[2] juvenew=.00003*v[0]+.777*v[1]+0*v[2] adultnew=0*v[0]+.071*v[1]+.949*v[2] LFnew=[larvnew,juvenew,adultnew] LFpopnew=vector(LFnew) return LFpopnew v=vector([20,50,30]) #ignore def LFpopiterate(v): larvnew=0*v[0]+0*v[1]+35315*v[2] juvenew=.00003*v[0]+.777*v[1]+0*v[2] adultnew=0*v[0]+.071*v[1]+.949*v[2] LFnew=[larvnew,juvenew,adultnew] LFpopnew=vector(LFnew) LFpopnew "We know the eigenvectors for the lionfish population via the matrix." LF1=vector([1.000, -4.787, 4.255]) LF2=vector([-1.000,8.935,-1.249]) LF3=vector([1.000,8.392,3.212]) LF4=LF1*a1[0] LF5=LF2*a1[1] LF6=LF3*a1[2] norm1=LF4.norm()/LF1.norm() norm2=LF5.norm()/LF2.norm() norm3=LF6.norm()/LF3.norm() if norm1>1: print "population grows" elif norm1==1: print "population remains the same" elif norm1<1: print "population shrinks" if norm2>1: print "population grows" elif norm2==1: print "population remains the same" elif norm2<1: print "population shrinks" if norm3>1: print "population grows" elif norm3==1: print "population remains the same" elif norm3<1: print "population shrinks" else: print "error" #ends here a1=LFpop(v) a1 a2=a1*LF a2 LF.eigenvalues() LF.eigenvectors_right() LF1=vector([1.000, -4.787, 4.255]) LF2=vector([-1.000,8.935,-1.249]) LF3=vector([1.000,8.392,3.212]) p1=plot(LF1, color="red") + plot(LF2, color="orange") + plot(LF3, color="yellow", aspect_ratio=1) "Go from here" a2 LF4=LF1*a1[0] LF5=LF2*a1[1] LF6=LF3*a1[2] norm1=LF4.norm()/LF1.norm() norm2=LF5.norm()/LF2.norm() norm3=LF6.norm()/LF3.norm() if norm1>1: print "population grows" elif norm1==1: print "population remains the same" elif norm1<1: print "population shrinks" if norm2>1: print "population grows" elif norm2==1: print "population remains the same" elif norm2<1: print "population shrinks" if norm3>1: print "population grows" elif norm3==1: print "population remains the same" elif norm3<1: print "population shrinks" else: print "error" "For initial conditions larvae=20, juveniles=50, adults=30, the long-term behavior is that they all grow."
(1.05945000000000e6, 38.8506000000000, 32.0200000000000) (0.001165518, 32.4603362, 37414476780.38698) [0.15026156162015658, 0.44126018150798385, 1.1344782568718599] [(0.15026156162015658, [(0.9999999988453299, -4.78668582110772e-05, 4.254893429043022e-06)], 1), (0.44126018150798385, [(-0.9999999959297882, 8.935490586919178e-05, -1.2494978895992127e-05)], 1), (1.1344782568718599, [(0.9999999959626241, 8.392118765878536e-05, 3.212454346004658e-05)], 1)] 'Go from here' (0.001165518, 32.4603362, 37414476780.38698) population grows population grows population grows 'For initial conditions larvae=20, juveniles=50, adults=30, the long-term behavior is that they all grow.'
#ignore def LFpop(v): larvnew=0*v[0]+0*v[1]+35315*v[2] juvenew=.00003*v[0]+.777*v[1]+0*v[2] adultnew=0*v[0]+.071*v[1]+.949*v[2] LFnew=[larvnew,juvenew,adultnew] LFpopnew=vector(LFnew) return LFpopnew v=vector([20,50,30]) a1=LFpop(v) a1 a2=a1*LF a2 LF.eigenvalues() LF.eigenvectors_right() LF1=vector([1.000, -4.787, 4.255]) LF2=vector([-1.000,8.935,-1.249]) LF3=vector([1.000,8.392,3.212]) p1=plot(LF1, color="red") + plot(LF2, color="orange") + plot(LF3, color="yellow", aspect_ratio=1) LF1new=LF1*20 LF2new=LF2*50 LF3new=LF3*30 p2=plot(LF1new, color="magenta") + plot(LF2new, color="salmon") + plot(LF3new, color="goldenrod", aspect_ratio=1) p1 p2 p1+p2 "For initial conditions larvae=20, juveniles=50, adults=30, the long-term behavior is that they all grow."
(1.05945000000000e6, 38.8506000000000, 32.0200000000000) (0.001165518, 32.4603362, 37414476780.38698) [0.15026156162015658, 0.44126018150798385, 1.1344782568718599] [(0.15026156162015658, [(0.9999999988453299, -4.78668582110772e-05, 4.254893429043022e-06)], 1), (0.44126018150798385, [(-0.9999999959297882, 8.935490586919178e-05, -1.2494978895992127e-05)], 1), (1.1344782568718599, [(0.9999999959626241, 8.392118765878536e-05, 3.212454346004658e-05)], 1)]
3D rendering not yet implemented
3D rendering not yet implemented
3D rendering not yet implemented
'For initial conditions larvae=20, juveniles=50, adults=30, the long-term behavior is that they all grow.'
#ignore def LFpopiterate(v): larvnew=0*v[0]+0*v[1]+35315*v[2] juvenew=.00003*v[0]+.777*v[1]+0*v[2] adultnew=0*v[0]+.071*v[1]+.949*v[2] LFnew=[larvnew,juvenew,adultnew] LFpopnew=vector(LFnew) LFpopnew "We know the eigenvectors for the lionfish population via the matrix." LF1=vector([1.000, -4.787, 4.255]) LF2=vector([-1.000,8.935,-1.249]) LF3=vector([1.000,8.392,3.212]) LF4=LF1*a1[0] LF5=LF2*a1[1] LF6=LF3*a1[2] norm1=LF4.norm()/LF1.norm() norm2=LF5.norm()/LF2.norm() norm3=LF6.norm()/LF3.norm() if norm1>1: print "population grows" elif norm1==1: print "population remains the same" elif norm1<1: print "population shrinks" if norm2>1: print "population grows" elif norm2==1: print "population remains the same" elif norm2<1: print "population shrinks" if norm3>1: print "population grows" elif norm3==1: print "population remains the same" elif norm3<1: print "population shrinks" else: print "error"
#ignore v=[20,50,30] LFpopiterate(v)
population grows population grows population grows
#40 v=vector([10.,14.,13.]) #define vector of initial conditions v1=vector([39.,49.,80.]) #define vector of second set of initial conditions k1=[v[0]/sum(v)] #create lists to hold new values for iterations k2=[v[1]/sum(v)] k3=[v[2]/sum(v)] k4=[v1[0]/sum(v1)] k5=[v1[1]/sum(v1)] k6=[v1[2]/sum(v1)] for i in srange(0,100): #begin for loop for iterations v=vector([10,14,13]) #define vector L=matrix([[0,0,35315], [.00003, .777, 0], [0,.071,.949]]) #define matrix v=L*v #redefine vector k1.append(v[0]/sum(v)) #append values of the new populatio proportions to appropriate lists k2.append(v[1]/sum(v)) k3.append(v[2]/sum(v)) v1=vector([39,49,80]) #repeat above with second set of initial conditions v1=L*v1 k4.append(v1[0]/sum(v1)) k5.append(v1[1]/sum(v1)) k6.append(v1[2]/sum(v1)) print k1[-1]; k2[-1]; k3[-1] #show proportions for first set of initial conditions print k4[-1]; k5[-1]; k6[-1] #show proportions for second set of initial conditions "For the first set of initial conditions, the longterm proportions are .99995 larvae, .00002 juveniles, and .00003 adults." "For the second set of initial conditions, the longterm proportions are .99996 larvae, .00001 juveniles, and .00003 adults."
0.999947270121769 0.0000236938463467597 0.0000290360318844538 0.999958421249606 0.0000134760678619528 0.0000281026825317845 'For the first set of initial conditions, the longterm proportions are .99995 larvae, .00002 juveniles, and .00003 adults.' 'For the second set of initial conditions, the longterm proportions are .99996 larvae, .00001 juveniles, and .00003 adults.'
#41 "For a population to be constant or declining, the dominant eigenvalue has to be equal to or less than one, respectively."
'For a population to be constant or declining, the dominant eigenvalue has to be equal to or less than one, respectively.'
#42 "The three methods I will try are reducing the number of lionfish larvae laid; reducing the number of larvae that survive to be juveniles; and reducing the number of juveniles that survive to be adults." #test out with matrix representing reduced larvae birth rate #tests will compare using second set of initial conditions from 40 v1=vector([39.,49.,80.]) #same code as 40 except it only uses the second set of initial conditions k4=[v1[0]/sum(v1)] k5=[v1[1]/sum(v1)] k6=[v1[2]/sum(v1)] for i in srange(0,100): L=matrix([[0,0,20000], [.00003, .777, 0], [0,.071,.949]]) v1=vector([39,49,80]) v1=L*v1 k4.append(v1[0]/sum(v1)) k5.append(v1[1]/sum(v1)) k6.append(v1[2]/sum(v1)) print k4[-1] #print larvae proportion #test out for reduced # larvae surviving to juvenilehood v1=vector([39.,49.,80.]) #same code as 40 except it only uses the second set of initial conditions k4=[v1[0]/sum(v1)] k5=[v1[1]/sum(v1)] k6=[v1[2]/sum(v1)] for i in srange(0,100): L=matrix([[0,0,20000], [.00003, .777, 0], [0,.071,.949]]) v1=vector([39,49,80]) v1=L*v1 k4.append(v1[0]/sum(v1)) k5.append(v1[1]/sum(v1)) k6.append(v1[2]/sum(v1)) print k5[-1] #print juveniles proportion #test out for reduced # juveniles surviving to adulthood v1=vector([39.,49.,80.]) #same code as 40 except it only uses the second set of initial conditions k4=[v1[0]/sum(v1)] k5=[v1[1]/sum(v1)] k6=[v1[2]/sum(v1)] for i in srange(0,100): L=matrix([[0,0,20000], [.00003, .777, 0], [0,.071,.949]]) v1=vector([39,49,80]) v1=L*v1 k4.append(v1[0]/sum(v1)) k5.append(v1[1]/sum(v1)) k6.append(v1[2]/sum(v1)) print k6[-1] #print adults proportion "The larvae growth rate goes from .9999584 to .9999266 so it is a small degree of change. The juvenile growth rate goes from .00001348 to .00001348 which is not a significant change at all. The adult growth rate goes from .0000281 to .0000277 which is also a small degree of change. The change with the most significant results is the alteration of the number of larvae born, which makes sense because a birthrate lower than whatever the threshhold for maintaining the species population will definitely lead to decline."
'The three methods I will try are reducing the number of lionfish larvae laid; reducing the number of larvae that survive to be juveniles; and reducing the number of juveniles that survive to be adults.' 0.999926584658958 0.0000237946092323904 0.0000496207318095854 'The larvae growth rate goes from .9999584 to .9999266 so it is a small degree of change. The juvenile growth rate goes from .00001348 to .00001348 which is not a significant change at all. The adult growth rate goes from .0000281 to .0000277 which is also a small degree of change. The change with the most significant results is the alteration of the number of larvae born, which makes sense because a birthrate lower than whatever the threshhold for maintaining the species population will definitely lead to decline.'