簡體   English   中英

使用 Scipy 輸出牛頓法的迭代次數

[英]Output number of iterations for Newton Method using Scipy

我想知道在使用牛頓法找到根時如何輸出迭代次數。 我正在使用 Scipy 自動計算根,所以我想知道是否有辦法知道它需要多少次迭代:

from spicy.optimize import newton
from math import *

f = lambda x : x**2 - sin(x)
ans = newton(f, 1, tol = 1.0E-8, maxiter = 100)
print(round(ans, 8))

Spicy 是 scipy 的一個很酷的名字。 :)

撇開玩笑不談,您只需要在對newton的調用中包含full_output=True (有關詳細信息,請參閱文檔)。 執行你的代碼,我通過打印 ans 得到這個輸出:

(0.8767262153950625,       converged: True
           flag: 'converged'
 function_calls: 7
     iterations: 6
           root: 0.8767262153950625)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM