Exemplo n.º 1
0
        public CSplineSubinterval(CPoint p1, CPoint p2, double df, double ddf)
        {
            _p1 = p1;
            _p2 = p2;

            B = ddf;
            C = df;
            D = p1.Y;
            A = (_p2.Y - B * Math.Pow(_p2.X - _p1.X, 2) - C * (_p2.X - _p1.X) - D) / Math.Pow(_p2.X - _p1.X, 3);
        }
Exemplo n.º 2
0
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Xlist.Clear();
                Ylist.Clear();
                var points = new CPoint[coordinateGridView.Rows.Count - 1];

                for (int i = 0; i < coordinateGridView.Rows.Count - 1; ++i)
                {
                    //Добавляем строку, указывая значения каждой ячейки по имени (можно использовать индекс 0, 1, 2 вместо имен)
                    string tx = coordinateGridView["XColumn", i].Value.ToString();
                    tx = tx.Replace(".", ",");
                    string ty = coordinateGridView["YColumn", i].Value.ToString();
                    ty = ty.Replace(".", ",");

                    //интерполяция Лагранжем
                    Xlist.Add(Convert.ToDouble(tx));
                    Ylist.Add(Convert.ToDouble(ty));

                    //сплайн
                    points[i] = new CPoint(Convert.ToDouble(tx), Convert.ToDouble(ty));
                }
                _model = new CSpline(points);
                _model.GenerateSplines();
                #region находим минимальные и максимальные значения для граффика
                QestmaxX = Xlist[0];
                QestminX = Xlist[0];
                QestmaxY = Ylist[0];
                QestminY = Ylist[0];

                foreach (double x in Xlist)
                {
                    if (x > QestmaxX)
                    {
                        QestmaxX = x;
                    }
                    if (x < QestminX)
                    {
                        QestminX = x;
                    }
                }
                QestmaxX *= 1.05;
                QestminX *= 0.95;

                foreach (double y in Ylist)
                {
                    if (y > QestmaxY)
                    {
                        QestmaxY = y;
                    }
                    if (y < QestminY)
                    {
                        QestminY = y;
                    }
                }
                QestmaxY *= 1.05;
                QestminY *= 0.9;

                #endregion

                double sumX   = 0;
                double sumX2  = 0;
                double sumX3  = 0;
                double sumX4  = 0;
                double sumY   = 0;
                double sumXY  = 0;
                double sumX2Y = 0;

                foreach (double x in Xlist)
                {
                    sumX += x;
                }
                foreach (double x in Xlist)
                {
                    sumX2 += x * x;
                }
                foreach (double x in Xlist)
                {
                    sumX3 += x * x * x;
                }
                foreach (double x in Xlist)
                {
                    sumX4 += x * x * x * x;
                }
                foreach (double y in Ylist)
                {
                    sumY += y;
                }
                for (int n = 0; n < Xlist.Count; n++)
                {
                    sumXY += Xlist[n] * Ylist[n];
                }
                for (int n = 0; n < Xlist.Count; n++)
                {
                    sumX2Y += (Xlist[n] * Xlist[n]) * Ylist[n];
                }
                for (int n = 0; n < Xlist.Count; n++)
                {
                    DrawPoint(Xlist[n], Ylist[n], 6);
                }

                textBox1.Text  = "";
                textBox1.Text += $"\u03B2\u2080*{Xlist.Count}+\u03B2\u2081*{Math.Round(sumX, accuracy)}+\u03B2\u2081*{Math.Round(sumX2, accuracy)}= {sumY}\r\n ";
                textBox1.Text += $"\u03B2\u2080*{Math.Round(sumX, accuracy)}+\u03B2\u2081*{Math.Round(sumX2, accuracy)}+\u03B2\u2081*{Math.Round(sumX3, accuracy)}= {Math.Round(sumXY, accuracy)}\r\n ";
                textBox1.Text += $"\u03B2\u2080*{Math.Round(sumX2, accuracy)}+\u03B2\u2081*{Math.Round(sumX3, accuracy)}+\u03B2\u2082*{Math.Round(sumX4, accuracy)}= {Math.Round(sumX2Y, accuracy)}\r\n ";

                double delta = Xlist.Count * sumX2 * sumX4 + 2 * sumX * sumX3 * sumX2 - sumX2 * sumX2 * sumX2
                               - Xlist.Count * sumX3 * sumX3 - sumX * sumX * sumX4;
                textBox1.Text += $"\u0394 is {Math.Round(delta, accuracy)}\r\n ";
                double deltaB0 = sumY * sumX2 * sumX4 + sumX * sumX3 * sumX2Y + sumXY * sumX3 * sumX2
                                 - sumX2Y * sumX2 * sumX2 - sumX3 * sumX3 * sumY - sumXY * sumX * sumX4;
                double B0 = deltaB0 / delta;
                textBox1.Text += $"\u03B2\u2080 = {Math.Round(B0, accuracy)}\r\n ";
                double deltaB1 = Xlist.Count * sumXY * sumX4 + sumY * sumX3 * sumX2 + sumX * sumX2Y * sumX2 - sumX2 * sumXY * sumX2 - sumX2Y * sumX3 * Xlist.Count - sumX * sumY * sumX4;
                double B1      = deltaB1 / delta;
                textBox1.Text += $"\u03B2\u2081 = {Math.Round(B1, accuracy)}\r\n ";
                double deltaB2 = Xlist.Count * sumX2 * sumX2Y + sumXY * sumX * sumX2 + sumX * sumX3 * sumY - sumX2 * sumX2 * sumY - sumX3 * sumXY * Xlist.Count - sumX * sumX * sumX2Y;
                double B2      = deltaB2 / delta;
                textBox1.Text += $"\u03B2\u2082 = {Math.Round(B2, accuracy)}\r\n ";
                double tempSumm = 0;
                for (int i = 0; i < Xlist.Count; i++)
                {
                    tempSumm += (Ylist[i] - B0 - B1 * Xlist[i] - B2 * Xlist[i] * Xlist[i]) * (Ylist[i] - B0 - B1 * Xlist[i] - B2 * Xlist[i] * Xlist[i]);
                }
                double tempSigma = (tempSumm / (Xlist.Count - 1));
                double sigma     = Math.Sqrt(tempSigma);
                textBox1.Text += $"\u03A3= {Math.Round(sigma, accuracy)}\r\n ";
                textBox1.Text += $" уравнение кривой:  y={Math.Round(B0, accuracy)}+{Math.Round(B1, accuracy)}*x+{Math.Round(B2, accuracy)}*x^2\r\n ";
                DrawGrapf(B2, B1, B0);
                DrawGrapf(Xlist, Ylist);
                DrawGrapf(_model);
            }
            catch (Exception ex)
            {
                textBox1.Text = ex.Message;
            }
        }