- 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 hour ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 317 Views 8602
- by Eli 7 hours ago Python Packages for Scientific Computing View the latest post Replies 8 Views 2858
- by Eli 8 hours ago Re: What is in Your Mind? View the latest post Replies 681 Views 273551
- by Eli 9 hours ago Dunia Yetu: Building Tanzania's Digital Future Together View the latest post Replies 5 Views 1728
- by Eli 10 hours ago Iran Launches Retaliatory Attack Against Israel, and Israel Retaliates by Attacking Iranian Isfahan Millitary Base View the latest post Replies 21 Views 627
- by Eli 2 days ago Russia Invades Ukraine View the latest post Replies 646 Views 210251
- by Eli 3 days ago Programmatically Move Files from One Folder to Another View the latest post Replies 6 Views 1360
- by Eli 1 week ago Collection of Greatest Christian Hymns of all Times View the latest post Replies 33 Views 43575
- by Eli 1 week ago What is Retrieval-Augmented Generation (RAG)? View the latest post Replies 2 Views 289
- by Eli 1 week ago Chat With ChatGPT - An Interactive Conversational AI View the latest post Replies 22 Views 24199
-
Simple Python Class to calculate gravity between two objects
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5315
- 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