VB6.0 二分法解方程

Private Sub Command1_Click()
    Dim H, x1, x2, m As Double
    x1 = Val(Text1.Text)
    x2 = Val(Text2.Text)
    If f(x1) * f(x2) < 0 Then
        Do
            m = (x1 + x2) / 2
            If f(m) = 0 Then
                x1 = m
                x2 = m
            End If
            If f(x1) * f(m) > 0 Then
                x1 = m
            Else
                x2 = m
            End If
        Loop Until Abs(x1 - x2) < 0.005
        Text3.Text = Text3.Text & vbCrLf & "此方程的根为:x=" + CStr((x1 + x2) / 2)
    Else
        Text3.Text = Text3.Text & vbCrLf & "区间错误!"
    End If
End Sub

Function f(ByVal x As Double) As Double
    f = 6 * x ^ 3 + 2 * x ^ 2 - 5 * x + 1
End Function

 

原文地址:https://www.cnblogs.com/blackice/p/2851435.html