- 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 RealityKing 1 hour ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 274 Views 6525
- by Eli 11 hours ago Russia Invades Ukraine View the latest post Replies 643 Views 209057
- by Eli 11 hours ago Collection of Greatest Christian Hymns of all Times View the latest post Replies 30 Views 42694
- by Eli 1 day ago Chat With ChatGPT - An Interactive Conversational AI View the latest post Replies 21 Views 22689
- by Eli 5 days ago Re: What is in Your Mind? View the latest post Replies 658 Views 271378
- by Eli 6 days ago Mission Control Live: NASA InSight Mars Landing Live Coverage View the latest post Replies 32 Views 12962
- by Eli 6 days ago Christian Podcasts View the latest post Replies 4 Views 28281
- by Eli 1 week ago Python Packages for Scientific Computing View the latest post Replies 7 Views 2713
- by Eli 1 week ago Pondering Big Cosmology Questions Through Lectures and Dialogues View the latest post Replies 32 Views 44705
- by Eli 2 weeks ago C++ PROGRAMMING CRASH COURSE View the latest post Replies 4 Views 1292
-
Simple Python Class to calculate gravity between two objects
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5214
- 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