Skip to content

Instantly share code, notes, and snippets.

@mvw
Last active September 7, 2015 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvw/ae1fac79db95bb7969a1 to your computer and use it in GitHub Desktop.
Save mvw/ae1fac79db95bb7969a1 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# gravnet.rb
require "matrix"
# simplified gravitational force (G=m=M=1.0)
# from (i,j) to r
def fij(i, j, r)
# difference vector
rij = Vector[i,j] - r
# length of difference vector
d = rij.r
# force
f = rij / (d**3)
return f
end
# force summing over the grid square [-n,n] x [-n, n]
def fsum(r, n)
f = Vector[0.0, 0.0]
(-n).upto(n) do |i|
(-n).upto(n) do |j|
f += fij(i, j, r)
end
end
return f
end
def list(r, n_max)
# show the last three total forces
(n_max-2).upto(n_max) do |n|
f = fsum(r, n)
puts "n = #{n}: r = #{r}, F = #{f}"
end
puts
end
n_max = 100
list(Vector[0.5,0.5], n_max)
list(Vector[0.25,0.25], n_max)
list(Vector[0.25,0.0], n_max)
list(Vector[0.0,0.25], n_max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment