SIR II (Epidemic modeling)
在这个模型中,考虑一个在一定空间内的流行病模型。人只能与附近的人交互作用,所以给人在一定的空间里随机运动。
首先,在一个2维平面,随机游走:
using Plots
using Compose
using Cairo
using Fontconfig
struct Coordinate{T}
x::T
y::T
end
#定义 +
function Base.:+(a::Coordinate, b::Coordinate)
return Coordinate(a.x+b.x, a.y+b.y)
end
possiblemoves = [
Coordinate( 1, 0),
Coordinate( 0, 1),
Coordinate(-1, 0),
Coordinate( 0,-1),
]
#每一步随机取possiblemoves中的一个,加上去,形成随机游走
function randwalk(P::Coordinate, n::Int)
accumulate(+, rand(possiblemoves, n); init = P)
end
#绘图
function p_tuple(c)
c.x, c.y
end
P = plot(ratio=1)
pline = randwalk(Coordinate(4,4), 1000)
figure = plot!(P, p_tuple.(pline),label=nothing, linewidth=2, linealpha=LinRange(1.0, 0.2, length(pline)))
#savefig(figure,"figure1.png")
function p_plot(p::Plots.Plot, line::Vector)
plot!(p, p_tuple.(line), label=nothing,
linewidth=2, linealpha=LinRange(1.0, 0.2, length(pline)))
end
p1 = plot(ratio=1)
for i ∈ 1:10
p_plot(p1, randwalk(Coordinate(4,4), 1000))
end
p1
给定一个空间,点在空间中移动,一旦超过边界,返回点在边界上:
空间大小为: L × L L\times L L×