Python 用牛顿法计算程序的收敛速度

Python 用牛顿法计算程序的收敛速度,python,return,convergence,Python,Return,Convergence,我的任务是编写一个程序来计算方法的收敛速度。我不得不用牛顿法对根进行近似。代码的这一部分很好用,但我会把它包括进去 x0 : start value F: function DF: jacobi matrix tol : tolerance rate of the approximation. If it is reached the loop shall be stopped --> that`s why I calculate with count maxit: maximum

我的任务是编写一个程序来计算方法的收敛速度。我不得不用牛顿法对根进行近似。代码的这一部分很好用,但我会把它包括进去

x0 : start value

F: function

DF: jacobi matrix

tol : tolerance rate of the approximation. If it is reached the loop shall be stopped --> that`s why I calculate with count

maxit: maximum iterations
重要的是,我试着对任何n维空间都这样做

def konv(x0, F, FD, tol, maxit):

#set counter of the iterations to zero and define an array for the values of x in the iteration 

    count = 0
    x = np.zeros([np.shape(x0)[0], maxit])

    x[:,0] = x0

#fill the array with the values given by the formula x_k+1 = x_k - ((DF(x_k))^(-1)*F(x_k))
#((DF(x_k))^(-1)*F(x_k)) = s

    for i in range(maxit):
        count = 1+i

        s =  np.linalg.solve(DF(x[..., i]), F(x[..., i]))
        x[..., i+1] = x[..., i] - s

        if np.all((np.linalg.norm(x[..., i+1]-x[..., i]) < tol*np.linalg.norm(x[..., i]))):
            break
#define an array which stores the errors
    e = np.zeros(count)

    for i in range(count):
        e[i] = np.linalg.norm(x[..., i] - x[..., count])

#return the rate of convergence

    return lambda e : np.log(e[2:]/e[1:-1]/np.log(e[1:-1])/e[:-2])
我得到的结果是:

[ 0.39384945  0.03214274] 6
<function konv.<locals>.<lambda> at 0x0000023312A82268>
[0.39384945 0.03214274]6

这是什么意思?它不是应该返回一个数字吗?为什么我的返回值中有字母?

使用lambda函数返回的是函数而不是数字

只需返回:

return np.log(e[2:]/e[1:-1]/np.log(e[1:-1])/e[:-2])
return np.log(e[2:]/e[1:-1]/np.log(e[1:-1])/e[:-2])