みその計算物理学
ホーム はじめに リンク集
2次方程式を解の公式を用いて解を求める数値計算例(Visual Basic)
  • 2次方程式

    2次方程式の解は、解の公式を用いて解析的に求められるので、わざわざコンピュータに解かせる必要はないが、プログラミングの入門には最適な課題である。今回は、解の判別式を用いて、複素解まで求めるようにした。

  • フォームデザイン

    まず土台となるフォームのデザインから行う。2次方程式の3つの係数を入力させるために3つのテキストボックスを用意する。計算を始めるためのスタートボタンを用意し、さらに解の表示にもう1つテキストボックスを用意する。なお配置デザインは各自適当でよい。

  • プログラムソース

    プログラムは長いように見えるが、実際に書くのは下のほうにあるButton1のイベントプロシージャ内である。上の方は無視して良い。

    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows フォーム デザイナで生成されたコード "
    
        Public Sub New()
            MyBase.New()
    
            ' この呼び出しは Windows フォーム デザイナで必要です。
            InitializeComponent()
    
            ' InitializeComponent() 呼び出しの後に初期化を追加します。
    
        End Sub
    
        ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        ' Windows フォーム デザイナで必要です。
        Private components As System.ComponentModel.IContainer
    
        ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
        'Windows フォーム デザイナを使って変更してください。  
        ' コード エディタを使って変更しないでください。
        Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
        Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
        Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents Label3 As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.TextBox1 = New System.Windows.Forms.TextBox
            Me.TextBox2 = New System.Windows.Forms.TextBox
            Me.TextBox3 = New System.Windows.Forms.TextBox
            Me.Button1 = New System.Windows.Forms.Button
            Me.TextBox4 = New System.Windows.Forms.TextBox
            Me.Label1 = New System.Windows.Forms.Label
            Me.Label2 = New System.Windows.Forms.Label
            Me.Label3 = New System.Windows.Forms.Label
            Me.SuspendLayout()
            '
            'TextBox1
            '
            Me.TextBox1.Location = New System.Drawing.Point(40, 16)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(232, 19)
            Me.TextBox1.TabIndex = 0
            Me.TextBox1.Text = ""
            '
            'TextBox2
            '
            Me.TextBox2.Location = New System.Drawing.Point(40, 48)
            Me.TextBox2.Name = "TextBox2"
            Me.TextBox2.Size = New System.Drawing.Size(232, 19)
            Me.TextBox2.TabIndex = 1
            Me.TextBox2.Text = ""
            '
            'TextBox3
            '
            Me.TextBox3.Location = New System.Drawing.Point(40, 80)
            Me.TextBox3.Name = "TextBox3"
            Me.TextBox3.Size = New System.Drawing.Size(232, 19)
            Me.TextBox3.TabIndex = 2
            Me.TextBox3.Text = ""
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(16, 128)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(80, 24)
            Me.Button1.TabIndex = 3
            Me.Button1.Text = "計算する"
            '
            'TextBox4
            '
            Me.TextBox4.Location = New System.Drawing.Point(16, 176)
            Me.TextBox4.Multiline = True
            Me.TextBox4.Name = "TextBox4"
            Me.TextBox4.Size = New System.Drawing.Size(256, 80)
            Me.TextBox4.TabIndex = 4
            Me.TextBox4.Text = ""
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(8, 16)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(24, 16)
            Me.Label1.TabIndex = 5
            Me.Label1.Text = "a="
            '
            'Label2
            '
            Me.Label2.Location = New System.Drawing.Point(8, 48)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(24, 16)
            Me.Label2.TabIndex = 6
            Me.Label2.Text = "b="
            '
            'Label3
            '
            Me.Label3.Location = New System.Drawing.Point(8, 80)
            Me.Label3.Name = "Label3"
            Me.Label3.Size = New System.Drawing.Size(24, 16)
            Me.Label3.TabIndex = 7
            Me.Label3.Text = "c="
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
            Me.ClientSize = New System.Drawing.Size(292, 266)
            Me.Controls.Add(Me.Label3)
            Me.Controls.Add(Me.Label2)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.TextBox4)
            Me.Controls.Add(Me.Button1)
            Me.Controls.Add(Me.TextBox3)
            Me.Controls.Add(Me.TextBox2)
            Me.Controls.Add(Me.TextBox1)
            Me.Name = "Form1"
            Me.Text = "2次方程式の解"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'ここから記述
            Dim a, b, c As Double
            a = TextBox1.Text
            b = TextBox2.Text
            c = TextBox3.Text
    
            Dim Re1, Re2, Im1, Im2, D As Double
    
            D = b * b - 4 * a * c '解の判別式
    
            If D > 0 Then
                Re1 = (-b + Math.Sqrt(D)) / (2 * a)
                Re2 = (-b - Math.Sqrt(D)) / (2 * a)
                Im1 = 0
                Im2 = 0
            ElseIf D = 0.0 Then
                Re1 = -b / (2 * a)
                Im1 = 0
                Re2 = Im2 = 0
            Else
                Re1 = -b / (2 * a)
                Re2 = Re1
                Im1 = Math.Sqrt(-D) / (2 * a)
                Im2 = -Im1
            End If
    
            If D = 0 Then
                TextBox4.Text = Re1 & "+i" & Im1
            Else
                TextBox4.Text = Re1 & "+i" & Im1 & vbCrLf & Re2 & "+i" & Im2
            End If
            'ここまで記述
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    
  • プログラム実行結果

    プログラムを実行すると、2次方程式の係数を入力するようにもとめられる。今回は、x*x-5x+6=0 の解を求めることにした。