将伪代码翻译成python(正割法)

Translate pseudocode into python (secant method)

我正在做这个作业:

First, implement the f-function defined by: f(x)= exp(x)-sin(x) closest to zero.

Second, implement the Secant method on page 95 and use it to find the root of the f-function, given the input values x0 = -3.5 and x1 = -2.5

Add the following
- an absolute test: abs(f(x) ) < epsilon
- a relative test: abs(x^k - x^{k-1})/ abs(x^{k}) \leq delta
- a maximum iteration guard: k < iter_max

In each iteration print out the iteration number k, the value of current root and current f-value. Print float numbers with 20 digits.

这是我必须完成的代码:

import numpy as np
from math import exp, sin
import matplotlib.pyplot as plt

def f(x: float) -> float:
    return

def secant(x0: float, x1: float, f, epsilon: float, delta: float, iter_max: int) -> float:
    return

这是第 95 页的伪代码:

input: x_0, x_1, delta, epsilon, iter_max
fx_0 <- f(x_0); fx_1 <- f(x_1)
output: 0, x_0, fx_0
output: 1, x_1, fx_1
for k = 2 to iter_max do
    if |fx_0| > |fx_1| then
        x_0 <-> x_1; fx_0 <-> fx_1
    end if
    s <- (x_1 - x_0) / (fx_1 - fx_0)
    x_1 <- x_0
    fx_1 <- fx_0
    x_0 <- x_0 - fx_0 * s
    fx_0 <- f(x_0)

    output: k, x_0, fx_0
    if |fx_0| < epsilon or |x_1 - x_0| < delta then stop
end do

这是我自己的尝试:

def f(x: float) -> float:
    return exp(x) - sin(x) == 0

def secant(x0: float, x1: float, f, epsilon: float, delta: float, iter_max: int) -> float:
    fx0 = f(x0)
    fx1 = f(x1)
    return 0, x0, fx0
    return 1, x1, fx1

    for k in range(2, iter_max):
        if abs(fx0) > abs(fx1):
            x0 = x1
            x1 = x0
            fx0 = fx1
            fx1 = fx0

            s = (x1 - x0) / (fx1 - fx0)
            x1 = x0
            fx1 = fx0
            x0 = x0 - fx0 * s
            fx0 = f(x0)

        return k, x0, fx0

        if abs(fx0) < epsilon or abs(x**k - x**(k - 1))/ abs(x**(k))  <= delta:
            break

如果我按照我的代码

root = secant(-3.5, -2.5, f, 0.00000000001, 0.000001, 10) 
print(root) 

我得到:(0,-3.5,假)。所以它实际上并没有做任何迭代。我该如何解决?

编辑:
伪代码照片

此处:a=x_0、b=x_1 和 M=iter_max.
我希望输出是这样的:

您的实施存在以下错误:

  • 首先是函数f必须return要求根的函数的值,不能和零比较。

  • 第二个错误是由于没有阅读算法的objective和理解其过程造成的,如果它说:


output: 0, x_0, fx_0
output: 1, x_1, fx_1

表示是iter_max分别为0或1时的结果

  • 三、交换值的形式不充分,必须使用pivot否则会抹掉相关信息

  • 我不明白的另一件事是因为您执行以下内容:abs(x**k - x**(k - 1))/ abs(x**(k)) 而不是 abs(x1 - x0)

所以更正这两个错误你会得到以下代码:

def f(x: float) -> float:
    return exp(x) - sin(x)

def secant(x0: float, x1: float, f, epsilon: float, delta: float, iter_max: int) -> float:
    fx0 = f(x0)
    fx1 = f(x1)
    if iter_max == 0:
        return 0, x0, fx0

    elif iter_max == 1:
        return 1, x1, fx1

    for k in range(2, iter_max):
        if abs(fx0) > abs(fx1):
            tmp =  x0
            x0 = x1
            x1 =tmp
            tmp = fx0
            fx0 = fx1
            fx1 = tmp

        s = (x1 - x0) / (fx1 - fx0)
        x1 = x0
        fx1 = fx0
        x0 = x0 - fx0 * s
        fx0 = f(x0)
        if abs(fx0) < epsilon or abs(x1 - x0)  <= delta:
            break
    return k, x0, fx0


root = secant(-3.5, -2.5, f, 0.00000000001, 0.000001, 10) 

print(root)

输出:

(5, -3.183063011933318, 4.7351012000262926e-14)