Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Project: LAB 4 GROUP
Views: 342
Image: ubuntu2004
Kernel: SageMath 9.7
#Capstone Week 9 Code
#Lokta-Volterra (Predator Prey Model)
#Defining the differential equations: #T' = bT - BST #S' = mBST - dS
#Defining the differential equations: *added time delay* #T' = b(T-2) - BS(T-2) #S' = mBS(T-2) - dS
#First we will be using set values for the parameters (B, S, m, b), and manipulating the equation to see the affects of time delay.
#To compare the effect of each change, we will be plotting the time series and trajectory for each set of ics
#setting b=0.6, m=0.6, S=0.02, B= 0.3 # Define the differential equation var('T','S') DEs = [0.6*T - 0.02*T*S, 0.6*0.02*T*S - .3*S] # initial conditions ic = [40, 80] # T=40, S=80 # setting the timespan t = srange(0,100,.1) sol = desolve_odeint(DEs, dvars=[T,S], ics=ic, times=t) # plotting trajectory for ics T=40, S=80 list_plot(sol, axes_labels=['N','P'])
Image in a Jupyter notebook
#setting b=5, m=0.6, S=0.02, B= 0.3 # Define the differential equation var('T','S') DEs = [5*(T-2) - 0.02*(T-2)*S, 0.6*0.02*(T-2)*S - .3*S] # setting the first pair of initial conditions ic = [40, 80] # T=40, S=80 # setting the timespan t = srange(0,100,.1) sol = desolve_odeint(DEs, dvars=[T,S], ics=ic, times=t) # plotting the time series for ics T=40, S=80 and change in beta=5 sol = desolve_odeint([5*(T-2) - 0.02*(T-2)*S, 0.6*0.02*(T-2)*S - .3*S] , dvars=[T,S], ics=ic, times=t)
# plotting trajectory for ics T=40, S=80, B= 5 list_plot(sol, axes_labels=['N','P'])
Image in a Jupyter notebook
#setting b=0.6, m=0.6, S=0.02, B= 0.3 # Define the differential equation var('T','S') DEs = [5*(T-2) - 0.02*(T-2)*S, 0.6*0.02*(T-2)*S - .3*S] # setting the second pair of initial conditions ic = [100, 200] # T=100, S=200 # setting the timespan t = srange(0,100,.1) sol = desolve_odeint(DEs, dvars=[T,S], ics=ic, times=t) # plotting the time series for ics T=100, S=200 sol = desolve_odeint([5*(T-2) - 0.02*(T-2)*S, 0.6*0.02*(T-2)*S - .3*S] , dvars=[T,S], ics=ic, times=t)
# plotting trajectory for ics T=100, S=200 list_plot(sol, axes_labels=['N','P'])
Image in a Jupyter notebook
#setting b=5, m=0.6, S=0.02, B= 0.3 # Define the differential equation var('T','S') DEs = [5*T - 0.02*T*S, 0.6*0.02*T*S - .3*S] # setting the second pair of initial conditions ic = [100, 200] # T=100, S=200 # setting the timespan t = srange(0,100,.1) sol = desolve_odeint(DEs, dvars=[T,S], ics=ic, times=t) # plotting the time series for ics T=100, S=200 sol = desolve_odeint([5*T - 0.02*T*S, 0.6*0.02*T*S - .3*S] , dvars=[T,S], ics=ic, times=t)
# plotting trajectory for ics T=100, S=200, b= 5 list_plot(sol, axes_labels=['N','P'])
Image in a Jupyter notebook
#setting b=0.6, m=0.6, S=0.02, B= 0.3 # Define the differential equation var('T','S') DEs = [5*(T-2) - 0.02*(T-2)*S, 0.6*0.02*(T-2)*S - .3*S] # setting the third pair of initial conditions ic = [5, 50] # T=5, S=50 # setting the timespan t = srange(0,100,.1) sol = desolve_odeint(DEs, dvars=[T,S], ics=ic, times=t) # plotting the time series for ics T=5, S=50 sol = desolve_odeint([5*(T-2) - 0.02*(T-2)*S, 0.6*0.02*(T-2)*S - .3*S] , dvars=[T,S], ics=ic, times=t)
# plotting trajectory for ics T=5, S=50 list_plot(sol, axes_labels=['N','P'])
Image in a Jupyter notebook
#setting b=5, m=0.6, S=0.02, B= 0.3 # Define the differential equation var('T','S') DEs = [5*T - 0.02*T*S, 0.6*0.02*T*S - .3*S] # setting the third pair of initial conditions ic = [5, 50] # T=5, S=50 # setting the timespan t = srange(0,100,.1) sol = desolve_odeint(DEs, dvars=[T,S], ics=ic, times=t) # plotting the time series for ics T=5, S=50 sol = desolve_odeint([5*T - 0.02*T*S, 0.6*0.02*T*S - .3*S] , dvars=[T,S], ics=ic, times=t)
# plotting trajectory for ics T=5, S=50, b=5 list_plot(sol, axes_labels=['N','P'])
Image in a Jupyter notebook
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)
File /ext/sage/9.7/src/sage/plot/plot.py:3088, in list_plot(data, plotjoined, **kwargs) 3087 from sage.rings.real_double import RDF -> 3088 RDF(data[0]) 3089 data = list(enumerate(data))
TypeError: 'zip' object is not subscriptable During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) Input In [2], in <cell line: 8>() 4 sol=desolve_odeint([birthrate*N - RealNumber('0.01')*N*P, RealNumber('0.5')*RealNumber('0.01')*N*P - RealNumber('0.2')*P], ics=[Integer(100),Integer(200)], 5 dvars=[N,P], times=t) 6 return list_plot(zip(t,sol[:,Integer(1)]), legend_label ="N") + list_plot(zip(t,sol[:,Integer(0)]), color = "red", legend_label = "P") ----> 8 laziness(RealNumber('0.7')) 9 laziness(RealNumber('0.9')) 10 laziness(RealNumber('1.1')) Input In [2], in laziness(birthrate) 3 t = srange(Integer(0),Integer(100),RealNumber('0.1')) 4 sol=desolve_odeint([birthrate*N - RealNumber('0.01')*N*P, RealNumber('0.5')*RealNumber('0.01')*N*P - RealNumber('0.2')*P], ics=[Integer(100),Integer(200)], 5 dvars=[N,P], times=t) ----> 6 return list_plot(zip(t,sol[:,Integer(1)]), legend_label ="N") + list_plot(zip(t,sol[:,Integer(0)]), color = "red", legend_label = "P")
File /ext/sage/9.7/src/sage/misc/decorators.py:491, in options.__call__.<locals>.wrapper(*args, **kwds) 489 options['__original_opts'] = kwds 490 options.update(kwds) --> 491 return func(*args, **options)
File /ext/sage/9.7/src/sage/plot/plot.py:3098, in list_plot(data, plotjoined, **kwargs) 3089 data = list(enumerate(data)) 3090 except TypeError: # we can get this TypeError if the element is a list 3091 # or tuple or numpy array, or an element of CC, CDF 3092 # We also want to avoid doing CC(data[0]) here since it will go (...) 3096 # So, the only other check we need to do is whether data[0] is an 3097 # element of the Symbolic Ring. -> 3098 if data[0] in sage.symbolic.ring.SR: 3099 data = list(enumerate(data)) 3101 try:
TypeError: 'zip' object is not subscriptable
#Bifurcation diagram/Parameter Scan for LV differential equations: #T' = b(T-2) - BS(T-2) #S' = mBS(T-2) - dS var('T','S') t = srange(0,100,.1) #timespan sol = desolve_odeint([5*T - 0.02*T*S, 0.6*0.02*T*S - .3*S] , dvars=[T,S], ics=ic, times=t) list_plot(sol, plotjoined=true, axes_labels=["Time", "Population Density"])
Image in a Jupyter notebook