⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 最速下降法.frm

📁 最速下降法求极值的vb源程序
💻 FRM
📖 第 1 页 / 共 2 页
字号:
      Left            =   2280
      Locked          =   -1  'True
      TabIndex        =   9
      Text            =   "3*x1*x1+2*x1*x2+x2*x2+2*x1-2*x2+1"
      Top             =   240
      Width           =   4815
   End
   Begin VB.CommandButton Command2 
      Cancel          =   -1  'True
      Caption         =   "退出"
      Height          =   495
      Left            =   6480
      TabIndex        =   7
      Top             =   6000
      Width           =   1215
   End
   Begin VB.CommandButton Command1 
      Caption         =   "运行"
      Height          =   495
      Left            =   6480
      TabIndex        =   6
      Top             =   5160
      Width           =   1215
   End
   Begin VB.TextBox Text3 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   1920
      TabIndex        =   5
      Text            =   "0.01"
      Top             =   3840
      Width           =   1575
   End
   Begin VB.TextBox Text2 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   5640
      TabIndex        =   4
      Text            =   "0"
      Top             =   3120
      Width           =   1575
   End
   Begin VB.TextBox Text1 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   1920
      TabIndex        =   3
      Text            =   "0"
      Top             =   3120
      Width           =   1575
   End
   Begin MSScriptControlCtl.ScriptControl ScriptControl1 
      Left            =   7320
      Top             =   1200
      _ExtentX        =   1005
      _ExtentY        =   1005
   End
   Begin VB.Label Label8 
      Alignment       =   2  'Center
      BackColor       =   &H00FF8080&
      Caption         =   "计算次数"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   300
      Left            =   360
      TabIndex        =   16
      Top             =   6480
      Width           =   1290
   End
   Begin VB.Label Label7 
      Alignment       =   2  'Center
      BackColor       =   &H00FF8080&
      Caption         =   "输出:"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   300
      Left            =   360
      TabIndex        =   12
      Top             =   4680
      Width           =   1290
   End
   Begin VB.Line Line1 
      BorderWidth     =   2
      X1              =   0
      X2              =   9000
      Y1              =   4560
      Y2              =   4560
   End
   Begin VB.Label Label6 
      Alignment       =   2  'Center
      BackColor       =   &H00FF8080&
      Caption         =   "min f(x1,x2)"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   300
      Left            =   360
      TabIndex        =   11
      Top             =   5880
      Width           =   1770
   End
   Begin VB.Label Label5 
      Alignment       =   2  'Center
      BackColor       =   &H00FF8080&
      Caption         =   "(x1,x2)"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   300
      Left            =   360
      TabIndex        =   10
      Top             =   5280
      Width           =   1290
   End
   Begin VB.Label Label4 
      Alignment       =   2  'Center
      BackColor       =   &H00FF8080&
      Caption         =   "min f(x1,x2)"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   240
      TabIndex        =   8
      Top             =   240
      Width           =   1815
   End
   Begin VB.Label Label3 
      Alignment       =   2  'Center
      BackColor       =   &H00FF8080&
      Caption         =   "精度(e)"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   300
      Left            =   360
      TabIndex        =   2
      Top             =   3960
      Width           =   1335
   End
   Begin VB.Label Label2 
      Alignment       =   2  'Center
      BackColor       =   &H00FF8080&
      Caption         =   "初始点(x2)"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   300
      Left            =   4080
      TabIndex        =   1
      Top             =   3240
      Width           =   1290
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      BackColor       =   &H00FF8080&
      Caption         =   "初始点(x1)"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   300
      Left            =   240
      TabIndex        =   0
      Top             =   3240
      Width           =   1290
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'最速下降法求极值
'研自0816  胡兴武

Private Sub Command1_Click()
'定义h1,h2,h3,h4为海赛阵的按行拉直
'd1,d2为函数f(x)对x1,x2的偏导
'x1,x2为初始点
'r最佳步长

Dim x1, x2, e, r, f1, f2, h1, h2, h3, h4 As Single
Dim Count As Integer
Dim d1, d2 As String

e = ScriptControl1.Eval(Text3.Text)     '精度要求

h1 = ScriptControl1.Eval(Text10.Text)
h2 = ScriptControl1.Eval(Text11.Text)
h3 = ScriptControl1.Eval(Text12.Text)
h4 = ScriptControl1.Eval(Text13.Text)

d1 = Text8.Text
d2 = Text9.Text
'计算初始点
f1 = ScriptControl1.Eval(Replace(Replace(d1, "x1", Text1.Text), "x2", Text2.Text))
f2 = ScriptControl1.Eval(Replace(Replace(d2, "x1", Text1.Text), "x2", Text2.Text))

While (Sqr(f1 * f1 + f2 * f2) > e)            '比较两边界的差值是否小于精度要求
'迭代计算
   r = (f1 * f1 + f2 * f2) / (h1 * f1 * f1 + h3 * f1 * f2 + h2 * f1 * f2 + h4 * f2 * f2) '计算最佳步长
  '确定下一个近似点
   x1 = x1 - r * f1
   x2 = x2 - r * f2
   
   f1 = ScriptControl1.Eval(Replace(Replace(d1, "x1", CStr(x1)), "x2", CStr(x2)))
   f2 = ScriptControl1.Eval(Replace(Replace(d2, "x1", CStr(x1)), "x2", CStr(x2)))
   Count = Count + 1
Wend
 
'输出显示
    Text5.Text = "(" & Format(x1, "0.000000") & "," & Format(x2, "0.000000") & ")"
    Text6.Text = Format(ScriptControl1.Eval(Replace(Replace(Text4.Text, "x1", CStr(x1)), "x2", CStr(x2))), "0.000000")
    Text7.Text = Str(Count)
End Sub

Private Sub Command2_Click()
End
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -