using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace vcsKAIKI { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //--------------------------------------------------------------------------- private void toolStripButton1_Click(object sender, EventArgs e) { int ndata = 7; double[] datax= {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}; double[] datay= {3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0}; double aa,bb,rr; KAIKI(ndata, datax, datay, out aa, out bb, out 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")); } //--------------------------------------------------------------------------- private void KAIKI(int ndata, double[] datax, double[] datay, out double aa, out double bb, out double rr) { int i; double x1,y1,x2,xy,xm,ym,c1,c2,c3; //回帰式:y=aa*x+bb x1 = 0.0 ; y1 = 0.0 ; x2 = 0.0 ; xy = 0.0; for(i=0;i<=ndata;i++){ x1 = x1 + datax[i]; y1 = y1 + datay[i]; x2 = x2 + datax[i] * datax[i]; xy = xy + datax[i] * datay[i]; } xm = x1 / (double)(ndata + 1) ; ym = y1 / (double)(ndata + 1); aa = ((double)(ndata + 1) * xy - x1 * y1) / ((double)(ndata + 1) * x2 - x1 * x1); bb = (x2 * y1 - x1 * xy) / ((double)(ndata + 1) * x2 - x1 * x1); c1 = 0.0 ; c2 = 0.0 ; c3 = 0.0; for(i=0;i<=ndata;i++){ c1 = c1 + (datax[i] - xm) * (datay[i] - ym); c2 = c2 + (datax[i] - xm) * (datax[i] - xm); c3 = c3 + (datay[i] - ym) * (datay[i] - ym); } rr = c1 / Math.Sqrt(c2 * c3); } //--------------------------------------------------------------------------- } }