ps:這是按照自己的理解寫的程序,代碼比較糙。獻醜了,望海涵!
#Gradient descent method
'''
min f(x) = 2x1^2+x2^2
A = [[4,0],[0,2]]
'''
#x is col vector
import numpy as np
def func(x,A):
return 0.5*(x.T@A@x)
def func_der(x,A):
return A@x
def gradient_descent(x0,A):
eps = 1e-1
step = 0.3
flag = True
while(flag):
g = func_der(x0,A)
d = -g
if np.linalg.norm(d)<eps:
flag = False
x = x0 + step*d
x0 = x.copy()
print('x0 = ',x0)
print('The optimal value is {}'.format(x0))
A = np.array([[4,0],[0,2]])
x0 = np.array([[2],[2]])
gradient_descent(x0,A)