Pregunta

I am having trouble with this equation.

from __future__ import division
import math

pi = 3.14159265
g = 6.67428*(10**-11)

user_circum = raw_input("Circumference (km) of planet? ")
user_acc = raw_input("Acceleration due to gravity (m/s^2)?")

def display_results(radius , mass , velocity):
    print "Radius of the planet"  , radius ,"km"
    print "Mass of the planet" , float(mass/10**15) ,"(10^21 kg)"
    print "Escape velocity of the planet" , velocity/1000 , "(km/s)"

def escape_velocity(circumference , acceleration):
    circumference = float(circumference)
    acceleration = float(acceleration)
    radius = circumference/(2*pi)
    mass = (acceleration * radius ** 2)/g
    vEscape = ((2*g*mass)/radius) ** 0.5
    display_results(radius , mass , vEscape)

escape_velocity(user_circum, user_acc)

With the user input of 38000 and 9.8:

Circumference (km) of planet? 38000
Acceleration due to gravity (m/s^2)? 9.8

I am supposed to get this answer:

Calculating the escape velocity...
Planet radius = 6047.9 km
Planet mass = 5370.7 x 10^21 kg
Escape velocity = 10.9 km/s

But instead I get this answer for escape velocity. The first 2 are correct.

Escape velocity of the planet 0.344294353352 (km/s)

This is the formula for escape velocity √2gm/r Any idea how to fix this? Thank you so much. How could I round my answers to one decimal place? Thanks.

¿Fue útil?

Solución 3

Fixing the radius units and correcting in display, it looks like:

from __future__ import print_function
from __future__ import division
from math import pi

G = 6.67428*(10**-11)

user_circum = raw_input("Circumference (km) of planet? ")
user_acc = raw_input("Acceleration due to gravity (m/s^2)?")

def display_results(radius, mass, velocity):
    print("Radius of the planet {:.1f}km".format(radius/1000))
    print("Mass of the planet {:.1f}(10^21 kg)".format(mass/10**21))
    print("Escape velocity of the planet {:.1f}(km.s)".format(velocity/1000))

def escape_velocity(circumference, acceleration):
    circumference = float(circumference)
    acceleration = float(acceleration)
    radius = circumference*1000/(2*pi)
    mass = (acceleration * radius ** 2)/G
    vEscape = ((2*G*mass)/radius) ** 0.5
    display_results(radius, mass, vEscape)                                                                                                                                     

escape_velocity(user_circum, use_acc)

Otros consejos

I think you are mixing up units. The user inputs the circumference in kilometers, but the gravitational constant you use is in meters. If I input the circumference in meters instead, the escape velocity gives the result you expect:

Circumference (km) of planet? 38000000
Acceleration due to gravity (m/s^2)?9.8
Radius of the planet 6047887.8444 km
Mass of the planet 5370677950.42 (10^21 kg)
Escape velocity of the planet 10.8875434213 (km/s)

Of course, the two first results are not good anymore. So, either adjust the gravitational constant to have it in kilometers, or better yet, always use meters. For convenience, you can always ask the user to input the circumference in kilometers and convert it to meters yourself.

Sounds like your life would be easier if you had a library to manage the units for you. Something like this would give you a headstart:

from collections import Counter

class UnitsThing(object):
    UNITS = ["kg", "km", "m", "s"]
    def __init__(self, measure, units):
        self.measure = measure
        if type(units) is str:
            if "/" not in units:
                over, under = units, ""
            else:
                over, under = units.split("/")
            o, u = self.unitize(over), self.unitize(under)
            o.subtract(u)
            self.units = o
        else:
            self.units = units
        p = self.units.get("km")
        if p:
            self.units["m"] += p
            self.units["km"] = 0
            self.measure *= 1000.0 ** p
    def __mul__(self, x):
        if type(x) is UnitsThing:
            new_units = self.units.copy()
            new_units.update(x.units)
            return UnitsThing(self.measure * x.measure, new_units)
        else:
            return UnitsThing(self.measure * x, self.units.copy())
    def __div__(self, x):
        if type(x) is UnitsThing:
            new_units = self.units.copy()
            new_units.subtract(x.units)
            return UnitsThing(self.measure / x.measure, new_units)
        else:
            print self.measure
            return UnitsThing(self.measure / x, self.units.copy())
    def __plus__(self, x):
        assert type(x) is UnitsThing
        assert x.units == self.units
        return UnitsThing(self.measure + x, self.units)
    def __sub__(self, x):
        assert type(x) is UnitsThing
        assert x.units == self.units
        return UnitsThing(self.measure - x, self.units)
    def unitize(self, units):
        c = Counter()
        for u in self.UNITS:
            while u in units:
                c[u] += 1
                i = units.index(u)
                units = units[:i] + units[i + len(u):]
        return c
    def unit_format(self, c):
        return "".join("{0}^{1}".format(k, v) for k, v in c.items() if v)
    def __str__(self):
        u = self.unit_format(self.units)
        return "{0} {1}".format(self.measure, u)
    def sqrt(self):
        return UnitsThing(self.measure ** 0.5, self.units)

Then you could write code like this:

from math import pi

g = UnitsThing(6.67428e-11, "mmm/kgss")
user_circum = UnitsThing(40075, "km")
user_acc = UnitsThing(9.81, "m/ss")
radius = user_circum / (2 * pi)
mass = (user_acc * (radius * radius))/g
vEscape = ((g * mass * 2.0) / radius).sqrt()

And you'd get results like this:

>>> print vEscape
11186.5542433 m^2s^-2

Added github repo for this.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top