求教VB黄金分割法求最值,不知道哪里出问题了 大神帮帮忙看看谢谢
程序代码:
Public Function f(x As Long) As Long '定义程序f i = Val(Text2.Text) f = (0.8 - ((1 + Sqr(i * x)) / (0.97 * x * (i + Sqr(i * x)))) ^ (1 / 3) * x) ^ 2 End Function Private Sub Command1_Click() Dim m As Long, n As Long, a As Long, b As Long, fm As Long, c As Long Dim fa As Long, fb As Long, q As Long a = 1# '一级和二级传动比的比值取1~1.7 b = 1.7 q = 0.259 m = 1.2674 n = 1.4326 Do While (q > 0.01) '精度要求为0.01 m = a + 0.382 * (b - a) n = a + 0.618 * (b - a) q = (n - m) / (b - a) If fm >= fn Then '新的搜索区间 a = a b = n ElseIf fm < fn Then '新的搜索区间 a = n b = b End If Loop c = (a + b) / 2 '取中点值 Text1.Text = c End Sub
这是用黄金分割法求解X的值使得函数f(x)的值最小,其中i是变量(i=15~40),i是输入值
这个程序我求处理无论i是多少结果都是X=2,是错误的。
求各位大神帮看看 拜谢了!!!在线等