我想在我的问题开头说,我对Python非常陌生,并且只开始在研究生院的某个特定班级使用Python。
我编写了一个脚本,使用迭代方法,例如定点、二分法和Newton方法找到函数的根。我的算法的python代码如下:
定点法:
def fixedpoint(func, g_func,x0,tol, MAXIT):
def g(x):
return eval(g_func)
print(f"Seeking root of {func}.")
print("FIXED POINT ITERATION:")
iterated_x = g(x0)
for i in range(0, MAXIT):
iterated_x = g(iterated_x)
print("Iteration", i + 1, iterated_x)
condition = abs(g(iterated_x) - (iterated_x))
if condition < tol:
print(f"The root converges to {iterated_x} after {i+1} iterations.")
break
if i == MAXIT and condition >= tol:
print("ERROR: The root did not converge after maximum iterations.")
fixedpoint('x**2 - 4*x + 2', '(x**2+2)/4', 0.75, 10**(-4), 100)
fixedpoint('math.exp(-x) + x - 7 ', '7 - math.exp(-x)', 0.75, 10**(-4), 100)
二分法:
def bisection(func,a, b, tol, MAXIT):
def f(x):
return eval(func)
print(f"Seeking root of {func}")
print("BISECTION METHOD:")
if f(a)*f(b) <0:
for i in range(0, MAXIT):
c = (a+b)/2
if f(a)*f(c) < 0:
a = a
b = c
print(f"Iteration", i + 1, f(c))
elif f(b)*f(c) < 0:
b = b
a = c
print(f"Iteration", i + 1, f(c))
if f(c) == 0 or abs(f(c)) < tol:
print ("Exact Solution Found")
print(f"Iteration", i + 1, c)
print("The root converges to", c, "after", i + 1, "iterations.")
break
elif i == MAXIT and abs(f(c)) > tol:
print("Bisection method fails.")
return None
bisection('x**2 - 4*x + 2',0.5, 1, 10**(-4), 100)
bisection('math.exp(-x) + x - 7',6, 8, 10**(-4), 100)
牛顿-拉夫森方法:
def newtonraphson(func, deriv, x0, tol, MAXIT):
def f(x):
return eval(func)
def ddx(x):
return eval(deriv)
print(f"Seeking root of {func}.")
print("NEWTON-RAPHSON METHOD:")
for i in range(1, MAXIT):
iterated_x = x0 - (f(x0)/ddx(x0))
x0 = iterated_x
print(f"Iteration", i, x0)
if f(x0) < tol:
print(f"The root converges to {x0} after {i} iterations.")
break
elif i==MAXIT and f(x0) > tol:
print("After maximum iterations, root was not found.")
newtonraphson('x**2-4*x+2', '2*x-4', 0.75,10**(-4), 100)
newtonraphson('math.exp(-x) + x - 7', '-(math.exp(-x)) + 1', 0.75,10**(-4), 100)
虽然我的脚本能够成功地找到我感兴趣的方程的根,但我还是遇到了一个更简单的问题。基本上,我想告诉我的程序,如果在最大的迭代之后,我的容忍度条件没有得到满足,那么打印“方法失败”。
但是,我的代码没有打印我想要的语句,当我试验那些当最大迭代设置为100,公差设置为0.0001时不覆盖的函数时。
我的语法对fail语句正确吗?
在我编写的函数的上下文中,像"if i==MAXIT and f(x0) > tol“这样的条件有意义吗?
对于这个问题,我希望得到任何和所有的建议,因为我正在努力改进Python。
谢谢。
发布于 2022-02-10 18:19:34
在Python中实现这一目标的一个好方法是使用for\else
构造。本质上,如果在for循环中挂起一个“off”子句,如果循环没有因为break
语句而提前终止,它就会执行。它对于寻找某些东西的循环非常有用,而根查找绝对符合这个模型。因此,不动点迭代循环如下所示:
for i in range(0, MAXIT):
iterated_x = g(iterated_x)
condition = abs(g(iterated_x) - (iterated_x))
if condition < tol:
print(f"The root converges to {iterated_x} after {i+1} iterations.")
break
else:
print("ERROR: The root did not converge after maximum iterations.")
这样做的一个好处是,如果在循环中更改成功标准并不重要--如果您不使用break语句声明成功,则会运行One子句,而不管如何定义成功。
发布于 2022-02-10 00:05:54
您所面临的问题是,python中的range
只能从起始索引到结束索引之前的一个。也就是说,range(0,3)
超过数字(0,1,2)。代码中发生的情况是,您根本无法激活失败条件,因为索引实际上从未达到该值。然而,解决这个问题很容易。在你的职责中,你说:
for i in range(1,MAXIT):
if f(x0) < tol:
print(f"The root converges to {x0} after {i} iterations.")
break
elif i==MAXIT and f(x0) > tol:
print("After maximum iterations, root was not found.")
只要把它换成
for i in range(1,MAXIT):
if f(x0) < tol:
print(f"The root converges to {x0} after {i} iterations.")
return
print("After maximum iterations, root was not found.")
而不是。如果函数找到了解决方案,就会停止该函数,如果您设法通过整个循环而没有找到答案,则会给出错误消息。
https://stackoverflow.com/questions/71058213
复制相似问题