CoCalc Shared FilesLab 5 Group 1.sagews
Views : 34
#Lab 5 Group 1
#Zoe Woolf, Jose Hopkins, Michael Sandoval, Nick Scheier

#Romeo and Juliet

s=var('R,J') #R and J are the variables
t=srange(0,20,0.05) # we will use t for desolve_odeint. It is the range of time values
# for which we will solve the differential equations.

ic1=[.5,.6] # our 4 initial conditions. Play around with different values for these.
ic2=[.5,-.2]
ic3=[-.9,.6]
ic4=[-.5,-.7]

@interact
def Romeo_Juliet(a=slider(-2,2,0.5,default=-1),
b=slider(-2,2,0.5,default=0),
c=slider(-2,2,0.5,default=0),
d=slider(-2,2,0.5,default=-1)):
# use 4 sliders for the coefficients, set the default values
# for a,d to -1, b and c to 0.

titleeq= ("$R'= "+'%2.1f'%(a)+" R "+"%+2.1f"%(b)+" J$\n"
+"$J' = "+'%2.1f'%(c)+" R "+"%+2.1f"%(d)+" J$")
# some fancy formatting stuff to create a title with our differential equations
vector_field=[a*R+b*J,c*R+d*J]
# our differential equations
pv=plot_vector_field(vector_field,(R,-1,1),(J,-1,1),plot_points=11,axes_labels=["R","J"])
# plot the vector field,R and J both going from -1 to 1, using 11x11 arrows
sol1=desolve_odeint(vector_field,times=t,ics=ic1,dvars=[R,J])
# solve the differential equation starting at ic1, for times in t
ps1=list_plot(sol1,plotjoined=True,color='blue')+disk(ic1,.03,(0,2*pi),color='blue')
# plot a trajectory starting at ic2, with a starting dot
sol2=desolve_odeint(vector_field,times=t,ics=ic2,dvars=[R,J])
# solve the differential equation starting at ic2, for times in t
ps2=list_plot(sol2,plotjoined=True,color='red')+disk(ic2,.03,(0,2*pi),color='red')
# plot a trajectory starting at ic2, with a starting dot
sol3=desolve_odeint(vector_field,times=t,ics=ic3,dvars=[R,J])
# solve the differential equation starting at ic3, for times in t
ps3=list_plot(sol3,plotjoined=True,color='green')+disk(ic3,.03,(0,2*pi),color='green')
#plot a trajectory starting at ic3, with a starting dot
sol4=desolve_odeint(vector_field,times=t,ics=ic4,dvars=[R,J])
# solve the differential equation starting at ic4, for times in t
ps4=list_plot(sol4,plotjoined=True,color='purple')+disk(ic4,.03,(0,2*pi),color='purple')
#a trajectory starting at ic2, with a starting dot
show(pv+ps1+ps2+ps3+ps4,xmin=-1,xmax=1,ymin=-1,ymax=1,aspect_ratio=1,title=titleeq,figsize=8,fontsize=20)  #"R'=aR+bJ\n J'=cR+dJ")
# combine and show the plots but limit the range of x and of y, add the title etc.


#Deer and Moose Code 1

vs=var("D,M")
t=srange(0,50,0.05)
Dmax=3.1
Mmax=2.5

@interact # use the slider function to specify defaults and fancy labels

def Deer_Moose(r_D=slider(0,5,0.5,default=3,label="$r_D$"),
r_M=slider(0,5,0.5,default=2,label="$r_M$"),
alpha=slider(0,2,0.5,default=0.5,label="$\\alpha$"),
D0=slider(0,Dmax,0.1,default=1,label="$D_0$"),
M0=slider(0,Mmax,0.1,default=1,label="$M_0$")):
titleeq=("$D'= " + '%2.1f'%(r_D) + " D - M D - D^2$\n " +
"$M'= " + '%2.1f'%(r_M) + " M - " + '%2.1f'%(alpha) + " M D - M^2$")
# make a title for the plot which has the differential equations.

vector_field=[r_D*D-M*D-D^2,r_M*M-alpha*M*D-M^2]
initial_value=[D0,M0]
pv=plot_vector_field(vector_field,(D,0,Dmax),(M,0,Mmax),
plot_points=11,axes_labels=["D","M"])
sol1=desolve_odeint(vector_field,times=t,ics=initial_value,dvars=[D,M])
ps1=list_plot(sol1,plotjoined=True,color='green',thickness=2)
d1=disk(initial_value,.035,(0,2*pi),color='green')
nullclines=( plot(r_D-D,(D,0,r_D),color="red",thickness=3)
+plot(r_M-alpha*D,(D,0,r_M/alpha),color="blue",thickness=3))
# make plots of the nullcline lines (excluding D=0 and M=0)
show(pv+ps1+d1+nullclines,xmin=0,xmax=Dmax,ymin=0,ymax=Mmax,
aspect_ratio=1,title=titleeq,figsize=8,fontsize=20,svg=False)


#Deer and Moose Code 2

def Scale_vf(xy): #Defining the code
# a function that takes a vector input and returns a scaled
# version of the vector to keep arrows from getting too long
scale=sqrt(5+xy[0]^2+xy[1]^2)
return [xy[0]/scale,xy[1]/scale]

vs=var("D,M")  #creating the labels
t=srange(0,50,0.05) #setting the range
Dmax=4.0  #setting max for graph
Mmax=3.5  #Setting min for graph

@interact #calling the code
def Deer_Moose_norm(r_D=slider(0,5,0.5,default=3,label="$r_D$"),  #setting the sliders and their range with labels
r_M=slider(0,5,0.5,default=2,label="$r_M$"),
alpha=slider(0,2,0.5,default=0.5,label="$\\alpha$"),
D0=slider(0,Dmax,0.1,default=1,label="$D_0$"),
M0=slider(0,Mmax,0.1,default=1,label="$M_0$")):
titleeq=("$D'= " + '%2.1f'%(r_D) + " D - M D - D^2$\n " +  #setting the title with the equations
"$M'= " + '%2.1f'%(r_M) + " M - " + '%2.1f'%(alpha) + " M D - M^2$")
vector_field=[r_D*D-M*D-D^2,r_M*M-alpha*M*D-M^2] #setting the vector field
initial_value=[D0,M0] #initial values
pv=plot_vector_field(Scale_vf(vector_field),(D,0,Dmax),(M,0,Mmax), plot_points=11,axes_labels=["D","M"]) #setting the plots with labels and vector sizes and axes labels

# plot the scaled vector field
sol1=desolve_odeint(vector_field,times=t,ics=initial_value,dvars=[D,M])
ps1=list_plot(sol1,plotjoined=True,color='green',thickness=2)
d1=disk(initial_value,.035,(0,2*pi),color='green')
nullclines=(plot(r_D-D,(D,0,r_D),color="red",thickness=3)
+plot(r_M-alpha*D,(D,0,r_M/alpha),color="blue",thickness=3))
show(pv+ps1+d1+nullclines,xmin=0,xmax=Dmax,ymin=0,ymax=Mmax,  #what is being showed on the graph
aspect_ratio=1,title=titleeq,figsize=8,fontsize=20,svg=False)






#Shark Tuna Code

def Scale_vf(xy):  #Defining the code
# a function that takes a vector input and returns a scaled
# version of the vector to keep arrows from getting too long
#

scale=sqrt(5+xy[0]^2+xy[1]^2)
return [xy[0]/scale,xy[1]/scale]

vs=var('T,S')   #creating the labels
t=srange(0,150,0.05) #setting the range
Tmax=100  #setting max for graph
Smax=40 #Setting min for graph
@interact #calling the code
def Shark_Tuna_norm(r_T=slider(0,0.4,0.01,default=0.1,label="$r_T$"), #setting the sliders and their range with labels
r_S=slider(-0.5,0,0.1,default=-0.2,label="$r_S$"),
beta=slider(0,0.1,0.005,default=0.01,label="$\\beta$"),
m=slider(0,1,0.01,default=0.5,label="$m$"),
T0=slider(0,Tmax,0.1,default=20,label="$T_0$"),
S0=slider(0,Smax,0.1,default=10,label="$S_0$")):
titleeq=("$T'= "+'%4.3f'%(r_T)+" T - "'%4.3f'%(beta)+" S T$\n " #setting the title with the equations
+"$S' = "+'%4.3f'%(r_S)+" S + "+'%4.3f'%(beta*m)+" S T$")
vector_field=[r_T*T-beta*T*S-0.001*T^2,r_S*S+m*beta*T*S]  #setting the vector field
initial_value=[T0,S0] #initial values
pv=plot_vector_field(Scale_vf(vector_field),(T,0,Tmax), #setting the plots with labels and vector sizes
(S,0,Smax),plot_points=11,axes_labels=["T","S"])
sol1=desolve_odeint(vector_field,times=t,ics=initial_value,dvars=[T,S])
ps1=list_plot(sol1,plotjoined=True,color='green',thickness=2)
d1=disk(initial_value,1,(0,2*pi),color='green')
show(pv+ps1+d1,xmin=0,xmax=Tmax,ymin=0,ymax=Smax, #what is being showed on the graph
aspect_ratio=1,title=titleeq,figsize=8,fontsize=20,svg=False)