1
1
# ## A Pluto.jl notebook ###
2
- # v0.19.14
2
+ # v0.19.25
3
3
4
4
# > [frontmatter]
5
5
# > chapter = 3
11
11
# > layout = "layout.jlhtml"
12
12
# > youtube_id = "S71YIZ8e7MQ"
13
13
# > description = ""
14
- # > tags = ["lecture", "module3"]
14
+ # > tags = ["lecture", "module3", "epidemiology", "plotting", "ODE", "differential equation", "track_julia", "track_math", "programming", "DifferentialEquations", "type", "structure", "track_climate" ]
15
15
16
16
using Markdown
17
17
using InteractiveUtils
@@ -132,9 +132,6 @@ md"""
132
132
To set up the **problem** instance we use a type `ODEProblem` that is defined in the `DifferentialEquations.jl` package, into which we pass all the information necessary to define the problem. The parameters must go in the following order:
133
133
"""
134
134
135
- # ╔═╡ 6e7c8e9e-bef0-4bf8-b1b1-d50c82aa203e
136
- problem = ODEProblem (f, u0, time_span, p)
137
-
138
135
# ╔═╡ b5379dc2-d97f-47ed-8737-35e3fe59285c
139
136
md """
140
137
(For more advanced use there are also some additional, optional, keyword arguments.)
@@ -146,9 +143,6 @@ md"""
146
143
To solve the ODE we call the `solve` function:
147
144
"""
148
145
149
- # ╔═╡ ff91515a-89a4-4423-a186-5572c712493d
150
- solution = solve (problem)
151
-
152
146
# ╔═╡ 64b26404-1ca0-4aa4-bea0-3c9075b08298
153
147
md """
154
148
What happened here? A suitable solver (i.e. an algorithm to calculate the solution) was chosen *automatically*, and it chose certain moments in time at which to output information about the (approximate, but very accurate) solution.
@@ -158,9 +152,6 @@ In this particular case it chose to output data at only eight points in time bet
158
152
Let's try to plot the `solution` object:
159
153
"""
160
154
161
- # ╔═╡ b07de2ce-640b-42d6-8b60-39fcbcd116e7
162
- plot (solution, size= (500 , 300 ), label= " solution" )
163
-
164
155
# ╔═╡ 0b1855de-a8c9-492e-a249-3238e41fe84c
165
156
md """
166
157
### Plot recipes
@@ -176,23 +167,11 @@ md"""
176
167
The second surprise is that the output looks like a smooth curve, rather than just 8 points. Let's see those points on top of the curve. We can extract the relevant data from the `solution` object:
177
168
"""
178
169
179
- # ╔═╡ d65372df-e6ca-4165-8d9b-b21cc5c9f796
180
- scatter! (solution. t, solution. u, label= " discrete output" )
181
-
182
170
# ╔═╡ b274c61b-4fab-4f4a-99df-5d39e0f56aa1
183
171
md """
184
172
We see that the package in fact gives not only the value at those points, but it is in fact also capable of calculating an (approximate) solution at *any* intermediate point, using **interpolation**. In fact, we can access this by treating `solution` as if it were a function:
185
173
"""
186
174
187
- # ╔═╡ e3d9e0be-6cf4-4ae1-8c7f-b444a153a9f5
188
- begin
189
- tt = 3.5
190
- solution (tt)
191
- end
192
-
193
- # ╔═╡ 180bcdc1-5f51-4b32-8b9c-5000605cdf32
194
- scatter! ([tt], [solution (tt)], label= " t = $(tt) " , ms= 5 , m= :square )
195
-
196
175
# ╔═╡ 13b38f88-2ead-46b3-bc96-eae2ea10204d
197
176
md """
198
177
For this particular ODE we know the analytical solution. Let's compare them as we vary the parameter $p$:
@@ -203,6 +182,27 @@ md"""
203
182
p = $(@bind p Slider(0.0:0.1:2.0, show_value=true))
204
183
"""
205
184
185
+ # ╔═╡ 6e7c8e9e-bef0-4bf8-b1b1-d50c82aa203e
186
+ problem = ODEProblem (f, u0, time_span, p)
187
+
188
+ # ╔═╡ ff91515a-89a4-4423-a186-5572c712493d
189
+ solution = solve (problem)
190
+
191
+ # ╔═╡ b07de2ce-640b-42d6-8b60-39fcbcd116e7
192
+ plot (solution, size= (500 , 300 ), label= " solution" )
193
+
194
+ # ╔═╡ d65372df-e6ca-4165-8d9b-b21cc5c9f796
195
+ scatter! (solution. t, solution. u, label= " discrete output" )
196
+
197
+ # ╔═╡ e3d9e0be-6cf4-4ae1-8c7f-b444a153a9f5
198
+ begin
199
+ tt = 3.5
200
+ solution (tt)
201
+ end
202
+
203
+ # ╔═╡ 180bcdc1-5f51-4b32-8b9c-5000605cdf32
204
+ scatter! ([tt], [solution (tt)], label= " t = $(tt) " , ms= 5 , m= :square )
205
+
206
206
# ╔═╡ 0ed6d65c-707d-4666-b203-2ad0ea822687
207
207
let
208
208
@@ -270,58 +270,54 @@ function SIR(x, p, t)
270
270
271
271
end
272
272
273
- # ╔═╡ d0f40681-73df-4cd3-bbd5-3edb8193153e
274
- params = [β, γ]
275
-
276
- # ╔═╡ fd122a25-5655-410b-aa7f-34936fc97b53
277
- SIR_problem = ODEProblem (SIR, x0, (0.0 , 50.0 ), params)
278
-
279
- # ╔═╡ 8ac7da6b-46e0-470b-91b3-1a61c226fa4a
280
- sol = solve (SIR_problem)
281
-
282
273
# ╔═╡ 7aa45efe-865d-4e0e-9c71-0c032c72c40d
283
274
md """
284
275
Now we see that the solverr has recognised that everything is a vector, and it returns a vector at each time stamp.
285
276
286
277
Again we can plot:
287
278
"""
288
279
289
- # ╔═╡ bca8112f-cb61-43dc-ae87-383915c8a89b
290
- gr ()
291
-
292
- # ╔═╡ a766a141-5d7b-499f-9e18-48bf926ee7ea
293
- plot (sol)
294
-
295
280
# ╔═╡ 76cbc37d-54c8-4626-8bfe-58b63a602c38
296
281
md """
297
282
β = $(@bind β Slider(-0.5:0.01:2.0, default=1.0, show_value=true))
298
283
299
284
γ = $(@bind γ Slider(-0.5:0.01:2.0, default=0.1, show_value=true))
300
285
"""
301
286
287
+ # ╔═╡ d0f40681-73df-4cd3-bbd5-3edb8193153e
288
+ params = [β, γ]
289
+
290
+ # ╔═╡ fd122a25-5655-410b-aa7f-34936fc97b53
291
+ SIR_problem = ODEProblem (SIR, x0, (0.0 , 50.0 ), params)
292
+
293
+ # ╔═╡ 8ac7da6b-46e0-470b-91b3-1a61c226fa4a
294
+ sol = solve (SIR_problem)
295
+
296
+ # ╔═╡ a766a141-5d7b-499f-9e18-48bf926ee7ea
297
+ plot (sol)
298
+
302
299
# ╔═╡ c2765282-bcc7-4110-9822-10557326461e
303
300
md """
304
301
It knows to plot each variable separately.
305
302
306
303
We can instead plot combinations of variables in *phase space* or *state space*:
307
304
"""
308
305
309
- # ╔═╡ 7671506d-d4b7-4792-9571-e003097235e1
310
- gr ()
311
-
312
306
# ╔═╡ 203e90b0-4d0c-4999-8513-d4eb43f53aac
313
- plot (sol, vars = (1 , 2 ), xlabel= " s" , ylabel= " i" , arrow= true , xlims= (- 0.1 , 1.0 ), size= (500 , 300 ))
307
+ plot (sol, idxs = (1 , 2 ), xlabel= " s" , ylabel= " i" , arrow= true , xlims= (- 0.1 , 1.0 ), size= (500 , 300 ))
314
308
315
309
# ╔═╡ 326890ae-0675-4779-b5e8-9b3e0412a52b
316
310
md """
317
311
And even in 3D:
318
312
"""
319
313
320
- # ╔═╡ d7025d1f-c3b8-461a-94dc-2414fbdfd373
321
- plotly ()
322
-
323
314
# ╔═╡ 7f5c41f9-96b9-40d6-87f8-3e81c372b48e
324
- plot (sol, vars= (1 , 2 , 3 ), xlabel= " s" , ylabel= " i" , zlabel= " r" )
315
+ let
316
+ plotly ()
317
+ p = plot (sol, vars= (1 , 2 , 3 ), xlabel= " s" , ylabel= " i" , zlabel= " r" )
318
+ gr ()
319
+ p
320
+ end
325
321
326
322
# ╔═╡ d8f15e9b-aef1-4795-8522-85ea3564e351
327
323
md """
@@ -446,11 +442,11 @@ problem.tspan
446
442
447
443
# ╔═╡ 9ef4aa78-2ecd-4d53-87ae-f0909bb46915
448
444
md """
449
- To see everything contained in the object, we can use `Dump` in Pluto, or ` dump` if we are not using Pluto :
445
+ To see everything contained in the object, we can use `dump`:
450
446
"""
451
447
452
448
# ╔═╡ 798bc482-7556-4904-8946-0d9898ce2d33
453
- Dump (problem)
449
+ dump (problem)
454
450
455
451
# ╔═╡ 32e9f6e5-8df6-4d40-8092-efaf2bce28d3
456
452
md """
@@ -466,7 +462,7 @@ Similarly we can look inside the solution object:
466
462
fieldnames (typeof (solution))
467
463
468
464
# ╔═╡ f4c3b174-57ac-461b-a816-869460d8896d
469
- Dump (solution)
465
+ dump (solution)
470
466
471
467
# ╔═╡ 500973f5-faa7-4a31-b812-d0b20dbb9d82
472
468
md """
@@ -615,9 +611,9 @@ PlutoUI = "~0.7.48"
615
611
PLUTO_MANIFEST_TOML_CONTENTS = """
616
612
# This file is machine-generated - editing it directly is not advised
617
613
618
- julia_version = "1.8.0 "
614
+ julia_version = "1.8.5 "
619
615
manifest_format = "2.0"
620
- project_hash = "b48a2ebd54d8ff6b6e42c00d3207d729135a6751 "
616
+ project_hash = "487bb43b85ea993e75ee5d27f6f9874e03e8f289 "
621
617
622
618
[[deps.AbstractPlutoDingetjes]]
623
619
deps = ["Pkg"]
@@ -730,7 +726,7 @@ uuid = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"
730
726
version = "0.1.27"
731
727
732
728
[[deps.Cairo_jll]]
733
- deps = ["Artifacts", "Bzip2_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
729
+ deps = ["Artifacts", "Bzip2_jll", "CompilerSupportLibraries_jll", " Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
734
730
git-tree-sha1 = "4b859a208b2397a7a623a03449e4636bdb17bcf2"
735
731
uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a"
736
732
version = "1.16.1+1"
@@ -809,7 +805,7 @@ version = "4.3.0"
809
805
[[deps.CompilerSupportLibraries_jll]]
810
806
deps = ["Artifacts", "Libdl"]
811
807
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
812
- version = "0.5.2 +0"
808
+ version = "1.0.1 +0"
813
809
814
810
[[deps.ConstructionBase]]
815
811
deps = ["LinearAlgebra"]
@@ -1088,9 +1084,9 @@ version = "0.21.0+0"
1088
1084
1089
1085
[[deps.Glib_jll]]
1090
1086
deps = ["Artifacts", "Gettext_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE2_jll", "Pkg", "Zlib_jll"]
1091
- git-tree-sha1 = "fb83fbe02fe57f2c068013aa94bcdf6760d3a7a7 "
1087
+ git-tree-sha1 = "d3b3624125c1474292d0d8ed0f65554ac37ddb23 "
1092
1088
uuid = "7746bdde-850d-59dc-9ae8-88ece973131d"
1093
- version = "2.74.0+1 "
1089
+ version = "2.74.0+2 "
1094
1090
1095
1091
[[deps.Graphite2_jll]]
1096
1092
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@@ -1323,9 +1319,9 @@ version = "1.42.0+0"
1323
1319
1324
1320
[[deps.Libiconv_jll]]
1325
1321
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
1326
- git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778 "
1322
+ git-tree-sha1 = "c7cb1f5d892775ba13767a87c7ada0b980ea0a71 "
1327
1323
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
1328
- version = "1.16.1+1 "
1324
+ version = "1.16.1+2 "
1329
1325
1330
1326
[[deps.Libmount_jll]]
1331
1327
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@@ -1632,9 +1628,9 @@ uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1632
1628
1633
1629
[[deps.Qt5Base_jll]]
1634
1630
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Fontconfig_jll", "Glib_jll", "JLLWrappers", "Libdl", "Libglvnd_jll", "OpenSSL_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libxcb_jll", "Xorg_xcb_util_image_jll", "Xorg_xcb_util_keysyms_jll", "Xorg_xcb_util_renderutil_jll", "Xorg_xcb_util_wm_jll", "Zlib_jll", "xkbcommon_jll"]
1635
- git-tree-sha1 = "c6c0f690d0cc7caddb74cef7aa847b824a16b256 "
1631
+ git-tree-sha1 = "0c03844e2231e12fda4d0086fd7cbe4098ee8dc5 "
1636
1632
uuid = "ea2cea3b-5b76-57ae-a6ef-0a8af62496e1"
1637
- version = "5.15.3+1 "
1633
+ version = "5.15.3+2 "
1638
1634
1639
1635
[[deps.QuadGK]]
1640
1636
deps = ["DataStructures", "LinearAlgebra"]
@@ -1918,7 +1914,7 @@ version = "1.10.0"
1918
1914
[[deps.Tar]]
1919
1915
deps = ["ArgTools", "SHA"]
1920
1916
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
1921
- version = "1.10.0 "
1917
+ version = "1.10.1 "
1922
1918
1923
1919
[[deps.TensorCore]]
1924
1920
deps = ["LinearAlgebra"]
@@ -2271,14 +2267,11 @@ version = "1.4.1+0"
2271
2267
# ╠═fd122a25-5655-410b-aa7f-34936fc97b53
2272
2268
# ╠═8ac7da6b-46e0-470b-91b3-1a61c226fa4a
2273
2269
# ╟─7aa45efe-865d-4e0e-9c71-0c032c72c40d
2274
- # ╠═bca8112f-cb61-43dc-ae87-383915c8a89b
2275
2270
# ╠═a766a141-5d7b-499f-9e18-48bf926ee7ea
2276
2271
# ╟─76cbc37d-54c8-4626-8bfe-58b63a602c38
2277
2272
# ╟─c2765282-bcc7-4110-9822-10557326461e
2278
- # ╠═7671506d-d4b7-4792-9571-e003097235e1
2279
2273
# ╠═203e90b0-4d0c-4999-8513-d4eb43f53aac
2280
2274
# ╟─326890ae-0675-4779-b5e8-9b3e0412a52b
2281
- # ╠═d7025d1f-c3b8-461a-94dc-2414fbdfd373
2282
2275
# ╠═7f5c41f9-96b9-40d6-87f8-3e81c372b48e
2283
2276
# ╟─d8f15e9b-aef1-4795-8522-85ea3564e351
2284
2277
# ╟─e723400e-3f8f-47d6-8944-28355a54c698
0 commit comments