- 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 4 hours ago Re: What is in Your Mind? View the latest post Replies 673 Views 273180
- by Eli 8 hours ago Iran Launches Retaliatory Attack Against Israel View the latest post Replies 18 Views 403
- by Eli 10 hours ago Programmatically Move Files from One Folder to Another View the latest post Replies 6 Views 1266
- by Eli 1 day ago Russia Invades Ukraine View the latest post Replies 644 Views 209910
- by Forbidden_Technology 1 day ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 312 Views 8190
- by Eli 6 days ago Collection of Greatest Christian Hymns of all Times View the latest post Replies 33 Views 43378
- by Eli 1 week ago What is Retrieval-Augmented Generation (RAG)? View the latest post Replies 2 Views 212
- by Eli 1 week ago Chat With ChatGPT - An Interactive Conversational AI View the latest post Replies 22 Views 24069
- by Eli 2 weeks ago Christian Podcasts View the latest post Replies 5 Views 28574
- by Eli 2 weeks ago Pondering Big Cosmology Questions Through Lectures and Dialogues View the latest post Replies 33 Views 45300
-
Simple Python Class to calculate gravity between two objects
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5295
- 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