This commit is contained in:
lwj
2025-06-17 21:07:52 +08:00
parent fc0e7cec5f
commit 515714cbf5

View File

@@ -2,7 +2,7 @@ import math
def NewtonDownHillSolve(fx, dfx, x0, err1,err2, N0,min_t): def NewtonDownHillSolve(fx, dfx, x0, err1,err2, N0,min_t):
count = 0 count = 0
print(f"k={count}, x0={x0}") print(f"k={count}, x0={x0}\n")
x1 = x0 + 1 + err1 x1 = x0 + 1 + err1
while abs(x1 - x0) > err1 or abs(fx(x1)) > err2: while abs(x1 - x0) > err1 or abs(fx(x1)) > err2:
t = 1 t = 1
@@ -12,7 +12,7 @@ def NewtonDownHillSolve(fx, dfx, x0, err1,err2, N0,min_t):
print(f"当前点: x0={x0}") print(f"当前点: x0={x0}")
while t >= min_t: while t >= min_t:
x1 = x0 - t * fx(x0) / dfx(x0) x1 = x0 - t * fx(x0) / dfx(x0)
print(f"下山: t={t}, x1={x1}, abs(fx(x1))={abs(fx(x1))}, abs(fx(x0))={abs(fx(x0))}") print(f"下山: t={t}, x1={x1}, fx(x{count+1})={fx(x1)}, fx(x{count})={fx(x0)}")
if abs(fx(x1)) < abs(fx(x0)): if abs(fx(x1)) < abs(fx(x0)):
break break
t *= 0.5 t *= 0.5
@@ -21,7 +21,7 @@ def NewtonDownHillSolve(fx, dfx, x0, err1,err2, N0,min_t):
return None, -2 return None, -2
# x1 = x0 - fx(x0) / dfx(x0) # x1 = x0 - fx(x0) / dfx(x0)
count += 1 count += 1
print(f"k={count}, x{count}={x1},x1-x0={abs(x1-x0)}") print(f"k={count}, x{count}={x1},x1-x0={abs(x1-x0)}\n")
if count > N0: if count > N0:
return None, -1 return None, -1
x0 = x1 x0 = x1