Last active
September 7, 2015 19:57
-
-
Save mvw/ae1fac79db95bb7969a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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