Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }