使用二分法找到 f(x) = sqrt(x) - cos(x) 的 p3,我在紙上得到了正確的答案 p3 = .625
我在轉換問題并使用 Jupyter notebook 解決問題時遇到了麻煩。有小費嗎?
Jupyter 筆記本代碼
uj5u.com熱心網友回復:
0.625是逼近程序中的第三次猜測,所以for回圈應該在第四次迭代之前完成,代碼應該如下。
import math
##problem statement
#finding a root of f(x) = sqrt(x)-cos(x) between the interval [0,1]
#f(0) = -1 f(1) = 0.45969.. so there is at least one root between the interval [0,1]
a = 0
b = 1
maxIt = 100
negative_result = math.sqrt(a) - math.cos(a)
positive_result = math.sqrt(b) - math.cos(b)
for i in range(maxIt):
if i == 3:
print("answer is: ", guess)
break
guess = (a b)/2
guess_result = math.sqrt(guess) - math.cos(guess)
if guess_result*negative_result >= 0:
a = guess
else:
b = guess
輸出是
0.625
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/424830.html
標籤:数学 jupyter-笔记本
