CoCalc Shared FilesLab 10 / Lab 10-turnin.sagews
Author: Francis Nguyen
Views : 93


# Lab 10

# Name: Francis Nguyen
# 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.

#1
# Xprime represents the plants because they have logistic growth ivolved as well as a saturation death rate that involves being eaten by the herbivore.
# Yprime is the herbivores because their birth rate is proportional to the amount of plants they consume as well as a natural death rate and a saturation function describing their predation by carnivores.
# Zprime represents the carnivores because their birth rate is proportional to the amount of herbivores they consume. In addition, they have a natural death rate and no predation.

#2
_=var('X,Y,Z')
a1=5
b1=3
a2=0.1
b2=2
d1=0.4
d2=0.01
xprime=X*(1-X)-(a1*X*Y)/(1+b1*X)
yprime=(a1*X*Y)/(1+b1*X)-d1*Y-(a2*Y*Z)/(1+b2*Y)
zprime=(a2*Y*Z)/(1+b2*Y)-d2*Z
time=srange(0,1000,1)
sol=desolve_odeint([xprime,yprime,zprime],dvars = [X,Y,Z],times = time,ics = [1,0.5,8])
list_plot(zip(time,sol[:,0]), color="lightsalmon", axes_labels=["Time","Population"], legend_label="Plants", plotjoined=True)+ list_plot(zip(time,sol[:,1]), color="orchid", legend_label="Herbivores", plotjoined=True)+list_plot(zip(time, sol[:,2]), color="lightpink", legend_label="Carnivores", plotjoined=True)
list_plot(sol, axes_labels=["X", "Y", "Z"], plotjoined=True)

3D rendering not yet implemented
#3
sol1=desolve_odeint([xprime,yprime,zprime],dvars = [X,Y,Z],times = time,ics = [1.5,1,4])
list_plot(zip(time,sol[:,0]),color="indigo",axes_labels=["Time","Population"],legend_label="Plants A",plotjoined=True)+list_plot(zip(time,sol[:,1]),color="skyblue",legend_label="Herbivores A",plotjoined=True)+list_plot(zip(time,sol[:,2]),color="orangered",legend_label="Aarnivores A",plotjoined=True)+list_plot(zip(time,sol1[:,0]),color="teal",axes_labels=["Time","Population"],legend_label="Plants B",plotjoined=True)+list_plot(zip(time,sol1[:,1]),color="magenta",legend_label="Herbivores B",plotjoined=True)+list_plot(zip(time,sol1[:,2]),color="lightgreen",legend_label="Carnivores B",plotjoined=True)
list_plot(sol,axes_labels=["X'","Y'","Z'"],plotjoined=True,legend_label="trajectory A")+list_plot(sol1,axes_labels=["X'","Y'","Z'"],plotjoined=True, legend_label="trajectory B",color="lavender")

3D rendering not yet implemented
#4
#The time series looks more similar because there is a bounded region. The trajctories look more different becuase the second trajectory started at a lower state than the first one.

#5
@interact
def fun(b1=(2,3,0.1)):
_=var('X,Y,Z')
a1=5
a2=0.1
b2=2
s1=0.4
d2=0.01
xprime=X*(1-X)-(a1*X*Y)/(1+b1*X)
yprime=(a1*X*Y)/(1+b1*X)-d1*Y-(a2*Y*Z)/(1+b2*Y)
zprime=(a2*Y*Z)/(1+b2*Y)-d2*Z
time=srange(0,1000,1)
solb1=desolve_odeint([xprime,yprime,zprime],dvars = [X,Y,Z],times = time,ics = [2,1.5,7])
show(list_plot(zip(time,solb1[:,0]), color="lavender", axes_labels=["Time", "Population"], legend_label="Plants", plotjoined=True, ymax=12)+list_plot(zip(time,solb1[:,1]), color="midnightblue",legend_label="Herbivores", plotjoined=True)+list_plot(zip(time,solb1[:,2]), color="steelblue", legend_label="Carnivores", plotjoined=True))
show(list_plot(solb1, axes_labels=["X","Y","Z"], plotjoined=True))

Interact: please open in CoCalc
#6
#When you increase the value of b1, the plot begins to exhibit its chaotic and aperiodic oscillatory behavior earlier.

#7
t=srange(0,21,1)
iterate=[0.2]
r=2.5
for i in t[1:]:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
list_plot(zip(t, iterate), plotjoined=True, color="gold")

t=srange(0,21,1)
iterate=[0.2]
r=3.1
for i in t[1:]:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
list_plot(zip(t, iterate),color="firebrick", plotjoined=True)

t=srange(0,21,1)
iterate=[0.2]
r=3.5
for i in t[1:]:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
list_plot(zip(t,iterate),plotjoined=True)

t=srange(0,21,1)
iterate=[0.2]
r=3.9
for i in t[1:]:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
list_plot(zip(t,iterate),color="tan", plotjoined=True)

#For the initial conditions of 2.5, 3.1, 3.5, there are periodic oscillations or no osciallations at all. For intial condition 3.9, there is aperiodic behavior and it demonstrates an aspect of chaos which is sensitive dependency on initial conditions.


#8
t=srange(0,21,1)
iterate1=[0.4]
r=3.9
for i in t[1:]:
f=r*iterate1[-1]*(1-iterate1[-1])
iterate1.append(f)
list_plot(zip(t,iterate1),color="grey",plotjoined=True, axes_labels=["Time","Xn+1"])

t=srange(0,21,1)
iterate2=[0.401]
r=3.9
for i in t[1:]:
f=r*iterate2[-1]*(1-iterate2[-1])
iterate2.append(f)
list_plot(zip(t,iterate2),plotjoined=True,axes_labels=["Time","Xn+1"])+list_plot(zip(t,iterate2),plotjoined=True, color="aquamarine")
#The plot changes completely even though the initial conidition is slightly changed-This illustrates sensitive dependency on initial conditions.

#9
t=srange(0,21,1)
iterate=[0.35]
r=3.8
for i in t[1:]:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
iterate1=[0.36]
for i in t[1:]:
f=r*iterate1[-1]*(1-iterate1[-1])
iterate1.append(f)
list_plot(zip(t,iterate),plotjoined=True,axes_labels=["time","Xn+1"])+list_plot(zip(t,iterate1),plotjoined=True, color="turquoise")

t=srange(0,21,1)
iterate=[0.33]
r=4
for i in t[1:]:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
iterate1=[0.33]
for i in t[1:]:
f=r*iterate1[-1]*(1-iterate1[-1])
iterate1.append(f)
list_plot(zip(t,iterate),plotjoined=True,axes_labels=["time","Xn+1"])+list_plot(zip(t,iterate1),plotjoined=True, color="salmon")

t=srange(0,21,1)
iterate=[0.1]
r=3.75
for i in t[1:]:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
iterate1=[0.2]
for i in t[1:]:
f=r*iterate1[-1]*(1-iterate1[-1])
iterate1.append(f)
list_plot(zip(t,iterate),plotjoined=True,axes_labels=["time","Xn+1"])+list_plot(zip(t,iterate1),plotjoined=True, color="lime")
#As seen, the function is sensitive to the changing of initial conditions. The plots all behave differently or will eventually behave differnetly when the initial conditions are altered. --again, sensitve dependency on initial conditions

#10
time=srange(0,8,1)
difference=[]
for i in time:
s=abs(iterate1[i]-iterate2[i])
difference.append(s)
difference

[0.201000000000000, 0.336776100000000, 0.669016109971719, 0.355258297045168, 0.00838403071103655, 0.0421757273897050, 0.0279624128697432, 0.0896008081618991]
list_plot(difference, plojoined=True, color="lightgreen")

verbose 0 (163: primitive.py, options) WARNING: Ignoring option 'plojoined'=True verbose 0 (163: primitive.py, options) The allowed options for Point set defined by 8 point(s) are: alpha How transparent the point is. faceted If True color the edge of the point. (only for 2D plots) hue The color given as a hue. legend_color The color of the legend text legend_label The label for this item in the legend. marker the marker symbol for 2D plots only (see documentation of plot() for details) markeredgecolorthe color of the marker edge (only for 2D plots) rgbcolor The color as an RGB tuple. size How big the point is (i.e., area in points^2=(1/72 inch)^2). zorder The layer level in which to draw verbose 0 (163: primitive.py, options) WARNING: Ignoring option 'plojoined'=True verbose 0 (163: primitive.py, options) The allowed options for Point set defined by 8 point(s) are: alpha How transparent the point is. faceted If True color the edge of the point. (only for 2D plots) hue The color given as a hue. legend_color The color of the legend text legend_label The label for this item in the legend. marker the marker symbol for 2D plots only (see documentation of plot() for details) markeredgecolorthe color of the marker edge (only for 2D plots) rgbcolor The color as an RGB tuple. size How big the point is (i.e., area in points^2=(1/72 inch)^2). zorder The layer level in which to draw verbose 0 (163: primitive.py, options) WARNING: Ignoring option 'plojoined'=True verbose 0 (163: primitive.py, options) The allowed options for Point set defined by 8 point(s) are: alpha How transparent the point is. faceted If True color the edge of the point. (only for 2D plots) hue The color given as a hue. legend_color The color of the legend text legend_label The label for this item in the legend. marker the marker symbol for 2D plots only (see documentation of plot() for details) markeredgecolorthe color of the marker edge (only for 2D plots) rgbcolor The color as an RGB tuple. size How big the point is (i.e., area in points^2=(1/72 inch)^2). zorder The layer level in which to draw

#FURTHER EXERCISE 7.7.15
v(x,y)=[2*x, 3*y^2-6]
plot_vector_field(v, (x,0,2),(y,0,2), axes_labels=["x","y"], color="seagreen")+ point((0, sqrt(2)), color="magenta", size=30) #Plot vetor field using the differential equations.



v(x,y)=[6*x^2+y, -6*y+x]
plot_vector_field(v, (x,0,2),(y,0,2), axes_labels=["x","y"], color="orangered")+ point((0,0), color="salmon", size=30) #Plot vetor field using the differential equations.

v(x,y)=[6*x^2+y, -6*y+x]
plot_vector_field(v, (x,0,2),(y,0,2), axes_labels=["x","y"], color="LIGHTsalmon")+ point((-1/36,-1/216), color="teal", size=30) #Plot vetor field using the differential equations.


t=srange(0,101,1)
iterate=[0.01]
r=4
for i in t:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
list_plot(zip(t,iterate),plotjoined=True)

t=srange(0,21,1)
iterate=[0.6]
r=4
for i in t:
f=r*iterate[-1]*(1-iterate[-1])
iterate.append(f)
list_plot(zip(t,iterate),plotjoined=True)
iterate

[0.600000000000000, 0.960000000000000, 0.153600000000000, 0.520028160000000, 0.998395491228058, 0.00640773729417265, 0.0254667127877661, 0.0992726373102061, 0.357670323166729, 0.918969052370147, 0.297859732624244, 0.836557249221031, 0.546916871987090, 0.991195228491788, 0.0349089900276012, 0.134761409771416, 0.466403088831347, 0.995484990239702, 0.0179784977886481, 0.0706210856236468, 0.262534991555938, 0.774441479058645]