Exemplo n.º 1
0
        private void runButton_Click(object sender, EventArgs e)
        {
            double a, b, h1, h2;
            int    n, p;

            try
            {
                a = Convert.ToDouble(aTextBox.Text);
                b = Convert.ToDouble(bTextBox.Text);
                n = Convert.ToInt32(nTextBox.Text);
            }
            catch
            {
                MessageBox.Show("incorrect input.", "Error");
                return;
            }

            p = 100 * n;

            h1 = (b - a) / n;
            h2 = (b - a) / p;

            double[] x = new double[n + 1];
            double[] t = new double[p + 1];

            for (int i = 0; i < n + 1; i++)
            {
                x[i] = a + i * h1;
            }
            for (int i = 0; i < p + 1; i++)
            {
                t[i] = a + i * h2;
            }

            mainChart.Series.Clear();

            for (int k = 0; k < n + 1; k++)
            {
                var spline = Bk1(x, x[k], h1);
                DrawHelper.DrawGraph(mainChart, spline, x, n, $"B{k},1", Color.FromArgb((10 * k + 100) % 255, 128, 0));
            }

            //for (int k = 0; k < n + 1; k++)
            //{
            //    var spline = Bk1(t, x[k], h1);
            //    DrawHelper.DrawGraph(mainChart, spline, t, p, $"B*{k},1", Color.FromArgb((10 * k + 50)%255, 255, 128));
            //}
        }
Exemplo n.º 2
0
        private void runButton_Click(object sender, EventArgs e)
        {
            CheckIfDataValid(out double[] x, out double[] y);

            int n = x.Length;
            int m = 3;

            double[,] matrix = new double[m, m];
            double[] vector = new double[m];

            for (int i = m - 1; i >= 0; i--)
            {
                for (int j = m - 1; j >= 0; j--)
                {
                    matrix[i, j] = RowSumForMatrix(n, x, i + j);
                }

                vector[i] = RowSumForVector(n, y, x, i);
            }

            double[] rez = GaussMethod.Calculate(m, matrix, vector);

            mainChart.Series.Clear();

            int p = 100 * n;

            double[] approximatedFunc = new double[p];
            double[] t = new double[p];
            double   h = (x[x.Length - 1] - x[0]) / p;

            for (int i = 0; i < p; i++)
            {
                t[i] = x[0] + i * h;
                approximatedFunc[i] = Polinom(m, rez, t[i]);
            }

            DrawHelper.DrawGraph(mainChart, y, x, n - 1, "points", Color.Green);
            DrawHelper.DrawGraph(mainChart, approximatedFunc, t, p - 1, "approximated", Color.Red);
        }
        private void runButton_Click(object sender, EventArgs e)
        {
            if (!IsFildsValid())
            {
                return;
            }

            int n;

            try {
                n = Convert.ToInt32(nTextBox.Text);
            } catch {
                errorLabel.Text = "Invalid data";
                return;
            }

            int m = 500;

            double[] accurateY = new double[m + 1];
            double[] accurateX = new double[m + 1];

            for (int i = 0; i < m + 1; i++)
            {
                accurateX[i] = i * 2 * Math.PI / m;
                accurateY[i] = _function.F(accurateX[i]);
            }

            double[] y = new double[2 * n + 1];
            double[] t = new double[2 * n + 1];

            double[] a = new double[n + 1];
            double[] b = new double[n];

            for (int j = 0; j < 2 * n + 1; j++)
            {
                t[j] = (2 * Math.PI * j) / (2 * n + 1);
                y[j] = _function.F(t[j]);
            }

            //a[k]
            for (int k = 0; k < n + 1; k++)
            {
                double sum = 0;

                for (int j = 0; j < 2 * n + 1; j++)
                {
                    sum += y[j] * Math.Cos(2 * Math.PI * j * k / (2 * n + 1));
                }

                a[k] = (2 * sum) / (2 * n + 1);
            }

            //b[k]
            for (int k = 0; k < n; k++)
            {
                double sum = 0;

                for (int j = 0; j < 2 * n + 1; j++)
                {
                    sum += y[j] * Math.Sin(2 * Math.PI * j * (k + 1) / (2 * n + 1));
                }

                b[k] = (2 * sum) / (2 * n + 1);
            }

            double[] q = new double[m + 1];

            for (int i = 0; i < m + 1; i++)
            {
                q[i] = Q(accurateX[i], n, a, b);
            }

            mainChart.Series.Clear();

            DrawHelper.DrawGraph(mainChart, accurateY, accurateX, m, "accurate", Color.Green);
            DrawHelper.DrawGraph(mainChart, q, accurateX, m, "trigonometric", Color.Blue);
        }
Exemplo n.º 4
0
        private void runButton_Click(object sender, EventArgs e)
        {
            double a, b, h1, h2;
            int    n, m;

            try
            {
                a = Convert.ToDouble(aTextBox.Text);
                b = Convert.ToDouble(bTextBox.Text);
                n = Convert.ToInt32(nTextBox.Text);
            }
            catch
            {
                MessageBox.Show("incorrect input.", "Error");
                return;
            }

            m = 100 * n;

            h1 = (b - a) / n;
            h2 = (b - a) / m;

            double[] x    = new double[n + 1];
            double[] alfa = new double[n + 1];
            double[] beta = new double[n + 3];
            double[] y    = new double[m + 1];
            double[] t    = new double[m + 1];

            for (int i = 0; i < n + 1; i++)
            {
                x[i]    = a + i * h1;
                alfa[i] = _function.F(x[i]);
            }
            for (int i = 0; i < m + 1; i++)
            {
                t[i] = a + i * h2;
                y[i] = _function.F(t[i]);
            }

            double[] S1 = new double[n + 1];
            double[] S3 = new double[m + 1];

            for (int i = 0; i < n + 1; i++)
            {
                S1[i] = SumForS1(n, alfa, x[i], x, h1);
            }

            //matrix.
            double[,] _A = new double[n + 3, n + 3];
            double[] _b = new double[n + 3];

            _A[0, 0]         = -0.5;
            _A[0, 2]         = 0.5;
            _A[n + 2, n]     = -0.5;
            _A[n + 2, n + 2] = 0.5;
            _b[0]            = h1 * 4 * a * a * a;
            _b[n + 2]        = h1 * 4 * b * b * b;

            for (int i = 1; i < n + 2; i++)
            {
                _A[i, i - 1] = 1 / 6.0;
                _A[i, i]     = 2 / 3.0;
                _A[i, i + 1] = 1 / 6.0;
                _b[i]        = _function.F(x[i - 1]);
            }

            beta = GaussMethod.Calculate(n + 3, _A, _b);

            for (int i = 0; i < m + 1; i++)
            {
                S3[i] = SumForS3(n, beta, t[i], x, h1);
            }

            mainChart.Series.Clear();

            DrawHelper.DrawGraph(mainChart, y, t, m - 1, "Exact", Color.Green);
            DrawHelper.DrawGraph(mainChart, S1, x, n, "S1", Color.Indigo);
            DrawHelper.DrawGraph(mainChart, S3, t, m, "S3", Color.Red);
        }
Exemplo n.º 5
0
        private void runButton_Click(object sender, EventArgs e)
        {
            if (!IsFildsValid())
            {
                return;
            }

            double a, b, faultX, h1, h2;
            int    n, m;

            try {
                a      = Convert.ToDouble(aTextBox.Text);
                b      = Convert.ToDouble(bTextBox.Text);
                faultX = Convert.ToDouble(xTextBox.Text);
                n      = Convert.ToInt32(nTextBox.Text);
                m      = Convert.ToInt32(mTextBox.Text);
            } catch {
                errorLabel.Text = "Invalid data";
                return;
            }

            h1 = (b - a) / n;
            h2 = (b - a) / m;

            double[] x          = new double[n + 1];
            double[] t          = new double[m + 1];
            double[] x_ch       = new double[n + 1];
            double[] y          = new double[n + 1];
            double[] accurate_y = new double[m + 1];
            double[] y_ch       = new double[n + 1];

            for (int k = 0; k < n + 1; k++)
            {
                x[k] = a + k * h1;
                y[k] = _function.F(x[k]);
            }
            for (int k = 0; k < m + 1; k++)
            {
                t[k]          = a + k * h2;
                accurate_y[k] = _function.F(t[k]);
            }
            for (int k = 0; k < n + 1; k++)
            {
                x_ch[k] = ((a + b) / 2) + ((b - a) / 2) * Math.Cos(((double)(2 * k + 1) / (double)(2 * (n + 1))) * Math.PI);
                y_ch[k] = _function.F(x_ch[k]);
            }

            double[] p    = new double[m + 1];
            double[] p_ch = new double[m + 1];

            for (int k = 0; k < m + 1; k++)
            {
                p[k] = _polynomialForm.CalcPolynomial(n + 1, x, y, t[k]);
            }
            for (int k = 0; k < m + 1; k++)
            {
                p_ch[k] = _polynomialForm.CalcPolynomial(n + 1, x_ch, y_ch, t[k]);
            }

            mainChart.Series.Clear();

            DrawHelper.DrawGraph(mainChart, accurate_y, t, m, "accurate", Color.Green);
            DrawHelper.DrawGraph(mainChart, p, t, m, "simple polynomial", Color.Blue);
            DrawHelper.DrawGraph(mainChart, p_ch, t, m, "Chebyshev polynomial", Color.Pink);

            double polynomialError = Math.Abs(_function.F(faultX) - _polynomialForm.CalcPolynomial(n, x, y, faultX));

            funcErrorLabel.Text = String.Format("Error: \n{0:f13}", polynomialError);
        }