11# ## A Pluto.jl notebook ###
2- # v0.19.14
2+ # v0.19.25
33
44# > [frontmatter]
55# > chapter = 3
1111# > layout = "layout.jlhtml"
1212# > youtube_id = "S71YIZ8e7MQ"
1313# > description = ""
14- # > tags = ["lecture", "module3"]
14+ # > tags = ["lecture", "module3", "epidemiology", "plotting", "ODE", "differential equation", "track_julia", "track_math", "programming", "DifferentialEquations", "type", "structure", "track_climate" ]
1515
1616using Markdown
1717using InteractiveUtils
@@ -132,9 +132,6 @@ md"""
132132To 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:
133133"""
134134
135- # ╔═╡ 6e7c8e9e-bef0-4bf8-b1b1-d50c82aa203e
136- problem = ODEProblem (f, u0, time_span, p)
137-
138135# ╔═╡ b5379dc2-d97f-47ed-8737-35e3fe59285c
139136md """
140137(For more advanced use there are also some additional, optional, keyword arguments.)
@@ -146,9 +143,6 @@ md"""
146143To solve the ODE we call the `solve` function:
147144"""
148145
149- # ╔═╡ ff91515a-89a4-4423-a186-5572c712493d
150- solution = solve (problem)
151-
152146# ╔═╡ 64b26404-1ca0-4aa4-bea0-3c9075b08298
153147md """
154148What 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
158152Let's try to plot the `solution` object:
159153"""
160154
161- # ╔═╡ b07de2ce-640b-42d6-8b60-39fcbcd116e7
162- plot (solution, size= (500 , 300 ), label= " solution" )
163-
164155# ╔═╡ 0b1855de-a8c9-492e-a249-3238e41fe84c
165156md """
166157### Plot recipes
@@ -176,23 +167,11 @@ md"""
176167The 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:
177168"""
178169
179- # ╔═╡ d65372df-e6ca-4165-8d9b-b21cc5c9f796
180- scatter! (solution. t, solution. u, label= " discrete output" )
181-
182170# ╔═╡ b274c61b-4fab-4f4a-99df-5d39e0f56aa1
183171md """
184172We 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:
185173"""
186174
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-
196175# ╔═╡ 13b38f88-2ead-46b3-bc96-eae2ea10204d
197176md """
198177For this particular ODE we know the analytical solution. Let's compare them as we vary the parameter $p$:
@@ -203,6 +182,27 @@ md"""
203182p = $(@bind p Slider(0.0:0.1:2.0, show_value=true))
204183"""
205184
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+
206206# ╔═╡ 0ed6d65c-707d-4666-b203-2ad0ea822687
207207let
208208
@@ -270,58 +270,54 @@ function SIR(x, p, t)
270270
271271end
272272
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-
282273# ╔═╡ 7aa45efe-865d-4e0e-9c71-0c032c72c40d
283274md """
284275Now we see that the solverr has recognised that everything is a vector, and it returns a vector at each time stamp.
285276
286277Again we can plot:
287278"""
288279
289- # ╔═╡ bca8112f-cb61-43dc-ae87-383915c8a89b
290- gr ()
291-
292- # ╔═╡ a766a141-5d7b-499f-9e18-48bf926ee7ea
293- plot (sol)
294-
295280# ╔═╡ 76cbc37d-54c8-4626-8bfe-58b63a602c38
296281md """
297282β = $(@bind β Slider(-0.5:0.01:2.0, default=1.0, show_value=true))
298283
299284γ = $(@bind γ Slider(-0.5:0.01:2.0, default=0.1, show_value=true))
300285"""
301286
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+
302299# ╔═╡ c2765282-bcc7-4110-9822-10557326461e
303300md """
304301It knows to plot each variable separately.
305302
306303We can instead plot combinations of variables in *phase space* or *state space*:
307304"""
308305
309- # ╔═╡ 7671506d-d4b7-4792-9571-e003097235e1
310- gr ()
311-
312306# ╔═╡ 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 ))
314308
315309# ╔═╡ 326890ae-0675-4779-b5e8-9b3e0412a52b
316310md """
317311And even in 3D:
318312"""
319313
320- # ╔═╡ d7025d1f-c3b8-461a-94dc-2414fbdfd373
321- plotly ()
322-
323314# ╔═╡ 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
325321
326322# ╔═╡ d8f15e9b-aef1-4795-8522-85ea3564e351
327323md """
@@ -446,11 +442,11 @@ problem.tspan
446442
447443# ╔═╡ 9ef4aa78-2ecd-4d53-87ae-f0909bb46915
448444md """
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`:
450446"""
451447
452448# ╔═╡ 798bc482-7556-4904-8946-0d9898ce2d33
453- Dump (problem)
449+ dump (problem)
454450
455451# ╔═╡ 32e9f6e5-8df6-4d40-8092-efaf2bce28d3
456452md """
@@ -466,7 +462,7 @@ Similarly we can look inside the solution object:
466462fieldnames (typeof (solution))
467463
468464# ╔═╡ f4c3b174-57ac-461b-a816-869460d8896d
469- Dump (solution)
465+ dump (solution)
470466
471467# ╔═╡ 500973f5-faa7-4a31-b812-d0b20dbb9d82
472468md """
@@ -615,9 +611,9 @@ PlutoUI = "~0.7.48"
615611PLUTO_MANIFEST_TOML_CONTENTS = """
616612# This file is machine-generated - editing it directly is not advised
617613
618- julia_version = "1.8.0 "
614+ julia_version = "1.8.5 "
619615manifest_format = "2.0"
620- project_hash = "b48a2ebd54d8ff6b6e42c00d3207d729135a6751 "
616+ project_hash = "487bb43b85ea993e75ee5d27f6f9874e03e8f289 "
621617
622618[[deps.AbstractPlutoDingetjes]]
623619deps = ["Pkg"]
@@ -730,7 +726,7 @@ uuid = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"
730726version = "0.1.27"
731727
732728[[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"]
734730git-tree-sha1 = "4b859a208b2397a7a623a03449e4636bdb17bcf2"
735731uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a"
736732version = "1.16.1+1"
@@ -809,7 +805,7 @@ version = "4.3.0"
809805[[deps.CompilerSupportLibraries_jll]]
810806deps = ["Artifacts", "Libdl"]
811807uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
812- version = "0.5.2 +0"
808+ version = "1.0.1 +0"
813809
814810[[deps.ConstructionBase]]
815811deps = ["LinearAlgebra"]
@@ -1088,9 +1084,9 @@ version = "0.21.0+0"
10881084
10891085[[deps.Glib_jll]]
10901086deps = ["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 "
10921088uuid = "7746bdde-850d-59dc-9ae8-88ece973131d"
1093- version = "2.74.0+1 "
1089+ version = "2.74.0+2 "
10941090
10951091[[deps.Graphite2_jll]]
10961092deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@@ -1323,9 +1319,9 @@ version = "1.42.0+0"
13231319
13241320[[deps.Libiconv_jll]]
13251321deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
1326- git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778 "
1322+ git-tree-sha1 = "c7cb1f5d892775ba13767a87c7ada0b980ea0a71 "
13271323uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
1328- version = "1.16.1+1 "
1324+ version = "1.16.1+2 "
13291325
13301326[[deps.Libmount_jll]]
13311327deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@@ -1632,9 +1628,9 @@ uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
16321628
16331629[[deps.Qt5Base_jll]]
16341630deps = ["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 "
16361632uuid = "ea2cea3b-5b76-57ae-a6ef-0a8af62496e1"
1637- version = "5.15.3+1 "
1633+ version = "5.15.3+2 "
16381634
16391635[[deps.QuadGK]]
16401636deps = ["DataStructures", "LinearAlgebra"]
@@ -1918,7 +1914,7 @@ version = "1.10.0"
19181914[[deps.Tar]]
19191915deps = ["ArgTools", "SHA"]
19201916uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
1921- version = "1.10.0 "
1917+ version = "1.10.1 "
19221918
19231919[[deps.TensorCore]]
19241920deps = ["LinearAlgebra"]
@@ -2271,14 +2267,11 @@ version = "1.4.1+0"
22712267# ╠═fd122a25-5655-410b-aa7f-34936fc97b53
22722268# ╠═8ac7da6b-46e0-470b-91b3-1a61c226fa4a
22732269# ╟─7aa45efe-865d-4e0e-9c71-0c032c72c40d
2274- # ╠═bca8112f-cb61-43dc-ae87-383915c8a89b
22752270# ╠═a766a141-5d7b-499f-9e18-48bf926ee7ea
22762271# ╟─76cbc37d-54c8-4626-8bfe-58b63a602c38
22772272# ╟─c2765282-bcc7-4110-9822-10557326461e
2278- # ╠═7671506d-d4b7-4792-9571-e003097235e1
22792273# ╠═203e90b0-4d0c-4999-8513-d4eb43f53aac
22802274# ╟─326890ae-0675-4779-b5e8-9b3e0412a52b
2281- # ╠═d7025d1f-c3b8-461a-94dc-2414fbdfd373
22822275# ╠═7f5c41f9-96b9-40d6-87f8-3e81c372b48e
22832276# ╟─d8f15e9b-aef1-4795-8522-85ea3564e351
22842277# ╟─e723400e-3f8f-47d6-8944-28355a54c698
0 commit comments