Python 牛顿-拉斐逊法方程求解算法
在下面的代码中,当我选择例如“max_n_iterations”等于1时,列表“approximations”在打印时会显示两个元素,其中应该只显示一个(初始x) 这是什么原因Python 牛顿-拉斐逊法方程求解算法,python,algorithm,numerical-methods,newtons-method,Python,Algorithm,Numerical Methods,Newtons Method,在下面的代码中,当我选择例如“max_n_iterations”等于1时,列表“approximations”在打印时会显示两个元素,其中应该只显示一个(初始x) 这是什么原因 #This exercise shows an immediate way to find the root of a real valued funciton, using successive better approximations #This method is known as Newton Raphso
#This exercise shows an immediate way to find the root of a real valued funciton, using successive better approximations
#This method is known as Newton Raphson method
print 'Find the root of a given function - NEWTON RAPHSONS METHOD'
print 'The function is the following: ...'
x=input('Choose an initial estimate:') #An initial estimate is necessary to be choosen to start the iteration process
x=float(x) #The number inserted is transformed into a floating point number
max_n_iterations=input('Choose max n. iterations:') #The user decides the maximum number of iterations to run
approximations = [] #Vector collecting all the intermediate solutions; i.e. the approximated roots evaluated before reaching the final solution
iterations= []
def f(x): #The given function has to be inserted manually in the code
return 2*x**3+45*x+1/x**2+16
def D(f): #Evaluates the first derivative of the given function, using the definition of derivative
def df(x, h): #x is the initial estimate, h is the increment
return (f(x+h) - f(x))/h #Difference quotient
return df #First derivative
def newtons_method(f, x, h=0.000001, epsilon=0.000001,): #This is the main process: f is the given function, x and h are the same as above, epsilon is the tolerance level. Epsilon and h have to be choosen sufficiently small
df = D(f) #df is just the first derivative, as before
for i in range(max_n_iterations): #The code runs until the maximum number of iterations is reached
x1 = x - f(x)/df(x, h) #Essence of Newton Raphson method: the iteration process
approximations.append(x) #Every intermediate solution is collected into a vector, as defined above
iterations.append(1)
if abs(x1 - x) < epsilon: #When the absolute difference between two successive roots is less than the tolerance level (i.e. the sequence of solutions strongly converges), the program exists the cycle
break
x = x1 #The next solution becomes the starting solution in the new cycle
return x #Final solution
def final_solution(): #The final solution from the Newton Raphson method
return newtons_method(f,x)
df=D(f) #These values have to be inserted again to allow the execution of the final step
h=0.000001
epsilon=0.000001
x=newtons_method(f,x)
if abs((x-f(x)/df(x,h))-x) < epsilon: #If (strong) convergence has been reached
print 'Solution is:', final_solution() #Prints the final solution
print 'Approximations before reaching convergence:', approximations #Prints the vector of intermediate solutions
print 'Convergence has been reached after', len(iterations), 'iterations'
print 'Newton Raphson method was successful!'
elif abs((x-f(x)/df(x,h))-x) >= epsilon: #If (strong) convergence has not been reached
print 'Approximated solution is:', final_solution()
print 'Approximations evaluated:', approximations
print 'Convergence has not been reached after', max_n_iterations, 'iterations'
print 'Newton Raphson method was not successful'
#本练习展示了一种立即找到实值函数根的方法,使用逐次更好的近似
#这种方法称为牛顿-拉斐逊法
打印“查找给定函数的根-牛顿-拉斐逊方法”
打印“函数如下:…”
x=输入(“选择初始估计:”)#需要选择初始估计才能开始迭代过程
x=浮点(x)#插入的数字转换为浮点数
max_n_iterations=input('选择max n.iterations:')#用户决定要运行的最大迭代次数
近似值=[]#收集所有中间解的向量;i、 e.在达到最终解之前评估的近似根
迭代次数=[]
def f(x):#必须在代码中手动插入给定函数
返回2*x**3+45*x+1/x**2+16
def D(f):#使用导数的定义计算给定函数的一阶导数
def df(x,h):#x是初始估计值,h是增量
返回(f(x+h)-f(x))/h#差商
返回df#一阶导数
def newtons_方法(f,x,h=0.000001,epsilon=0.000001,):#这是主要过程:f是给定函数,x和h与上面相同,epsilon是公差水平。ε和h必须选择足够小的值
df=D(f)#df和以前一样,只是一阶导数
对于范围内的i(最大迭代次数):#代码运行直到达到最大迭代次数
x1=x-f(x)/df(x,h)#牛顿-拉斐逊法的本质:迭代过程
近似值。追加(x)#如上所述,将每个中间解收集到一个向量中
迭代。追加(1)
如果abs(x1-x)=epsilon:#如果没有达到(强)收敛
打印“近似解为:”,最终解()
打印“估算的近似值:”,近似值
打印“最大迭代次数”和“迭代次数”之后未达到收敛”
打印“牛顿-拉斐逊方法不成功”
在newtons\u method()
返回值之前,它必须再次调用自身。例如:
def newtons_method(f, x, h=0.000001, epsilon=0.000001,):
...
if abs((x - f(x)/df(x, h))-x)< epsilon:
print 'Solution is:', round(newtons_method(f,x),6) # function called again here
...
return x
else:
print 'Approximated solution is:', round(newtons_method(f,x),4) # and again here
...
return x
定义牛顿法(f,x,h=0.000001,ε=0.000001,):
...
如果abs((x-f(x)/df(x,h))-x)newtons\u method()
的第一次调用永远不会返回,因为它必须在return
之前调用自己,然后该函数调用必须在return
之前调用自己,然后
你能修改你的代码使
newtons\u method()
不以这种方式递归调用吗?正如ajcr所说,不要调用牛顿方法从牛顿方法中获得解。f(x)就是你所需要的
其次,当abs(x-x1)
if
条件中的两个连续根:(x-f(x)/df(x,h))-x
。最终的x
应该是您当前对根的猜测吗(以及该表达式中的x
的其他实例是您以前的猜测)?最终的x是最终解(根),而括号中的第一个表达式只是检查收敛条件,以评估该方法是否成功。无论如何,我写了一个更好的代码,可能是m