[英]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.