Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中求解f(x)=0的割线法_Python_Math_Methods - Fatal编程技术网

Python中求解f(x)=0的割线法

Python中求解f(x)=0的割线法,python,math,methods,Python,Math,Methods,如何使用Python中的割线方法来求解方程f(x)=0,给定两个初始猜测x0和x1 def secant(f,x0,x1,tol): 我需要用它来找到二次函数和更高的x因子的解,例如,给定区间的x^3-4x^2+1=0。 这只是一个在黑暗中的镜头,所以任何有用的网站链接也将受到赞赏 维基百科有一个 def secant(f, x0, x1, tol): # store initial values fx0 = f(x0) fx1 = f(x1) while ab

如何使用Python中的割线方法来求解方程f(x)=0,给定两个初始猜测x0和x1

def secant(f,x0,x1,tol):
我需要用它来找到二次函数和更高的x因子的解,例如,给定区间的x^3-4x^2+1=0。 这只是一个在黑暗中的镜头,所以任何有用的网站链接也将受到赞赏

维基百科有一个

def secant(f, x0, x1, tol):
    # store initial values
    fx0 = f(x0)
    fx1 = f(x1)
    while abs(fx1) < tol:
        # do calculation
        x2 = (x0 * fx1 - x1 * fx0) / (fx1 - fx0)
        # shift variables (prepare for next loop)
        x0,  x1  = x1,  x2
        fx0, fx1 = fx1, f(x2)
    return x1
def secant(f, x0, x1, tol, max_iterations=100):
    # keep initial values for error reporting
    init_x0 = x0
    init_x1 = x1

    # store y values instead of recomputing them
    fx0 = f(x0)
    fx1 = f(x1)

    # iterate up to maximum number of times
    for _ in range(max_iterations):
        # see whether the answer has converged
        if abs(fx1) < tol:
            return x1

        # do calculation
        x2 = (x0 * fx1 - x1 * fx0) / (fx1 - fx0)
        # shift variables (prepare for next loop)

        x0,  x1  = x1,  x2
        fx0, fx1 = fx1, f(x2)

    # for loop has ended - failed to converge
    raise ValueError(
        "call to secant(f={}, x0={}, x1={}, tol={}," \
        " max_iterations={}) has failed to converge"
        .format(
            f.__name__,
            init_x0,
            init_x1,
            tol,
            max_iterations
        )
    )

将第一次迭代转换为Python看起来像

x2 = (x0 * f(x1) - x1 * f(x0)) / (f(x1) - f(x0))
与多次调用
f(x0)
f(x1)
不同,我们应该调用它们一次并存储值:

fx0 = f(x0)
fx1 = f(x1)
x2 = (x0 * fx1 - x1 * fx0) / (fx1 - fx0)
对于第二次迭代,我们必须计算

x3 = (x1 * fx2 - x2 * fx1) / (fx2 - fx1)
但我们不想每次都用新的变量重新写方程。相反,我们将这些值移动:让
x1
x2
的值,
x2
x3
的值,依此类推。我们可以在循环中重复这样做,就像

# store initial values
fx0 = f(x0)
fx1 = f(x1)
while (something):
    # do calculation
    x2 = (x0 * fx1 - x1 * fx0) / (fx1 - fx0)
    # shift variables (prepare for next loop)
    x0,  x1  = x1,  x2
    fx0, fx1 = fx1, f(x2)
现在我们只需要知道何时停止,当最后一个答案与0之间的差值小于
tol
abs(f(x2)-0)
,可以简化为
abs(fx1)

所以函数看起来像

def secant(f, x0, x1, tol):
    # store initial values
    fx0 = f(x0)
    fx1 = f(x1)
    while abs(fx1) < tol:
        # do calculation
        x2 = (x0 * fx1 - x1 * fx0) / (fx1 - fx0)
        # shift variables (prepare for next loop)
        x0,  x1  = x1,  x2
        fx0, fx1 = fx1, f(x2)
    return x1
def secant(f, x0, x1, tol, max_iterations=100):
    # keep initial values for error reporting
    init_x0 = x0
    init_x1 = x1

    # store y values instead of recomputing them
    fx0 = f(x0)
    fx1 = f(x1)

    # iterate up to maximum number of times
    for _ in range(max_iterations):
        # see whether the answer has converged
        if abs(fx1) < tol:
            return x1

        # do calculation
        x2 = (x0 * fx1 - x1 * fx0) / (fx1 - fx0)
        # shift variables (prepare for next loop)

        x0,  x1  = x1,  x2
        fx0, fx1 = fx1, f(x2)

    # for loop has ended - failed to converge
    raise ValueError(
        "call to secant(f={}, x0={}, x1={}, tol={}," \
        " max_iterations={}) has failed to converge"
        .format(
            f.__name__,
            init_x0,
            init_x1,
            tol,
            max_iterations
        )
    )

请展示你已经尝试过的,堆栈溢出不是一个代码生成机器。听说过牛顿的方法吗?欢迎来到堆栈溢出!您似乎在要求某人为您编写一些代码。堆栈溢出是一个问答网站,而不是代码编写服务。请学习如何写有效的问题。