本文主要是介绍用梯度法程序求极小值点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用梯度法程序求极小值点
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import math
from mpl_toolkits.mplot3d import Axes3D
import warningsdef hyj():x1 = 0x2 = 3alpha = 0.001iter_num = 1def f2(x1, x2):return (x1-2)**4 + (x1-2*x2)**2def hx1(x1, x2):return 4*(x1-2)**3+2*x1-4*x2def hx2(x, y):return 4*x2GD_X1 = [x1]GD_X2 = [x2]GD_Y = [f2(x1,x2)]y_change = f2(x1,x2)while(y_change > 1e-10 and iter_num < 100) :tmp_x1 = x1 - alpha * hx1(x1,x2)tmp_x2 = x2 - alpha * hx2(x1,x2)tmp_y = f2(tmp_x1,tmp_x2)f_change = np.absolute(tmp_y - f2(x1,x2))x1 = tmp_x1x2 = tmp_x2GD_X1.append(x1)GD_X2.append(x2)GD_Y.append(tmp_y)iter_num += 1print("极值点" ,(x1, x2))print("极值" ,f2(x1,x2))print(GD_X1)print(GD_X2)
hyj()
这篇关于用梯度法程序求极小值点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!