【问题标题】:Newton's method for finding roots牛顿求根法
【发布时间】:2019-06-27 16:59:34
【问题描述】:

我正在尝试在 Python 中实现牛顿的求根方法。

预期结果是 B 点,但 Python 返回的是 A 点:

代码:

import matplotlib.pyplot as plt
import numpy as np

def f(theta):
    return 1 - (((2 * 1.5) * np.sin(theta))/ 2.7)

def derivative(f, x):
    dx = 1E-8
    return (f(x + dx) - f(x)) / dx

def x_next(f, x_n):
    return 1 - (f(x_n) / derivative(f, x_n))

def newtons_method(f, x_n = 1, i = 0, max_iter = 100):
    i = i + 1
    if (i == max_iter):
        return None
    x_n = x_next(f, x_n)
    if (abs(f(x_n)) < 1E-4):
        return x_n
    print("i:",i,"x_n:",x_n,"f(x_n)",f(x_n))
    newtons_method(f, x_n, i, max_iter)

print(newtons_method(f))

【问题讨论】:

    标签: python numpy math newtons-method


    【解决方案1】:

    您的主要问题在于您的日常工作x_next。你有一个1,应该有一个x_n。所以例程应该是

    def x_next(f, x_n):
        return x_n - (f(x_n) / derivative(f, x_n))
    

    你的衍生程序也很差。如果您必须近似导数,Newton-Raphson 不是最好的方法。您使用的近似方法在数值上也不好,尽管它确实遵循导数的定义。如果必须使用近似值,请使用

    def derivative(f, x):
        dx = 1E-8
        return (f(x + dx) - f(x - dx)) / (2.0 * dx)
    

    但是在这种情况下,导数很容易直接计算。所以最好用

    def derivative(f, x):
        return -2 * 1.5 * np.cos(x) / 2.7
    

    您也不会打印根及其函数值的最终近似值 - 您计算它并返回而不打印它。因此,在测试返回之前放置您的 print 语句。

    通过这些更改(加上注释掉您从未使用过的 matplotlib 的导入),您的代码现在是

    #import matplotlib.pyplot as plt
    import numpy as np
    
    def f(theta):
        return 1 - (((2 * 1.5) * np.sin(theta))/ 2.7)
    
    def derivative(f, x):
        return -2 * 1.5 * np.cos(x) / 2.7
    
    def x_next(f, x_n):
        return x_n - (f(x_n) / derivative(f, x_n))
    
    def newtons_method(f, x_n = 1, i = 0, max_iter = 100):
        i = i + 1
        if (i == max_iter):
            return None
        x_n = x_next(f, x_n)
        print("i:",i,"x_n:",x_n,"f(x_n)",f(x_n))
        if (abs(f(x_n)) < 1E-4):
            return x_n
        newtons_method(f, x_n, i, max_iter)
    
    print(newtons_method(f))
    

    结果只有两行

    i: 1 x_n: 1.1083264212579311 f(x_n) 0.005607493777795347
    i: 2 x_n: 1.1196379358595814 f(x_n) 6.373534192993802e-05
    

    这就是你想要的。如果你坚持对导数使用数值近似,使用我上面给出的版本,结果会略有不同:

    i: 1 x_n: 1.10832642185337 f(x_n) 0.005607493482616466
    i: 2 x_n: 1.1196379360265405 f(x_n) 6.373526104597182e-05
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 2022-01-10
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多