-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples of ODEs -03-06-2022.jl
87 lines (56 loc) · 1.33 KB
/
Examples of ODEs -03-06-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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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)
function Ex1(dx, x, p, t)
dx[1] = 3*x[2]
dx[2] = 3*x[1]
dx
end
tspan = (0., 20.)
x0 = [3., 3.]
p = [ ]
ode = ODEProblem(Ex1, x0, tspan, p)
sol = solve(ode)
plot(sol)
function Ex2(dx, x, p, t)
dx[1] = -3*x[2]
dx[2] = 3*x[1]
dx
end
tspan = (0., 20.)
x0 = [3., 3.]
p = [ ]
ode1 = ODEProblem(Ex2, x0, tspan, p)
sol = solve(ode1)
plot(sol)
## diseases model
function Ex3(dx, x, p, t)
dx[1] = -p[1]*x[1]*x[2] /p[3] + p[2]*x[2]
dx[2] = p[1]*x[1]*x[2]/p[3] - p[2]*x[2]
dx
end
beta = 0.5
gamma=1/7
N=10000
p = [beta gamma N]
tspan = (0., 20.)
x0 = [N-100, 100.]
ode1 = ODEProblem(Ex3, x0, tspan, p)
sol = solve(ode1)
plot(sol)
## predator prey model
alpha = 4 # growth rate of prey
beta = 0.1 # rate at which pedators feed on prey
delta= 0.1 # predation rate
gamma = 2 # death rate of predators
function PP(dx, x, p, t)
dx[1] = p[1]*x[1] - p[2]*x[1]*x[2]
dx[2] = p[4]*x[1]*x[2] -p[3]*x[2]
dx
end
p = [alpha, beta, gamma, delta]
tspan = (0., 100.)
x0 = [1000, 10.]
ode1 = ODEProblem(PP, x0, tspan, p)
sol = solve(ode1)
plot(sol)