0

我在这个方程上遇到了麻烦。

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)

使用 38000 和 9.8 的用户输入:

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

我应该得到这个答案:

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

但相反,我得到了逃逸速度的答案。前2个是正确的。

Escape velocity of the planet 0.344294353352 (km/s)

这是逃逸速度的公式 √2gm/r 知道如何解决这个问题吗?太感谢了。我怎么能把我的答案四舍五入到小数点后一位?谢谢。

4

3 回答 3

3

我认为您正在混淆单位。用户以千米为单位输入周长,但您使用的引力常数以米为单位。如果我以米为单位输入周长,则逃逸速度会给出您期望的结果:

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)

当然,前两个结果都不好了。所以,要么调整引力常数以公里为单位,要么更好的是,始终使用米。为方便起见,您始终可以要求用户输入以公里为单位的周长,然后自己将其转换为米。

于 2013-10-17T02:17:33.590 回答
1

如果你有一个图书馆来为你管理这些单元,听起来你的生活会更轻松。这样的事情会给你一个先机:

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)

然后你可以写这样的代码:

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()

你会得到这样的结果:

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

为此添加了 github 存储库

于 2013-10-17T04:44:16.270 回答
0

固定半径单位并在显示中更正,它看起来像:

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)
于 2013-10-17T02:27:37.740 回答