用python程序求y=(x-1)^2+10中y的最小值:
x = 5
y = (x-1)**2+10
while(1):
dx = 2*(x-1)
y0 = (x-1)**2+10
x = x-0.001*dx
y = (x-1)**2+10
if y0-y <= 0:
break
print(x)
结果:1.000000645429079
梯度公式:
阿尔法是learninggreate
同理:求z=(x-1)^2 +(y-3)^2+3的最小值
x = 5
y = 5
z = (x-1)**2+(y-3)**2+3
while(1):
dx = 2 * (x - 1)
dy = 2 * (y - 3)
z0 = (x-1)**2+(y-3)**2+3
x = x-0.001*dx
y = y-0.001*dy
z = (x-1)**2+(y-3)**2+3
if z0-z <= 0:
break
print(x,y,z)
结果:1.0000002880425625 3.0000001440212825 3.000000000000104