繁体   English   中英

python中球体的体积

[英]Volume of sphere in python

我可以使用此代码轻松地在python中计算球体的体积。

import math

radius = input("Enter Radius: ")
print("Radius: " + str(radius))

r = float(radius)

volume = 4.0/3.0 * math.pi * (r*r*r)
print("Volume: " + str(round(volume,2)))

现在我的计划是找到n维的体积。 我得出的找到体积和体积变化的方程是 在此处输入图片说明

我想这样使用

import math

dimension = input("Enter dimension: ")
print("dimension: " + str(dimension))
n = float(dimension)
volume = math.pi^(n/2)/math.gamma(n/2 + 1)
print("Volume: " + str(round(volume,2)))

没用 您可以通过获取球体体积图来帮助我找到不同尺寸的体积吗?

您必须更改:

volume = math.pi(n/2)^2/math.gamma(n/2 + 1)

至:

volume = math.pi**(n/2)/math.gamma(n/2 + 1)

完整的代码:

import math

dimension = input("Enter dimension: ")
print("dimension: " + str(dimension))
n = float(dimension)
volume = math.pi**(n/2)/math.gamma(n/2 + 1)
print("Volume: " + str(round(volume,4)))

输入:

Enter dimension: 3

输出:

dimension: 3
Volume: 4.1888

额外:

import math

import matplotlib.pyplot as plt
x = []
y = []
for dimension in range(100):
    n = float(dimension)
    volume = math.pi**(n/2)/math.gamma(n/2 + 1)
    x.append(n)
    y.append(volume)

plt.plot(x, y)
plt.show()

在此处输入图片说明

看起来pow()是您所需要的:

import math

dimension = input("Enter dimension: ")
print("dimension: " + str(dimension))
n = float(dimension)
volume = pow(math.pi,(n/2))/math.gamma(n/2 + 1)
print("Volume: " + str(round(volume,2)))

pow(x,y) x返回到幂y。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM