forked from mdoellma/Intro_Biocomp_ND_318_Tutorial10
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjanice_tutorial10Notes.rtf
More file actions
70 lines (69 loc) · 2.86 KB
/
Copy pathjanice_tutorial10Notes.rtf
File metadata and controls
70 lines (69 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;\red64\green128\blue128;\red0\green128\blue0;\red0\green0\blue255;
\red102\green102\blue102;\red187\green33\blue33;}
\margl1440\margr1440\vieww18780\viewh13680\viewkind0
\deftab720
\pard\pardeftab720
\f0\fs22 \cf2 #Tutorial 10 notes \
\
### Load the necessary packages\
\pard\pardeftab720
\cf3 import \cf4 pandas\
\cf3 import \cf4 scipy\
\cf3 import \cf4 scipy.integrate \cf3 as \cf4 spint\
\cf3 from \cf4 plotnine \cf3 import \cf5 *\
\
\pard\pardeftab720
\cf2 ### Custom function that defines the model differential equations\
# the odeint function we will use for simulating the model requires the function be\
# defined with the arguments for state variable list, time, and parameters be provided\
# in that order. Parameters must be defined individually because they are passed as\
# a Python tuple when calling odeint\
# odeint also requires that the dy/dt
\b \cf2 '
\b0 \cf2 s be returned as a list\
\pard\pardeftab720
\cf3 def \cf4 ddSim\cf0 (y,t0,r,K):\
\pard\pardeftab720
\cf2 # "unpack" lists containing state variables (y)\
\pard\pardeftab720
\cf0 N\cf5 =\cf0 y[\cf5 0\cf0 ]\
\pard\pardeftab720
\cf2 # calculate change in state variables with time, give parameter values\
# and current value of state variables\
\pard\pardeftab720
\cf0 dNdt\cf5 =\cf0 r\cf5 *\cf0 (\cf5 1-\cf0 N\cf5 /\cf0 K)\cf5 *\cf0 N\
\pard\pardeftab720
\cf2 # return list containing change in state variables with time\
\pard\pardeftab720
\cf3 return \cf0 [dNdt]\
\pard\pardeftab720
\cf2 ### Define parameters, initial values for state variables, and time steps\
\pard\pardeftab720
\cf0 params\cf5 =\cf0 (\cf5 0.3\cf0 ,\cf5 10\cf0 )\
N0\cf5 =\cf0 [\cf5 0.01\cf0 ]\
times\cf5 =\cf3 range\cf0 (\cf5 0\cf0 ,\cf5 600\cf0 )\
\pard\pardeftab720
\cf2 ### Simulate the model using odeint\
\pard\pardeftab720
\cf0 modelSim\cf5 =\cf0 spint\cf5 .\cf0 odeint(func\cf5 =\cf0 ddSim,y0\cf5 =\cf0 N0,t\cf5 =\cf0 times,args\cf5 =\cf0 params)\
\
\pard\pardeftab720
\cf2 ### put model output in a dataframe for plotting purposes\
\pard\pardeftab720
\cf0 modelOutput\cf5 =\cf0 pandas\cf5 .\cf0 DataFrame(\{\cf6 "t"\cf0 :times,\cf6 "N"\cf0 :modelSim[:,\cf5 0\cf0 ]\})\
\pard\pardeftab720
\cf2 ### plot simulation output\
\pard\pardeftab720
\cf0 ggplot(modelOutput,aes(x\cf5 =\cf6 "t"\cf0 ,y\cf5 =\cf6 "N"\cf0 ))\cf5 +\cf0 geom_line()\cf5 +\cf0 theme_classic()\
\
\pard\pardeftab720
\cf2 # function for simulating predator-prey dynamics\
\pard\pardeftab720
\cf3 def \cf4 predPreySim\cf0 (y,t0,r,K,consume,predDeath):\
prey\cf5 =\cf0 y[\cf5 0\cf0 ]\
pred\cf5 =\cf0 y[\cf5 1\cf0 ]\
dPrey_dt\cf5 =\cf0 r\cf5 *\cf0 (\cf5 1-\cf0 prey\cf5 /\cf0 K)\cf5 *\cf0 prey\cf5 -\cf0 consume\cf5 *\cf0 prey\cf5 *\cf0 pred\
dPred_dt\cf5 =\cf0 consume\cf5 *\cf0 prey\cf5 *\cf0 pred\cf5 -\cf0 predDeath\cf5 *\cf0 pred\
\cf3 return \cf0 [dPrey_dt,dPred_dt]}