InitializeComponent();
}
public Form1 form = new Form1();
public void output(double[] col1, double[] col2, double[] col3, double[] col4, double[] col5, double[] col6, double[] col7,
double s_col1, double s_col2, double s_col3, double s_col4, double s_col6, double s_col7, double R)
{
StreamReader filen = new StreamReader("n.txt");
int number = Convert.ToInt32(filen.ReadLine());
richTextBox1.Text = "";
for (int i = 0; i < number; i++)
{
dataGridView1.Rows[i].Cells[0].Value = Math.Round(col1[i], 3);
dataGridView1.Rows[i].Cells[1].Value = Math.Round(col2[i], 3);
dataGridView1.Rows[i].Cells[2].Value = Math.Round(col3[i], 3);
dataGridView1.Rows[i].Cells[3].Value = Math.Round(col4[i], 3);
dataGridView1.Rows[i].Cells[4].Value = Math.Round(col5[i], 3);
dataGridView1.Rows[i].Cells[5].Value = Math.Round(col6[i], 3);
dataGridView1.Rows[i].Cells[6].Value = Math.Round(col7[i], 3);
}
richTextBox1.AppendText("Коефіцієнт детермінації = " + Math.Round(R, 6));
dataGridView1.Rows[number + 1].Cells[0].Value = Math.Round(s_col1, 3);
dataGridView1.Rows[number + 1].Cells[1].Value = Math.Round(s_col2, 3);
dataGridView1.Rows[number + 1].Cells[2].Value = Math.Round(s_col3, 3);
dataGridView1.Rows[number + 1].Cells[3].Value = Math.Round(s_col4, 3);
dataGridView1.Rows[number + 1].Cells[5].Value = Math.Round(s_col6, 3);
dataGridView1.Rows[number + 1].Cells[6].Value = Math.Round(s_col7, 3);
output_2(R);
}
public void output_2(double R)
{
double d;
d = Convert.ToDouble(dataGridView3.Rows[6].Cells[4].Value);
if (R == Convert.ToDouble(dataGridView3.Rows[5].Cells[4].Value))
{
richTextBox1.AppendText("\nДана форма пояснює " + Math.Round(R, 6) + " варіації, що є найкращим показником, і таким найкраще чином підходить для апроксимації введених даних");
}
else
{
richTextBox1.AppendText("\nЛінійна модель пояснює " + Math.Round(R, 6) + " варіації, що означає неправильність вибору форми функціонального зв'язку.");
richTextBox1.AppendText("\nНайкраще підходить для апроксимації введених даних функція №" + d);
}
}
public void max(double[] x, double[] y)
{
function1(x, y);
function2(x, y);
function3(x, y);
function4(x, y);
function5(x, y);
double[] maxR = new double[5];
double max = 0;
int n = 0;
for (int i = 0; i < 5; i++)
{
maxR[i] = Convert.ToDouble(dataGridView3.Rows[i].Cells[4].Value);
if (maxR[i] >= max)
{
max = maxR[i];
n = i + 1;
}
}
dataGridView3.Rows[5].Cells[4].Value = max;
dataGridView3.Rows[6].Cells[4].Value = n;
}
public void function1(double[] x, double[] y)
{
StreamReader filen = new StreamReader("n.txt");
int number = Convert.ToInt32(filen.ReadLine());
StreamWriter fileyp = new StreamWriter("yp.txt");
double[] xw = new double[number];
double[] x2 = new double[number];
double[] xwy = new double[number];
double[] yp = new double[number];
double[] yp_y = new double[number];
double[] y_yn = new double[number];
double sy = 0, sx = 0, sxw = 0, sx2 = 0, sxwy = 0, syp_y = 0, sy_yn = 0, a0, a1, y_=0, R;
for (int i = 0; i < number; i++)
{
sx += x[i];
sy += y[i];
xw[i] = 1/x[i];
sxw += xw[i];
x2[i] = Math.Pow(xw[i], 2);
sx2 += x2[i];
xwy[i] = xw[i] * y[i];
sxwy += xwy[i];
}
y_ = sy/number;
a1 = ((number * sxwy) - (sy * sxw)) / ((number * sx2) - Math.Pow(sxw, 2));
a0 = (sy - (a1 * sxw)) / number;
for (int i = 0; i < number; i++)
{
yp[i] = a0 + (a1 / x[i]);
yp_y[i] = Math.Pow(yp[i] - y_, 2);
syp_y += yp_y[i];
y_yn[i] = Math.Pow(y[i] - y_, 2);
sy_yn += y_yn[i];
fileyp.Write(Math.Round(yp[i], 2) + "\n");
}
R = syp_y / sy_yn;
dataGridView1.Rows[0].Cells[4].Value = R;
output(xw, y, x2, xwy, yp, yp_y, y_yn, sxw, sy, sx2, sxwy, syp_y, sy_yn, R);
fileyp.Close();
}
public void function2(double[] x, double[] y)
{
StreamReader filen = new StreamReader("n.txt");
int number = Convert.ToInt32(filen.ReadLine());
StreamWriter fileyp = new StreamWriter("yp.txt");
double[] yw = new double[number];
double[] x2 = new double[number];
double[] xyw = new double[number];
double[] yp = new double[number];
double[] yp_y = new double[number];
double[] y_yn = new double[number];
double sy = 0, sx = 0, syw = 0, sx2 = 0, sxyw = 0, syp_y = 0, sy_yn = 0, a0, a1, a0w, y_ = 0, R;
for (int i = 0; i < number; i++)
{
sx += x[i];
sy += y[i];
yw[i] = Math.Log(y[i]);
syw += yw[i];
x2[i] = Math.Pow(x[i], 2);
sx2 += x2[i];
xyw[i] = x[i] * yw[i];
sxyw += xyw[i];
}
y_ = sy / number;
a1 = ((number * sxyw) - (syw * sx)) / ((number * sx2) - Math.Pow(sx, 2));
a0w = (syw - (a1 * sx)) / number;
a0 = Math.Exp(a0w);
for (int i = 0; i < number; i++)
{
yp[i] = a0 * Math.Exp(a1 * x[i]);
yp_y[i] = Math.Pow(yp[i] - y_, 2);
syp_y += yp_y[i];
y_yn[i] = Math.Pow(y[i] - y_, 2);
sy_yn += y_yn[i];
fileyp.Write(Math.Round(yp[i], 2) + "\n");
}
R = syp_y / sy_yn;
dataGridView1.Rows[1].Cells[4].Value = R;
output(x, yw, x2, xyw, yp, yp_y, y_yn, sx, syw, sx2, sxyw, syp_y, sy_yn, R);
fileyp.Close();
}
public void function3(double[] x, double[] y)
{
StreamReader filen = new StreamReader("n.txt");
int number = Convert.ToInt32(filen.ReadLine());
StreamWriter fileyp = new StreamWriter("yp.txt");
double[] xw = new double[number];
double[] yw = new double[number];
double[] x2 = new double[number];
double[] xyw = new double[number];
double[] yp = new double[number];
double[] yp_y = new double[number];
double[] y_yn = new double[number];
double sy = 0, sxw = 0, syw = 0, sx2 = 0, sxy = 0, syp_y = 0, sy_yn = 0, a0, a1, a0w, y_ = 0, R;
for (int i = 0; i < number; i++)
{
sy += y[i];
xw[i] = Math.Log(x[i]);
sxw += xw[i];
yw[i] = Math.Log(y[i]);
syw += yw[i];
x2[i] = Math.Pow(xw[i], 2);
sx2 += x2[i];
xyw[i] = xw[i] * yw[i];
sxy += xyw[i];
}
y_ = sy / number;
a1 = ((number * sxy) - (syw * sxw)) / ((number * sx2) - Math.Pow(sxw, 2));
a0w = (syw - (a1 * sxw)) / number;
a0 = Math.Exp(a0w);
for (int i = 0; i < number; i++)
{
yp[i] = a0 * Math.Pow(x[i], a1);
yp_y[i] = Math.Pow(yp[i] - y_, 2);
syp_y += yp_y[i];
y_yn[i] = Math.Pow(y[i] - y_, 2);
sy_yn += y_yn[i];
fileyp.Write(Math.Round(yp[i], 2) + "\n");
}
R = syp_y / sy_yn;
dataGridView1.Rows[2].Cells[4].Value = R;
output(xw, yw, x2, xyw, yp, yp_y, y_yn, sxw, syw, sx2, sxy, syp_y, sy_yn, R);
fileyp.Close();
}
public void function4(double[] x, double[] y)
{
StreamReader filen = new StreamReader("n.txt");
int number = Convert.ToInt32(filen.ReadLine());
StreamWriter fileyp = new StreamWriter("yp.txt");
double[] xw = new double[number];
double[] x2 = new double[number];
double[] xyw = new double[number];
double[] yp = new double[number];
double[] yp_y = new double[number];
double[] y_yn = new double[number];
double sy = 0, sxw = 0, sx2 = 0, sxy = 0, syp_y = 0, sy_yn = 0, a0, a1, y_ = 0, R;
for (int i = 0; i < number; i++)
{
xw[i] = Math.Log(x[i]);
sxw += xw[i];
sy += y[i];
x2[i] = Math.Pow(xw[i], 2);
sx2 += x2[i];
xyw[i] = xw[i] * y[i];
sxy += xyw[i];
}
y_ = sy / number;
a1 = ((number * sxy) - (sy * sxw)) / ((number * sx2) - Math.Pow(sxw, 2));
a0 = (sy - (a1 * sxw)) / number;
for (int i = 0; i < number; i++)
{
yp[i] = a0 + a1 * Math.Log(x[i]);
yp_y[i] = Math.Pow(yp[i] - y_, 2);
syp_y += yp_y[i];
y_yn[i] = Math.Pow(y[i] - y_, 2);
sy_yn += y_yn[i];
fileyp.Write(Math.Round(yp[i], 2) + "\n");
}
R = syp_y / sy_yn;
dataGridView1.Rows[3].Cells[4].Value = R;
output(xw, y, x2, xyw, yp, yp_y, y_yn, sxw, sy, sx2, sxy, syp_y, sy_yn, R);
fileyp.Close();
}
public void function5(double[] x, double[] y)
{
StreamReader filen = new StreamReader("n.txt");
int number = Convert.ToInt32(filen.ReadLine());
StreamWriter fileyp = new StreamWriter("yp.txt");
double[] xw = new double[number];
double[] yw = new double[number];
double[] x2 = new double[number];
double[] xyw = new double[number];
double[] yp = new double[number];
double[] yp_y = new double[number];
double[] y_yn = new double[number];
double sy = 0, sxw = 0, syw = 0, sx2 = 0, sxy = 0, syp_y = 0, sy_yn = 0, a0, a0w, a1, a1w, y_ = 0, R;
for (int i = 0; i < number; i++)
{
xw[i] = 1 / x[i];
sxw += xw[i];
yw[i] = 1 / y[i];
syw += yw[i];
sy += y[i];
x2[i] = Math.Pow(xw[i], 2);
sx2 += x2[i];
xyw[i] = xw[i] * yw[i];
sxy += xyw[i];
}
y_ = sy / number;
a1w = ((number * sxy) - (syw * sxw)) / ((number * sx2) - Math.Pow(sxw, 2));
a0w = (sy - (a1w * sxw)) / number;
a1 = 1 / a0w;
a0 = a1 / a0w;
for (int i = 0; i < number; i++)
{
yp[i] = (a0 * xw[i]) / (a1 + xw[i]);
yp_y[i] = Math.Pow(yp[i] - y_, 2);
syp_y += yp_y[i];
y_yn[i] = Math.Pow(yw[i] - y_, 2);
sy_yn += y_yn[i];
fileyp.Write(Math.Round(yp[i], 2) + "\n");
}
R = syp_y / sy_yn;
dataGridView1.Rows[4].Cells[4].Value = R;
output(xw, y, x2, xyw, yp, yp_y, y_yn, sxw, sy, sx2, sxy, syp_y, sy_yn, R);
fileyp.Close();
}
private void button3_Click(object sender, EventArgs e)
{
StreamReader filen = new StreamReader("n.txt");
int number = Convert.ToInt32(filen.ReadLine());
StreamReader filex = new StreamReader("x.txt");
StreamReader filey = new StreamReader("y.txt");
double[] x = new double[number];
double[] y = new double[number];
for (int i = 0; i < number; i++)
{
x[i] = Convert.ToDouble(filex.ReadLine());
y[i] = Convert.ToDouble(filey.ReadLine());
}
//додавання рядків
for (int i = 0; i < number + 2; i++)
{