-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples of ODEs -13-05-2022.jl
67 lines (42 loc) · 1.53 KB
/
Examples of ODEs -13-05-2022.jl
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
using Plots # load package plots (Documentation: https://docs.juliaplots.org/stable/)
using DifferentialEquations # (Documentation: https://diffeq.sciml.ai/stable/)
# (Further: https://juliapackages.com/p/differentialequations)
## exact solution
function LogGrowth(t,K,u0,gamma)
U0 = u0/(u0-K)
U0 .*K .* exp.(gamma .*t) ./(U0 .* exp.(gamma .*t) .-1)
end
## Assume time in years
# birth rate = 2 individuals/ year /individual
# death rate = 1 individual /year /individual
# u0 ... initial pop size = 100 individuals
## number of individuals after 10 years almost 100 000
LogGrowth(10, 100000,100,1)
## number of individuals after 20 years = 100 000
LogGrowth(20, 100000,100,1)
plot((1:2000) ./100,LogGrowth((1:2000) ./100, 100000,100,1))
#### optimize the code - we want to programm efficiently, so save on multplications
## exact solution
function LogGrowth1(t,K,u0,gamma)
#U0 .*K .* exp.(gamma .*t) ./(U0 .* exp.(gamma .*t) .-1)
K ./(1 .- exp.( - gamma .*t) .* (u0-K) ./u0 )
end
tt =(1:2000) ./100
plot(tt,LogGrowth1(tt, 100000,100,1))
### What happens if the pop exceeds carrying capacity K initially?
plot(tt,[LogGrowth1(tt, 100000,200000,1),LogGrowth1(tt, 100000,200,1)])
### Solving the ODE numericallly
### make sure to deine the operations element wise! Use . Operator!
function LGODE(du,u,p,t)
K, gamma = p
du .= gamma .* u .* (1 .- u/K)
du
end
tspan = (0.,20.)
u0 = [200.]
gamma = 1.
K = 100000.
p = [K, gamma]
ode = ODEProblem(LGODE,u0,tspan,p)
sol = solve(ode)
plot(sol)