1、无约束多维优化牛顿法 vb 编程Vb 编程运行后的界面Vb 程序Private Sub Command1_Click()Dim m1 As DoubleDim m2 As DoubleDim m3 As DoubleDim m4 As DoubleDim m5 As DoubleDim m6 As DoubleDim xx As DoubleDim fxx As DoubleDim yp1 As DoubleDim yp2 As DoubleDim x1 As DoubleDim x2 As DoubleDim As DoubleDim 1 As DoubleDim h As DoubleDi
2、m p0 As DoubleDim p1 As DoubleDim p2 As DoubleDim p3 As DoubleDim yp3 As DoubleDim tidu(1, 0) As DoubleDim haise(1, 1) As DoubleDim haiseni(1, 1) As DoubleDim s1 As DoubleDim s2 As DoubleDim xx1 As DoubleDim xx2 As DoubleDim xx3 As Doublem1 = Val(InputBox(“请输入 x12 的系数 m1“, “输入框“, 0)m2 = Val(InputBox
3、(“请输入 x22 的系数 m2“, “输入框“, 0)m3 = Val(InputBox(“请输入 x1*x2 的系数 m3“, “输入框“, 0)m4 = Val(InputBox(“请输入 x1 的系数 m4“, “输入框“, 0)m5 = Val(InputBox(“请输入 x2 的系数 m5“, “输入框“, 0)m6 = Val(InputBox(“请输入常数项 m6“, “输入框“, 0)x1 = Val(InputBox(“请输入迭代初始点 x1“, “输入框“, 0)x2 = Val(InputBox(“请输入迭代初始点 x2“, “输入框“, 0) = Val(InputB
4、ox(“ 请输入迭代收敛精度 “, “输入框“, 0)1 = Val(InputBox(“ 请输入一维搜索的收敛精度 1“, “输入框“, 0)h = Val(InputBox(“请输入一维搜索的初始步长 h“, “输入框“, 0)p0 = Val(InputBox(“请输入一维搜索的初始点 p0“, “输入框“, 0)Label1.Caption = “ 目标函数:f(X)= “ & m1 & “ x12 + “ & m2 & “ x22 + “ & m3 & “ x1x2 + “ & m4 & “ x1 + “ & m
5、5 & “ x2 + “ & m6Label2.Caption = “ 迭代初始点 x1= “ & x1 & “ 和 x2= “ & x2 & vbLf & “ 迭代收敛精度= “ & Label3.Caption = “ 一维搜索的初始点 p0= “ & p0 & vbLf & “ 一维搜索的初始步长 h= “ & h & vbLf & “ 一维搜索的收敛精度 1= “ & 1tidu(0, 0) = 2 * m1 * x1 + m3 * x2 + m4tidu(1, 0)
6、 = 2 * m2 * x2 + m3 * x1 + m5haise(0, 0) = 2 * m1haise(0, 1) = m3haise(1, 0) = m3haise(1, 1) = 2 * m2haiseni(0, 0) = 2 * m2 / (4 * m1 * m2 - m3 2)haiseni(0, 1) = -m3 / (4 * m1 * m2 - m3 2)haiseni(1, 0) = -m3 / (4 * m1 * m2 - m3 2)haiseni(1, 1) = 2 * m1 / (4 * m1 * m2 - m3 2)Do While (tidu(0, 0) 2 +
7、(tidu(1, 0) 2 s1 = -(haiseni(0, 0) * tidu(0, 0) + haiseni(0, 1) * tidu(1, 0)s2 = -(haiseni(1, 0) * tidu(0, 0) + haiseni(1, 1) * tidu(1, 0)p1 = p0xx1 = x1 + p1 * s1: xx2 = x2 + p1 * s2yp1 = m1 * xx1 2 + m2 * xx2 2 + m3 * xx1 * xx2 + m4 * xx1 + m5 * xx2 + m6p2 = p0 + hxx1 = x1 + p2 * s1: xx2 = x2 + p2
8、 * s2yp2 = m1 * xx1 2 + m2 * xx2 2 + m3 * xx1 * xx2 + m4 * xx1 + m5 * xx2 + m6If yp1 yp2 Thenh = 2 * hElseh = -0.25 * hp3 = p1: yp3 = yp1p1 = p2: yp1 = yp2p2 = p3: yp2 = yp3End Ifp3 = p0 + hxx1 = x1 + p3 * s1: xx2 = x2 + p3 * s2yp3 = m1 * xx1 2 + m2 * xx2 2 + m3 * xx1 * xx2 + m4 * xx1 + m5 * xx2 + m
9、6Do While yp2 = yp3h = 2 * hp1 = p2: yp1 = yp2p2 = p3: yp2 = yp3p3 = p0 + hxx1 = x1 + p3 * s1: xx2 = x2 + p3 * s2yp3 = m1 * xx1 2 + m2 * xx2 2 + m3 * xx1 * xx2 + m4 * xx1 + m5 * xx2 + m6LoopIf h 0 Thena = p1: ypa = yp1b = p3: ypb = yp3Elsea = p3: ypa = yp3b = p1: ypb = yp1End Ifp1 = a + 0.382 * (b -
10、 a)p2 = a + 0.618 * (b - a)xx1 = x1 + p1 * s1: xx2 = x2 + p1 * s2yp1 = m1 * xx1 2 + m2 * xx2 2 + m3 * xx1 * xx2 + m4 * xx1 + m5 * xx2 + m6xx1 = x1 + p2 * s1: xx2 = x2 + p2 * s2yp2 = m1 * xx1 2 + m2 * xx2 2 + m3 * xx1 * xx2 + m4 * xx1 + m5 * xx2 + m6Do While Abs(b - a) 1If yp1 yp2 Thenn0 = 0b = p2p2
11、= p1: yp2 = yp1Elsen0 = 1a = p1p1 = p2: yp1 = yp2End IfIf n0 = 0 Thenp1 = a + 0.382 * (b - a)xx1 = x1 + p1 * s1: xx2 = x2 + p1 * s2yp1 = m1 * xx1 2 + m2 * xx2 2 + m3 * xx1 * xx2 + m4 * xx1 + m5 * xx2 + m6Elsep2 = a + 0.618 * (b - a)xx1 = x1 + p2 * s1: xx2 = x2 + p2 * s2yp2 = m1 * xx1 2 + m2 * xx2 2
12、+ m3 * xx1 * xx2 + m4 * xx1 + m5 * xx2 + m6End IfLoopxx = (a + b) / 2x1 = x1 + xx * s1x2 = x2 + xx * s2fxx = m1 * x1 2 + m2 * x2 2 + m3 * x1 * x2 + m4 * x1 + m5 * x2 + m6tidu(0, 0) = 2 * m1 * x1 + m3 * x2 + m4tidu(1, 0) = 2 * m2 * x2 + m3 * x1 + m5LoopLabel4.Caption = “ 最优解 x1= “ & x1 & “ 和 x2= “ & x2 & vbLf & “ 最优值 f(x)= “ & fxxEnd SubPrivate Sub Command2_Click()Unload MeEnd Sub