Option Explicit On Option Strict On Public Class Form1 Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click Dim ndata As Integer = 7 Dim datax() As Double = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0} Dim datay() As Double = {3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0} Dim aa As Double Dim bb As Double Dim rr As Double Call KAIKI(ndata, datax, datay, aa, bb, rr) ListBox1.Items.Add("y=a*x+b") ListBox1.Items.Add("a=" & aa.ToString("0.000")) ListBox1.Items.Add("b=" & bb.ToString("0.000")) ListBox1.Items.Add("r=" & rr.ToString("0.000")) End Sub Private Sub KAIKI(ByVal ndata As Integer, ByRef datax() As Double, ByRef datay() As Double, _ ByRef aa As Double, ByRef bb As Double, ByRef rr As Double) Dim i As Integer Dim x1 As Double : Dim y1 As Double : Dim x2 As Double : Dim xy As Double Dim xm As Double : Dim ym As Double Dim c1 As Double : Dim c2 As Double : Dim c3 As Double '回帰式:y=aa*x+bb x1 = 0.0 : y1 = 0.0 : x2 = 0.0 : xy = 0.0 For i = 0 To ndata x1 = x1 + datax(i) y1 = y1 + datay(i) x2 = x2 + datax(i) * datax(i) xy = xy + datax(i) * datay(i) Next i xm = x1 / CDbl(ndata + 1) : ym = y1 / CDbl(ndata + 1) aa = (CDbl(ndata + 1) * xy - x1 * y1) / (CDbl(ndata + 1) * x2 - x1 * x1) bb = (x2 * y1 - x1 * xy) / (CDbl(ndata + 1) * x2 - x1 * x1) c1 = 0.0 : c2 = 0.0 : c3 = 0.0 For i = 0 To ndata c1 = c1 + (datax(i) - xm) * (datay(i) - ym) c2 = c2 + (datax(i) - xm) * (datax(i) - xm) c3 = c3 + (datay(i) - ym) * (datay(i) - ym) Next i rr = c1 / Math.Sqrt(c2 * c3) End Sub End Class