📄 二分法.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "二分法解方程"
ClientHeight = 3975
ClientLeft = 60
ClientTop = 345
ClientWidth = 7080
BeginProperty Font
Name = "Arial Black"
Size = 21.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form1"
ScaleHeight = 3975
ScaleWidth = 7080
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton CmdRoot
Caption = "开始求根"
Height = 615
Left = 2040
TabIndex = 5
Top = 3240
Width = 3495
End
Begin VB.TextBox Text2
Height = 735
Left = 4800
TabIndex = 3
Text = "2.5"
Top = 840
Width = 1335
End
Begin VB.TextBox Text1
Height = 735
Left = 1560
TabIndex = 1
Text = "2"
Top = 840
Width = 1335
End
Begin VB.Label Label3
Caption = "Sin(x)*2+3*Cos(x/5)-x^2=0"
Height = 615
Left = 240
TabIndex = 6
Top = 120
Width = 6495
End
Begin VB.Label lblRoot
Height = 495
Left = 480
TabIndex = 4
Top = 2280
Width = 6375
End
Begin VB.Label Label2
Caption = "X2:"
Height = 495
Left = 3600
TabIndex = 2
Top = 840
Width = 975
End
Begin VB.Label Label1
Caption = "X1:"
Height = 495
Left = 360
TabIndex = 0
Top = 840
Width = 975
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'二分法解方程,编制:曹新国,2004-3-14
'最后修改于 2006-4-5
Private Sub cmdRoot_Click()
'二分法求f(x)=Sin(x)*2+3*Cos(x/5)-x^2在区间[2,2.5]内的一个实根
Dim x1 As Double, x2 As Double, x As Double
x1 = Val(Text1) ' InputBox("输入区间x1的值", "输入框 ")
x2 = Val(Text2) ' InputBox("输入区间x2的值", "输入框")
x = x1
f1 = Sin(x) * 2 + 3 * Cos(x / 5) - x ^ 2
x = x2
f2 = Sin(x) * 2 + 3 * Cos(x / 5) - x ^ 2
If f1 * f2 < 0 Then
Do
x = (x1 + x2) / 2
f = Sin(x) * 2 + 3 * Cos(x / 5) - x ^ 2
If (f * f1) >= 0 Then
x1 = x
f1 = f
End If
If (f * f1) <= 0 Then
x2 = x
f2 = f
End If
Loop While Abs(x1 - x2) > 1E-20
x = (x1 + x2) / 2
lblRoot = "x=" & Format(x)
Else
MsgBox "方程在此区间内无根,请重新输入区间"
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -