如果引用或轉貼,麻煩註明出處與本網誌連結,否則視為侵權。

2014年7月6日

Visual Basic解數學之三 : 使用克拉馬規則(Cramer's Rule)解二元一次方程式

作者: Fred Wang (FW知識瑣記) 日期:2014/7/6

適用版本 : VB 2010~2013
 



功能
1. 二元一次方程式 aX + bY = c, dX + eY = f, 輸入a, b, c, d, e, f求X,Y的值
2. 以克拉馬規則(Cramer's Rule)
    X = ( c * e - b * f ) / ( a * e - b * d )
    Y = ( a * f - c * d ) / ( a * e - b * d ) 
3. 但是  a * e - b * d = 0 時, a * f  - c * d = 0 時無限多解, a * f  - c * d <> 0 時無解



Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBoxA.Focus()
    End Sub

    Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
        End
    End Sub

    Private Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.Click
        TextBoxA.Text = ""
        TextBoxB.Text = ""
        TextBoxC.Text = ""
        TextBoxD.Text = ""
        TextBoxE.Text = ""
        TextBoxF.Text = ""
        TextBoxA.Focus()
    End Sub

    Private Sub ButtonRun_Click(sender As Object, e As EventArgs) Handles ButtonRun.Click
        Dim vA, vB, vC, vD, vE, vF, vX, vY As Integer
        If IsNumeric(TextBoxA.Text) And IsNumeric(TextBoxB.Text) And IsNumeric(TextBoxC.Text) And IsNumeric(TextBoxD.Text) And IsNumeric(TextBoxE.Text) And IsNumeric(TextBoxF.Text) Then
            vA = Val(TextBoxA.Text)
            vB = Val(TextBoxB.Text)
            vC = Val(TextBoxC.Text)
            vD = Val(TextBoxD.Text)
            vE = Val(TextBoxE.Text)
            vF = Val(TextBoxF.Text)
            If vA * vE - vB * vD = 0 Then
                If vA * vF - vC * vD = 0 Then
                    TextBoxResult.Text = "無限多解"
                Else
                    TextBoxResult.Text = "無解"
                End If
            Else
                vX = (vC * vE - vB * vF) / (vA * vE - vB * vD)
                vY = (vA * vF - vC * vD) / (vA * vE - vB * vD)
                TextBoxResult.Text = "X = " & CStr(vX) & ", Y = " & CStr(vY)
            End If

        Else
            MsgBox("只能輸入數字", vbOKOnly)
        End If

    End Sub
End Class



上一篇 - Visual Basic解數學之二 : 使用牛頓拉夫森法(Newton-Raphson Method)求平方根近似值

Visual Basic解數學之二 : 使用牛頓拉夫森法(Newton-Raphson Method)求平方根近似值

作者: Fred Wang (FW知識瑣記) 日期: 2014/7/6

適用版本 : VB 2010~2013


牛頓拉夫森法(Newton-Raphson Method)又稱牛頓逼近法
功能
1. 輸入N值, 然後使用牛頓拉夫森法(Newton-Raphson Method)計算平方根近似值
2. Xn值為前一次Xn-1值與 N 除Xn-1值的平均
3. 如果Xn與前一次Xn-1值小數第三位以前相同時,則停止計算
4. 將過程的Xn值顯示出來(四捨五入到小數第三位)


Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Xn As Long
        Dim X0 As Long
        Dim N As Long
        Dim i As Integer
        If Not IsNumeric(TextBoxN.Text) Then
            MsgBox("請輸入數字", vbOKOnly)
        ElseIf Val(TextBoxN.Text) < 0 Or Val(TextBoxN.Text) = 0 Then
            MsgBox("值不可以為零或負值", vbOKOnly)
        Else
            N = Val(TextBoxN.Text)
            Xn = N / 3
            i = 0
            Do While True
                Me.DataGridView1.Rows.Add(CStr(i), Xn)
                i = i + 1
                X0 = Xn
                Xn = (X0 + N / X0) / 2
                If Int(Xn * 1000) / 1000 = Int(X0 * 1000) / 1000 Then Exit Do
            Loop

            Xn = Xn + 0.005
            Xn = Int(Xn * 100) / 100
            TextBoxResult.Text = CStr(Xn)

        End If
    End Sub
End Class


上一篇 - Visual Basic解數學之一 : 華式溫度與攝氏溫度的互換 下一篇 - Visual Basic解數學之三 : 使用克拉碼規則(Cramer's Rule)解二元一次方程式

Visual Basic解數學之一 : 華式溫度與攝氏溫度的互換

作者: Fred Wang (FW知識瑣記) 日期: 20140706

適用版本: VB2010~2013

功能
1. 選擇不同功能(轉換方式), Form的標題與 Label1, Label2的文字會跟著改變
    例如選擇"攝氏轉華氏"標題會變成"攝氏溫度轉華氏溫度", Label1的文字會變成
"攝氏溫度", Label2的文字會變成"華氏溫度"
2.按"執行"按鈕(ButtonRun)後,會檢查Textbox1是否輸入的是數值, 並執行計算,在Textbox2中顯示結果
3.按"離開"按鈕(ButtonQuit)後,會結束程式 

程式碼參考如下

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Focus()
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
        Me.Text = "攝氏溫度轉華氏溫度"
        Label1.Text = "攝氏溫度"
        Label2.Text = "華氏溫度"
    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        Me.Text = "華氏溫度轉攝氏溫度"
        Label1.Text = "華氏溫度"
        Label2.Text = "攝氏溫度"
    End Sub

    Private Sub ButtonQuit_Click(sender As Object, e As EventArgs) Handles ButtonQuit.Click
        End
    End Sub

    Private Sub ButtonRun_Click(sender As Object, e As EventArgs) Handles ButtonRun.Click
        If Not IsNumeric(TextBox1.Text) Then
            MsgBox("請輸入數字", MsgBoxStyle.OkOnly)
        Else
            If RadioButton1.Checked Then
                TextBox2.Text = CStr((5 / 9) * (Val(TextBox1.Text) - 32))
            Else
                TextBox2.Text = CStr((9 / 5) * Val(TextBox1.Text) + 32)
            End If
        End If
    End Sub
End Class


下一篇 - Visual Basic解數學之二 : 使用牛頓拉夫森法(Newton-Raphson Method)求平方根近似值