我知道这已经在 C/C++ 中解决了,但是我对这些语言不够满意,无法将其转换为 python。我正在尝试在 python中创建它。我能来的最接近的是:
#This is meant to work for functions of the form x^a + b = 0
def secant(base, exp=2, it=20):
def f(x):
return x**exp - base
x1 = base / float(exp**2)
xnm1 = x1 - 5
xnm2 = x1 + 5
xn = 0
for n in range(it):
q = (xnm1-xnm2)/float(f(xnm1)-f(xnm2))
xn = xnm1 - (f(xnm1)*q)
xnm1, xnm2 = xn, xnm1
return xn
print secant(2, 2)
这将返回错误:
Traceback (most recent call last):
File "/Users/Joe/Desktop/secant.py", line 16, in <module>
print secant(2, 2)
File "/Users/Joe/Desktop/secant.py", line 11, in secant
q = (xnm1-xnm2)/float(f(xnm1)-f(xnm2))
ZeroDivisionError: float division by zero
但是,我能够编写基于此代码的牛顿方法。如果有帮助,这里是:
def newton(base, exp=2, it=20):
def f(x):
return x**exp - base
def df(x):
return exp*(x**(exp-1))
x1 = base / float(exp**2)
xnp = x1
xn = 0
for n in range(it):
xn = xnp - ((f(xnp)/df(xnp)))
xnp = xn
return xn
以下方法在 20 次迭代后给出了 12 位精度的答案。任何帮助,将不胜感激。