aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2024-03-11 13:42:23 -0400
committersotech117 <michael_foiani@brown.edu>2024-03-11 13:42:23 -0400
commit7b4d951fa00ee0e94d1d1b65a2f2f06cb9850146 (patch)
tree9c7c77ef669ab5d96f5ee3611a7fd9932adf845a
parent807b632dcf8ef4ab24ca8c0bb1ed94fdf0c05cb1 (diff)
add files via upload
-rw-r--r--figs/rs-1dvels.pngbin0 -> 28263 bytes
-rw-r--r--figs/rs-3dvels.pngbin0 -> 60791 bytes
-rw-r--r--figs/rs-speeds.pngbin0 -> 17602 bytes
-rw-r--r--figs/rw-demo1.pngbin0 -> 25997 bytes
-rw-r--r--figs/rw-demo3.pngbin0 -> 134331 bytes
-rw-r--r--random-speed.jl69
-rw-r--r--random-walk.jl147
7 files changed, 216 insertions, 0 deletions
diff --git a/figs/rs-1dvels.png b/figs/rs-1dvels.png
new file mode 100644
index 0000000..c4c06d4
--- /dev/null
+++ b/figs/rs-1dvels.png
Binary files differ
diff --git a/figs/rs-3dvels.png b/figs/rs-3dvels.png
new file mode 100644
index 0000000..8dae989
--- /dev/null
+++ b/figs/rs-3dvels.png
Binary files differ
diff --git a/figs/rs-speeds.png b/figs/rs-speeds.png
new file mode 100644
index 0000000..6110329
--- /dev/null
+++ b/figs/rs-speeds.png
Binary files differ
diff --git a/figs/rw-demo1.png b/figs/rw-demo1.png
new file mode 100644
index 0000000..7e64a49
--- /dev/null
+++ b/figs/rw-demo1.png
Binary files differ
diff --git a/figs/rw-demo3.png b/figs/rw-demo3.png
new file mode 100644
index 0000000..09e8623
--- /dev/null
+++ b/figs/rw-demo3.png
Binary files differ
diff --git a/random-speed.jl b/random-speed.jl
new file mode 100644
index 0000000..0af9121
--- /dev/null
+++ b/random-speed.jl
@@ -0,0 +1,69 @@
+using Plots
+using Distributions
+
+num_velocities = 1000
+
+println("Starting Random Speed Simualtions...\n")
+
+function make_random_velocity()
+ # pull 3 nums randomly from normal distribution
+ N = Normal(0, 1)
+ return (rand(N), rand(N), rand(N))
+end
+
+function plot_frequencies(x)
+ # make a map each positions to it's Frequency
+ freqs = Dict()
+ for xi in x
+ if haskey(freqs, xi)
+ freqs[xi] += 1
+ else
+ freqs[xi] = 1
+ end
+ end
+
+ num_points = length(x)
+ plt = scatter(
+ collect(keys(freqs)), collect(values(freqs)),
+ label="Frequency", xlabel="Position", ylabel="Frequency",
+ title="Freq of Positions ($num_points points)",
+ msw=0, color=:green, legend=false)
+ return plt
+end
+
+
+# make velocities
+velocities = [make_random_velocity() for _ in 1:num_velocities]
+
+# plot these velocities on 1d plots
+px = histogram([v[1] for v in velocities], bins=30, title="X Velocity")
+py = histogram([v[2] for v in velocities], bins=30, title="Y Velocity")
+pz = histogram([v[3] for v in velocities], bins=30, title="Z Velocity")
+
+# print the mean and standard deviation of each velocity distribution
+println("X->\tμ:$(mean([v[1] for v in velocities])), σ:$(std([v[1] for v in velocities])), n:$(length([v[1] for v in velocities]))")
+println("Y->\tμ:$(mean([v[2] for v in velocities])), σ:$(std([v[2] for v in velocities])), n:$(length([v[2] for v in velocities]))")
+println("Z->\tμ:$(mean([v[3] for v in velocities])), σ:$(std([v[3] for v in velocities])), n:$(length([v[3] for v in velocities]))")
+
+p = plot(px, py, pz, layout=(3, 1), size=(800, 800))
+savefig(p, "figs/rs-1dvels.png")
+
+# plot these velocities in 3d space
+p = Plots.scatter(
+ [v[1] for v in velocities],
+ [v[2] for v in velocities],
+ [v[3] for v in velocities],
+ title="Velocities")
+savefig(p, "figs/rs-3dvels.png")
+
+# plot their speeds
+speeds = [sqrt(v[1]^2 + v[2]^2 + v[3]^2) for v in velocities]
+p = histogram(
+ speeds, title="Randomly Generated Speeds (n=$num_velocities)",
+ legend=false, xlabel="Speed", ylabel="Frequency")
+savefig(p, "figs/rs-speeds.png")
+# print the mean and standard deviation of the speed distribution
+println("\nSpeed->\tμ:$(mean(speeds)), σ:$(std(speeds)), n:$(length(speeds))")
+
+
+println("\nRandom Speed Simualtions Complete!") \ No newline at end of file
diff --git a/random-walk.jl b/random-walk.jl
new file mode 100644
index 0000000..51e58b8
--- /dev/null
+++ b/random-walk.jl
@@ -0,0 +1,147 @@
+using Plots # for plotting trajectory
+using Distributions # for fitting a normal distribution
+
+function dynamics!(params) # ! notation tells us that arguments will be modified
+ (x_change_per_step, num_steps, num_points) = params
+
+ x = zeros(num_points) # array to store x positions after the random walks
+
+ for i in 1:num_points
+ # determine the direction of the step
+ # set starting point to x
+ x[i] = 0.0
+
+ # simulate the random walk
+ for _ in 1:num_steps
+ if rand() < 0.5
+ # go left, negative
+ dx = -x_change_per_step
+ x[i] += dx
+ else
+ # go right, positive
+ dx = x_change_per_step
+ x[i] += dx
+ end
+ end
+ end
+
+
+ μ = sum(x) / length(x)
+ σ = sqrt(sum((x .- μ).^2) / length(x))
+ normal = Normal(μ, σ)
+
+
+ println("μ: $μ, σ: $σ, n: $(length(x))")
+
+ return x
+end
+
+function plot_frequencies(x)
+
+ # make a map each positions to it's Frequency
+ freqs = Dict()
+ for xi in x
+ if haskey(freqs, xi)
+ freqs[xi] += 1
+ else
+ freqs[xi] = 1
+ end
+ end
+
+ num_points = length(x)
+ plt = scatter(
+ collect(keys(freqs)), collect(values(freqs)),
+ label="Frequency", xlabel="Position", ylabel="Frequency",
+ title="Freq of Positions ($num_points points)",
+ msw=0, color=:green, legend=false)
+ return plt
+end
+
+function plot_histogram(x)
+ # make a histogram of the random walk
+ num_points = length(x)
+ plt = histogram(x, xlabel="Position",
+ ylabel="Frequency", legend=false,
+ title="Histogram of Random Walk")
+ return plt
+end
+
+function fit_normal(x)
+ # fit a normal distribution to the random walk
+ μ = sum(x) / length(x)
+ σ = sqrt(sum((x .- μ).^2) / length(x))
+ normal = Normal(μ, σ)
+
+ # get the pdf's over the min and max values found
+ x_min = minimum(x)
+ x_max = maximum(x)
+ bigger = max(abs(x_min), abs(x_max)) # center the plot
+ x_range = range(-bigger, bigger, length=100)
+ pdf_vals = pdf.(normal, x_range)
+
+ # plot the normal distribution
+ return plot(x_range, pdf_vals, lw=2, color=:red, title="Normal Fit", legend=false)
+end
+
+function demo_1(num_points=1000)
+ # simulation parameters
+ x_change_per_step = 1.0 # step size
+ num_steps = 100 # number of steps in the random walk
+ params = (x_change_per_step, num_steps, num_points)
+ x = dynamics!(params) # run the random walk
+
+ # plot only the frequencies
+ p = plot_frequencies(x)
+ savefig(p, "figs/rw-demo1.png")
+end
+
+function demo_3()
+ # simulation parameters
+ x_change_per_step = 1.0 # step size
+ num_steps = 100 # number of steps in the random walk
+
+ # run the random walk with a smaller number of points
+ num_points = 15 # number of random walks
+ params = (x_change_per_step, num_steps, num_points)
+ x_1 = dynamics!(params) # run the random walk
+
+ # run a second random walk with 1000 points
+ num_points = 100
+ params = (x_change_per_step, num_steps, num_points)
+ x_2 = dynamics!(params) # run the random walk
+
+ # run a third random walk with 10000 points
+ num_points = 10000
+ params = (x_change_per_step, num_steps, num_points)
+ x_3 = dynamics!(params) # run the random walk
+
+ # plot the results
+ p0_1 = plot_frequencies(x_1)
+ p1_1 = plot_histogram(x_1)
+ p2_1 = fit_normal(x_1)
+
+ p0_2 = plot_frequencies(x_2)
+ p1_2 = plot_histogram(x_2)
+ p2_2 = fit_normal(x_2)
+
+ p0_3 = plot_frequencies(x_3)
+ p1_3 = plot_histogram(x_3)
+ p2_3 = fit_normal(x_3)
+
+ p = plot(
+ p0_1, p1_1, p2_1,
+ p0_2, p1_2, p2_2,
+ p0_3, p1_3, p2_3,
+ layout=(3, 3),
+ size=(1000, 1000),
+ )
+
+ savefig(p, "figs/rw-demo3.png")
+end
+
+println("Starting Random Speed Simulations...\n")
+
+# demo_1(1000000) # plot a frequncy plot of param n points
+demo_3()
+
+println("\nRandom Walk Simulations Complete!")