-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples of ODEs -20-05-2022.jl
122 lines (88 loc) · 1.93 KB
/
Examples of ODEs -20-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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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)
### Example 1
function LinODE(dx,x,p,t)
dx[1] = -2.5 * x[1] + x[2]
dx[2] = -0.5 * x[2]
end
tspan = (0.,20.)
x0 = [5., 53]
p = [ ]
ode = ODEProblem(LinODE,x0,tspan,p)
sol = solve(ode)
plot(sol)
###############
function ex2(dx, x, p, t)
dx[1] = 3 * x[1] + x[2] - x[3]
dx[2] = - x[2] + 3*x[3]
dx[3] = -3 * x[3]
dx
end
tspan = (0., 5.)
p = []
x0 = [5., 3., -5.]
prob = ODEProblem(ex2, x0, tspan, p)
sol = solve(prob)
plot(sol,ylims=[-10,10])
function ex3(dx, x, p, t)
dx[1] = 1.5 * x[1] + 2*x[2]
dx[2] = -1.3 * x[1] - x[2] + x[3]
dx[3] = -x[3]
dx
end
tspan = (0., 100.)
p = []
x0 = [1., 3., -1.]
prob = ODEProblem(ex3, x0, tspan, p)
sol = solve(prob)
plot(sol)
function ex4(dx, x, p, t)
dx[1] = -1.5 * x[1] + 2* x[2]
dx[2] = -1.3 * x[1] - x[2] + x[3]
dx[3] = -x[3]
dx
end
tspan = (0., 10.)
p = []
x0 = [1., 3., -1.]
prob = ODEProblem(ex4, x0, tspan, p)
sol = solve(prob)
plot(sol)
###############
### Example 2
function LinODE2(dx,x,p,t)
dx[1] = 3 * x[1] + x[2] - x[3]
dx[2] = - x[2] + 3 * x[3]
dx[3] = -3 * x[3]
end
tspan = (0.,20.)
x0 = [5., 3, -5]
p = [ ]
ode2 = ODEProblem(LinODE2,x0,tspan,p)
sol2 = solve(ode2)
plot(sol2)
### Example 3
function LinODE3(du,u,p,t)
du[1] = 1.5 * u[1] + 2 * u[2]
du[2] = - 1.3* u[1] - u[2] + u[3]
du[3] = - u[3]
end
tspan = (0.,20.)
u0 = [1., 3, -1]
p = [ ]
ode3 = ODEProblem(LinODE3,u0,tspan,p)
sol3 = solve(ode3)
plot(sol3)
### Example 4
function LinODE4(du,u,p,t)
du[1] = -1.5 * u[1] + 2 * u[2]
du[2] = - 1.3* u[1] - u[2] + u[3]
du[3] = - u[3]
end
tspan = (0.,20.)
u0 = [1., 3, -1]
p = [ ]
ode4 = ODEProblem(LinODE4,u0,tspan,p)
sol4 = solve(ode4)
plot(sol4)