猿问

如何判断牛顿法是否失败

我正在为一个无约束优化问题创建一个基本的牛顿法算法,我的算法结果不是我所期望的。这是一个简单的目标函数,因此很明显该算法应该收敛于 (1,1)。我之前创建的梯度下降算法证实了这一点,这里:


def grad_descent(x, t, count, magnitude):

    xvalues.append(x)

    gradvalues.append(np.array([dfx1(x), dfx2(x)]))

    fvalues.append(f(x))   

    temp=x-t*dfx(x)

    x = temp

    magnitude = mag(dfx(x))    

    count+=1


    return xvalues, gradvalues, fvalues, count

我为牛顿法创建算法的尝试在这里:


def newton(x, t, count, magnitude):

  xvalues=[]

  gradvalues=[]

  fvalues=[]

  temp=x-f(x)/dfx(x)


  while count < 10:

    xvalues.append(x)

    gradvalues.append(dfx(x))

    fvalues.append(f(x))  


    temp=x-t*f(x)/dfx(x)

    x = temp

    magnitude = mag(dfx(x))    

    count+=1

    if count > 100:

      break

  return xvalues, gradvalues, fvalues, count

这是目标函数和梯度函数:


f = lambda x: 100*np.square(x[1]-np.square(x[0])) + np.square((1-x[0]))

dfx = lambda x: np.array([-400*x[0]*x[1]+400*np.power(x[0],3)+2*x[0]-2, 200*(x[1]-np.square(x[0]))])

这里是初始条件。请注意,在牛顿法中不使用 alpha 和 beta。


x0, t0, alpha, beta, count = np.array([-1.1, 1.1]), 1, .15, .7, 1

magnitude = mag(np.array([dfx1(x0), dfx2(x0)]))

调用函数:


xvalues, gradvalues, fvalues, iterations = newton(x0, t0, count, magnitude)


这是因为初始猜测与最佳点非常接近,还是我的算法中存在一些我没有发现的错误?任何建议将不胜感激。看起来解甚至可能是振荡的,但很难说


侃侃无极
浏览 143回答 3
3回答

慕的地10843

我现在确定 python 代码有问题。我决定在 Matlab 中实现该算法,它似乎工作正常。这是代码:clear; clc;x=[-1.1, 1.1]';t=1;count=1;xvalues=[];temp = x - inv([(-400*x(2)+1200*x(1)^2+2), -400*x(1); -400*x(1), 200]);disp(x-inv([(-400*x(2)+1200*x(1)^2+2), -400*x(1); -400*x(1), 200])*[-400*x(1)*x(2)+400*x(1)^3+2*x(1)-2; 200*(x(2)-x(1)^2)])while count<10&nbsp; &nbsp; xvalues(count,:)= x;&nbsp; &nbsp; temp = x - inv([(-400*x(2)+1200*x(1)^2+2), -400*x(1); -400*x(1), 200]) * [-400*x(1)*x(2)+400*x(1)^3+2*x(1)-2; 200*(x(2)-x(1)^2)];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; x = temp;&nbsp; &nbsp; count = count+1;enddisp(xvalues)输出:-1.1000&nbsp; &nbsp; 1.1000&nbsp; &nbsp;-1.0087&nbsp; &nbsp; 1.0091&nbsp; &nbsp;-0.2556&nbsp; &nbsp;-0.5018&nbsp; &nbsp;-0.2446&nbsp; &nbsp; 0.0597&nbsp; &nbsp; 0.9707&nbsp; &nbsp;-0.5348&nbsp; &nbsp; 0.9708&nbsp; &nbsp; 0.9425&nbsp; &nbsp; 1.0000&nbsp; &nbsp; 0.9991&nbsp; &nbsp; 1.0000&nbsp; &nbsp; 1.0000&nbsp; &nbsp; 1.0000&nbsp; &nbsp; 1.0000
随时随地看视频慕课网APP

相关分类

Python
我要回答