Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Blaec (CSO) Tasks and Strategy Outline.
Download
54 views
ubuntu2204
Kernel: Python 3 (system-wide)
import matplotlib.pyplot as plt import numpy as np from scipy.integrate import odeint # Lotka-Volterra (predator-prey) model def model(z, t): x = z[0] # Prey population y = z[1] # Predator population dxdt = alpha*x - beta*x*y # change in prey population dydt = delta*x*y - gamma*y # change in predator population dzdt = [dxdt,dydt] return dzdt # Initial conditions z0 = [600, 400] # Parameters alpha = 1.1 # Prey birth rate beta = 0.4 # Predator's efficiency in converting consumed prey into predator increments gamma = 0.4 # Predator death rate delta = 0.1 # Predator's ability to find and consume prey # Time vector t = np.linspace(0, 50, 100) # Solve ODE z = odeint(model, z0, t) plt.figure(figsize=[8,5]) plt.plot(t, z[:, 0], 'b-', label='Prey population') plt.plot(t, z[:, 1], 'r--', label='Predator population') plt.xlabel('Time') plt.ylabel('Population') plt.tile('Lotka-Volterra Predator-Prey Model') plt.grid() plt.legend() plt.show()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /tmp/ipykernel_48970/3244130731.py in <cell line: 34>() 32 plt.xlabel('Time') 33 plt.ylabel('Population') ---> 34 plt.tile('Lotka-Volterra Predator-Prey Model') 35 plt.grid() 36 plt.legend() AttributeError: module 'matplotlib.pyplot' has no attribute 'tile'
Image in a Jupyter notebook
from sympy import * from sympy.abc import a, b, s, t, u, v, w, x, y, z k, m, n = symbols("k, m, n", integer=True) f, g, h = symbols("f, g, h", cls=Function)
dx0, dx1 = symbols("dx0, dx1") Derivative.as_finite_difference(f(x).diff(x), [x-dx0, x, x+dx1])

dx0f(dx1+x)dx1(dx0+dx1)+(−1+dx1dx0)f(x)dx1−dx1f(−dx0+x)dx0(dx0+dx1)\displaystyle \frac{dx_{0} f{\left(dx_{1} + x \right)}}{dx_{1} \left(dx_{0} + dx_{1}\right)} + \frac{\left(-1 + \frac{dx_{1}}{dx_{0}}\right) f{\left(x \right)}}{dx_{1}} - \frac{dx_{1} f{\left(- dx_{0} + x \right)}}{dx_{0} \left(dx_{0} + dx_{1}\right)}

A=2
B=3
A+B
import matplotlib.pyplot as plt import numpy as np from scipy.integrate import odeint # Lotka-Volterra (predator-prey) model def model(z, t): x = z[0] # Prey population y = z[1] # Predator population dxdt = alpha*x - beta*x*y # change in prey population dydt = delta*x*y - gamma*y # change in predator population dzdt = [dxdt,dydt] return dzdt # Initial conditions z0 = [600, 400] # Parameters alpha = 1.1 # Prey birth rate beta = 0.4 # Predator's efficiency in converting consumed prey into predator increments gamma = 0.4 # Predator death rate delta = 0.1 # Predator's ability to find and consume prey # Time vector t = np.linspace(0, 50, 100) # Solve ODE z = odeint(model, z0, t) plt.figure(figsize=[8,5]) plt.plot(t, z[:, 0], 'b-', label='Prey population') plt.plot(t, z[:, 1], 'r--', label='Predator population') plt.xlabel('Time') plt.ylabel('Population') plt.title('Lotka-Volterra Predator-Prey Model') # Fixed typo here plt.grid() plt.legend() plt.show()