我目前主修數學,輔修物理,因此需要用 python 做一些家庭作業。我從來沒有在 python 中編碼(java 中的微不足道的經驗),也從未參加過 uni(ik) 分配的 python 類,目前我試圖通過測量 python 中圓圖下的面積來近似 pi。
我的問題是,雖然控制臺中沒有出現錯誤并且我正在呼叫該函式,但沒有顯示任何輸出(當我將 return 放在 print 陳述句上方時)
但是,當我在 print 陳述句之后放置 return 時,出現數學域錯誤;我以前在 halfCircle 函式中定義 y 時無法使用 x^2,而是嘗試使用 pow(x,2) 來解決它。
目前我只將最終結果實作為 pi/2。并且我知道這是一個不好的近似值,因為我只讓面積塊在圖表下方。如果此代碼有效,我將實作另一個函式,該函式對遍歷圖形的面積塊執行相同的操作,然后計算它們的平均值。然后我將它乘以 2。
我目前對將 return 陳述句放在哪里以及數學域錯誤是怎么回事感到困惑。這是我的代碼:
`
import math
def halfCircle(x):
y = abs(math.sqrt(1-pow(x,2)))
return y
#N is the Number of pieces we try to approximate pi with, the higher N, the closer G will be to pi.
def areaUnderGraph(N):
#x1 is initialized to be -1 since our range starts at x0 = -1
x1 = float(-1)
#x2 will be set to 0 at the moment.
x2 = float(0)
#We use G as the final area under the graph and set it to 0 to begin with.
G = 0
#Now we need to know how thick our areas will be, [-1,1] is 2 long on the x axis, so we divide 2 by N:
d = 2/N
#Now we come to calculating our areas and adding them up until we hit 1 as the end of the given range.
while x2 <= 1:
#x2 will be x1 d since we're adding from left to right.
x2 = x1 d
#Since we need the smaller area pieces we calculate the corresponding y values for x1 and x2 so as to discern the smaller area value.
y2 = halfCircle(x2)
y1 = halfCircle(x1)
#The area is smaller, if y is smaller;
a1 = (x2 - x1) * y1
a2 = (x2 - x1) * y2
if a1 > a2:
a = a2
else:
#if a2 and a1 are equal it doesn't matter wich value a takes (a1 or a2)
a = a1
#for the next step we let x1 become x2 since that will be the leftmost point of the next area piece.
x1 = x2
#We define G to be the area made up of all the previous areas and the newly calculated one.
G = G a
print('The Area under the graph is ' G)
return G
areaUnderGraph(100)
`
uj5u.com熱心網友回復:
math.sqrt()不取負數。當我嘗試使用負值作為引數時,它顯示數學域錯誤。
看這個例子:
import math
math.sqrt(-4)
Traceback (most recent call last):
File "/usr/lib/python3.10/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
ValueError: math domain error
abs所以,我在呼叫方法之前更改了要使用的函式sqrt。
更新代碼:
import math
def halfCircle(x):
y = math.sqrt(abs(1 - pow(x, 2)))
return y
# N is the Number of pieces we try to approximate pi with, the higher N, the closer G will be to pi.
def areaUnderGraph(N):
# x1 is initialized to be -1 since our range starts at x0 = -1
x1 = float(-1)
# x2 will be set to 0 at the moment.
x2 = float(0)
# We use G as the final area under the graph and set it to 0 to begin with.
G = 0
# Now we need to know how thick our areas will be, [-1,1] is 2 long on the x axis, so we divide 2 by N:
d = 2 / N
# Now we come to calculating our areas and adding them up until we hit 1 as the end of the given range.
while x2 <= 1:
# x2 will be x1 d since we're adding from left to right.
x2 = x1 d
# Since we need the smaller area pieces we calculate the corresponding y values for x1 and x2 so as to discern the smaller area value.
y2 = halfCircle(x2)
y1 = halfCircle(x1)
# The area is smaller, if y is smaller;
a1 = (x2 - x1) * y1
a2 = (x2 - x1) * y2
if a1 > a2:
a = a2
else:
# if a2 and a1 are equal it doesn't matter wich value a takes (a1 or a2)
a = a1
# for the next step we let x1 become x2 since that will be the leftmost point of the next area piece.
x1 = x2
# We define G to be the area made up of all the previous areas and the newly calculated one.
G = G a
return G
print(f'The Area under the graph is {areaUnderGraph(100)}')
輸出:
The Area under the graph is 1.5491342564916826
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537658.html
標籤:Python数学输出
上一篇:python應用程式的sentrysdk自定義性能集成
下一篇:根據角度查找橢圓中點的坐標
