Skip to content
Snippets Groups Projects
Commit b04b1023 authored by Alexander Fleming's avatar Alexander Fleming
Browse files

second set of exercises

parent b548121a
No related branches found
No related tags found
No related merge requests found
# I keep the slides/books here on my PC
course_notes
\ No newline at end of file
### A Pluto.jl notebook ###
# v0.19.27
#> [frontmatter]
#> Author = "Alexander Fleming"
#> title = "Particle Systems and Ensembles"
#> date = "2023-07-31"
using Markdown
using InteractiveUtils
......@@ -18,38 +23,102 @@ md"""
"""
# ╔═╡ 97fc9341-c7b8-4ab7-b5f3-c1253b9f1c0a
@doc """
Ensemble{T, D}
"""
mutable struct Ensemble{T, D}
begin
mutable struct PSystem{T, D}
n::Int
m_i::Vector{T}
m::T
x_i::Array{T, 2}
p_i::Array{T, 2}
q_i::Vector{T}
end
function PSystem(ndims, n::Int, m_i::Vector{T}, x_i::Matrix{T}, p_i::Matrix{T}) where {T}
mass = sum(m_i)
return PSystem{T, ndims}(n, m_i, mass, x_i, p_i)
end
@doc """
PSystem{T, D}
Represents a collection of particles in ``D`` space dimensions represented by numerical type `T`.
Type Parameters
---
- `T`: Numerical type of the underlying representation.
- `D`: Number of space dimensions.
Fields
---
- `n::Int` Number of particles in the system.
- `m_i::Vector{T}` Mass of each particle.
- `m_t::T` Total mass of the system.
- `p_i::Matrix{T}` Momentum of each particle.
- `
Methods
---
""" PSystem
end
# ╔═╡ 03c1f366-320f-4715-bf7b-22661c2914db
begin
mass(e::Ensemble) = sum(e.m_i)
center_of_mass(e::Ensemble) = (e.x_i * e.m_i)/mass(e)
mass(e::PSystem) = e.m
center_of_mass(e::PSystem) = (e.x_i * e.m_i)/mass(e)
end
# ╔═╡ dfbe8230-7c71-4b35-86ae-543ec4fa4d2b
begin
momentum(e::PSystem) = reduce(+, e.p_i, dims=2)[:,1]
end
# ╔═╡ e90ac67c-e4c8-4937-9507-8b9372b4e371
begin
r_ab(e::Ensemble, a::Int, b::Int) = e.x_i[:, b] - e.x_i[:, a]
r_ab(e::PSystem, a::Int, b::Int) = e.x_i[:, b] - e.x_i[:, a]
function dist_ab(e::Ensemble, a::Int, b::Int)
function dist_ab(e::PSystem, a::Int, b::Int)
r = r_ab(e, a, b)
return sqrt(r⋅r)
end
end
# ╔═╡ 5ce46eeb-95e2-4963-9dda-ede887e74847
# ╔═╡ 3cb9c3a7-a354-427e-aff5-dafdbeadd47a
system = Ensemble{Float64, 2}(4, [2.0, 2.0, 1,1], randn((2,4)), zeros(2,4), [1, 2, 2, 4])
system = PSystem(2, 4, [2.0, 2.0, 1,1], randn((2,4)), zeros(2,4))
# ╔═╡ cf042dda-92d0-4998-9f8b-ab3593943085
md"""
# Exercise: Stirling's Approximation
```math
\begin{align}
n!&=\sqrt{2\pi n}\left(\frac{n}{\mathrm e}\right)^n\left(1+\mathrm O\left(\frac{1}{n}\right)\right)\\
\ln n! &= \frac{1}{2}\ln 2\pi n + n\ln\frac{n}{\mathrm e} + \ln \left(1+\mathrm O\left(\frac{1}{n}\right)\right)\\
\ln n! &\approx \frac{1}{2}\ln 2\pi n + n\ln n - n\ln\mathrm e\\
\ln n! &\approx \frac{1}{2}\ln 2\pi n + n\ln n - n
\end{align}
```
"""
# ╔═╡ c1716a64-e971-48a9-a85e-a8ebfc5603a9
begin
# ln n! = sum 1 to n ln n
logn!(n) = sum(log(n) for n=1:n)
logn!_appx(n) = 0.5*log(2*π*n)+n*log(n)-n
rel_err(n) = abs(logn!_appx(n)-logn!(n))/logn!(n)
end;
# ╔═╡ 1a06e6ff-bb13-4fbd-9ae1-6cbf9b5bbfbe
center_of_mass(system)
# ╔═╡ 535bb6a0-4ad5-4ce2-a792-7f432a5032ac
plot(1:1000, rel_err, yscale=:log10, ylabel="Relative Error", xlabel=L"n", label=L"\ln n! - \mathrm{Stirling}(n!)", dpi=600)
# ╔═╡ 51eac4fc-6e19-43c8-a755-165f8c38ca19
md"""
# Exercise: Ideal Gas
"""
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
......@@ -1093,10 +1162,15 @@ version = "1.4.1+0"
# ╔═╡ Cell order:
# ╟─d0ee70ff-df81-4eb1-b0d1-e2c68a3e2c37
# ╠═e10dfe0c-2ba4-11ee-09c1-43f00e299944
# ╠═97fc9341-c7b8-4ab7-b5f3-c1253b9f1c0a
# ╟─97fc9341-c7b8-4ab7-b5f3-c1253b9f1c0a
# ╠═03c1f366-320f-4715-bf7b-22661c2914db
# ╠═dfbe8230-7c71-4b35-86ae-543ec4fa4d2b
# ╠═e90ac67c-e4c8-4937-9507-8b9372b4e371
# ╠═5ce46eeb-95e2-4963-9dda-ede887e74847
# ╠═3cb9c3a7-a354-427e-aff5-dafdbeadd47a
# ╠═1a06e6ff-bb13-4fbd-9ae1-6cbf9b5bbfbe
# ╟─cf042dda-92d0-4998-9f8b-ab3593943085
# ╠═c1716a64-e971-48a9-a85e-a8ebfc5603a9
# ╟─535bb6a0-4ad5-4ce2-a792-7f432a5032ac
# ╠═51eac4fc-6e19-43c8-a755-165f8c38ca19
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment