【问题标题】:Output number of iterations for Newton Method using Scipy使用 Scipy 输出牛顿法的迭代次数
【发布时间】:2019-11-29 14:20:55
【问题描述】:

我想知道如何在使用牛顿法找到根时输出迭代次数。我正在使用 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))

【问题讨论】:

    标签: python scipy newtons-method numerical-analysis


    【解决方案1】:

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

    除了笑话,您只需在对newton 的调用中包含full_output=True(有关更多详细信息,请参阅doc)。执行你的代码,我通过打印 ans 得到了这个输出:

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

    【讨论】:

    • 哈哈。输入代码,我没有注意到错字。感谢您的解决方案!
    • @Prog101 不用担心。请随时投票并接受答案。 :)
    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2018-01-03
    • 2023-03-06
    相关资源
    最近更新 更多