This commit is contained in:
2025-05-26 15:29:54 +08:00
parent ecdae2085b
commit 3badad06a0
2 changed files with 96 additions and 0 deletions

34
160-11.py Normal file
View File

@@ -0,0 +1,34 @@
def SecantSolve(fx, x0, x1, err=1e-10, N0=100):
count = 0
print(f"k={count}: x{count}={x0}, f(x{count})={fx(x0)}")
count += 1
print(f"k={count}: x{count}={x1}, f(x{count})={fx(x1)}")
count += 1
while abs(x1 - x0) > err or abs(fx(x1)) > err:
if fx(x1) == fx(x0):
return None,0
x2 = x1 - fx(x1) * (x1 - x0) / (fx(x1) - fx(x0))
print(f"k={count}: x{count}={x2}, f(x{count})={fx(x2)}")
count += 1
if count > N0:
return None,-1
x0 = x1
x1 = x2
return x2,1
if __name__ == "__main__":
err = 1e-5
N0 = 100
x0 = 0.3
x1 = 0.4
fx = lambda x: x**4 - 3*x + 1
result, status = SecantSolve(fx, x0, x1, err, N0)
if status == 1:
print(f"fx收敛 解为: {result}")
elif status == -1:
print("fx不收敛")
else:
print("分母为0无法收敛")