编辑代码

import math

def f(x):
    return 3 * x + math.sin(x) - math.exp(x)

def secant(x0, x1, tolerance):
    while True:
        x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0))
        if abs(x2 - x1) < tolerance:
            return x2
        x0, x1 = x1, x2

root = secant(0, 1, 1e-5)

print(f"The root is: {root:.5f}")