- class Gravity:
- """Gravitational force between two physical objects."""
- def __init__(self, m, M):
- self.m = m # mass of object 1
- self.M = M # mass of object 2
- self.G = 6.67428E-11 # gravity constant, m**3/kg/s**2
- def force(self, r):
- G, m, M = self.G, self.m, self.M
- return G*m*M/r**2
- def visualize(self, r_start, r_stop, n=100):
- from scitools.std import plot, linspace # Can use numpy.linspace
- import matplotlib.pyplot as plt
- r = linspace(r_start, r_stop, n)
- g = self.force(r)
- title='Gravity force: m=%g, M=%g' % (self.m, self.M)
- plot(r, g, title=title)
- fig = plt.figure()
- fig.savefig("Nothing.png")
- #plt.show()
- #Demo
- mass_moon = 7.35E+22; mass_earth = 5.97E+24
- gravity = Gravity(mass_moon, mass_earth)
- r = 3.85E+8 # Earth-Moon distance in meters
- Fg = gravity.force(r)
- print 'force:', Fg
- Visualize = gravity.visualize(r-100,r, 100)
-
- Active Topics
-
-
- by Eli 1 day ago Inaugural Lecture by Prof. Mukandala: Dola, Soko na Kushindwa kwa Taasisi View the latest post Replies 2 Views 91
- by Eli 2 days ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 332 Views 42217
- by Eli 2 days ago Iran's President Ebrahim Raisi Aged 63 Dies in a Helicopter Crash View the latest post Replies 3 Views 72
- by Eli 2 days ago Re: What is in Your Mind? View the latest post Replies 717 Views 309049
- by Eli 5 days ago PySpark for Large Data Processing View the latest post Replies 2 Views 8211
- by Eli 5 days ago Online Bible View the latest post Replies 3 Views 23342
- by Eli 5 days ago Generating SSH Key and Adding it to the ssh-agent for Authentication on GitHub View the latest post Replies 1 Views 547
- by Eli 1 week ago Russia Invades Ukraine View the latest post Replies 663 Views 242979
- by Eli 2 weeks ago President Museveni's Speech During International Development Association (IDA) Summit View the latest post Replies 1 Views 512
- by Eli 2 weeks ago From Simple Linear Regression Analysis to Covariance & Correlation to Independent Determinant, and R-Squared View the latest post Replies 11 Views 25162
-
Simple Python Class to calculate gravity between two objects
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5414
- Joined: 9 years ago
- Location: Tanzania
- Has thanked: 75 times
- Been thanked: 88 times
- Contact:
This is a simple implementation of Python class to calculate gravitational force between two objects, say the earth and moon.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
-
- Similar Topics
- Replies
- Views
- Last post
-
- Information
-
Who is online
Users browsing this forum: No registered users and 0 guests