Skip to content

Commit bf959ee

Browse files
committed
Update random growth examples to latest Julia. randomgrowth2 now uses PyPlot
1 parent 892a419 commit bf959ee

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

demos/book/8/randomgrowth.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ function random_growth(M, N, q)
77

88
G[1,1] = true
99
# update the possible sets
10-
imagesc((0,N), (M,0), G)
10+
display(imagesc((0,N), (M,0), G))
1111
for t = 1:T
1212
sets = next_possible_squares(G)
1313
## Highlights all the possible squares
1414
for i = 1:length(sets)
1515
idx = sets[i]::(Int,Int)
1616
G[idx[1], idx[2]] = 0.25
1717
end
18-
imagesc((0,N), (M,0), G)
18+
display(imagesc((0,N), (M,0), G))
1919
sleep(.01)
2020

2121
## Actual growth
@@ -26,12 +26,12 @@ function random_growth(M, N, q)
2626
G[idx[1], idx[2]] = ison
2727
end
2828
end
29-
imagesc((0,N), (M,0), G)
29+
display(imagesc((0,N), (M,0), G))
3030
G[G .== 0.5] = 1
3131
G[G .== 0.25] = 0
3232
sleep(.01)
3333
end
34-
G
34+
return G
3535
end
3636

3737
function next_possible_squares(G)

demos/book/8/randomgrowth2.jl

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#randomgrowth2.jl
2-
using Winston
2+
using PyPlot
33
using ODE
4-
include("../1/gradient.jl")
54

65
function randomgrowth2()
76
num_trials = 2000
87
g = 1
98
q = 0.7
10-
Ns = 80 # [50, 100, 200]
9+
Ns = 80
10+
# Ns = [50, 100, 200]
11+
clf()
1112
# [-1.771086807411 08131947928329]
1213
# Gap = c d^N
1314
for jj = 1:length(Ns)
@@ -23,24 +24,25 @@ function randomgrowth2()
2324
end
2425
C = (B - N*om(g,q)) / (sigma_growth(g,q)*N^(1/3))
2526
d = 0.2
26-
x, n = hist(C - exp(-N*0.0025 - 3), -6:d:4)
27-
p = FramedPlot()
28-
h = Histogram(n/(d*num_trials), step(x))
29-
h.x0 = first(x)
30-
add(p, h)
27+
subplot(1,length(Ns),jj)
28+
plt.hist(C - exp(-N*0.0025 - 3), normed = true)
29+
xlim(-6, 4)
3130

3231
## Theory
3332
t0 = 4
3433
tn = -6
3534
dx = 0.005
3635
deq = (t, y) -> [y[2]; t*y[1]+2*y[1]^3; y[4]; y[1]^2]
3736
y0 = [airy(t0); airy(1,t0); 0; airy(t0)^2] # boundary conditions
38-
t, y = ode23(deq, t0:-dx:tn, y0) # solve
39-
F2 = exp(-y[:,3]) # the distribution
37+
t, y = ode23(deq, y0, t0:-dx:tn) # solve
38+
F2 = Float64[exp(-y[i][3]) for i = 1:length(y)] # the distribution
4039
f2 = gradient(F2, t) # the density
41-
42-
add(p, Curve(t, f2, "color", "red", "linewidth", 3))
43-
Winston.display(p)
40+
41+
# add(p, Curve(t, f2, "color", "red", "linewidth", 3))
42+
# Winston.display(p)
43+
subplot(1,length(Ns),jj)
44+
plot(t, f2, "r", linewidth = 3)
45+
ylim(0, 0.6)
4446
println(mean(C))
4547
end
4648
end
@@ -49,7 +51,7 @@ function G(N, M, q)
4951
# computes matrix G[N,M] for a given q
5052
GG = floor(log(rand(N,M))/log(q))
5153
#float(rand(N,M) < q)
52-
54+
5355
# Compute the edges: the boundary
5456
for ii = 2:N
5557
GG[ii, 1] = GG[ii, 1] + GG[ii-1, 1]
@@ -64,7 +66,7 @@ function G(N, M, q)
6466
## Plot
6567
# imagesc((0,N),(M,0),GG)
6668
# sleep(.01)
67-
69+
6870
return GG[N, M]
6971
end
7072

0 commit comments

Comments
 (0)