Esempio n. 1
0
        public static double Integrate(IFunction func, double start, double end, int step)
        {
            var d   = (end - start) / step;
            var sum = (func.F(start) + func.F(end)) / 2;

            for (var i = 1; i < step; i++)
            {
                sum += func.F(start + i * d);
            }
            return(d * sum);
        }
Esempio n. 2
0
        private void DrawFunction()
        {
            double step = 0.01;

            double.TryParse(APointTextBox.Text, out double a);
            double.TryParse(BPointTextBox.Text, out double b);

            var xAxis = new List <double>();
            var yAxis = new List <double>();

            while (a <= b)
            {
                xAxis.Add(a);
                yAxis.Add(_selectedFunction.F(a));

                a += step;
            }

            DrawHelper.DrawGraph(Chart, yAxis.ToArray(), xAxis.ToArray(), _selectedFunction.ToString(), Color.Red, clearChart: true);
        }
Esempio n. 3
0
 public double Integrate(IFunction function, long steps, double startX, double endX)
 {
     double result = 0.0, d = (endX - startX) / ((double)steps);
     long step = 0;
     for (double x = startX; x <= endX; x += d)
     {
         double y = function.F(x);
         if (0 < step && step < steps)
         {
             double tmpResult = d * y;
             result += tmpResult;
         }
         else
         {
             double tmpResult = d * y * 0.5;
             result += tmpResult;
         }
         step++;
     }
     return result;
 }
Esempio n. 4
0
        public double Integrate(IFunction function, long steps, double startX, double endX)
        {
            double result = 0.0, d = (endX - startX) / ((double)steps);
            long   step = 0;

            for (double x = startX; x <= endX; x += d)
            {
                double y = function.F(x);
                if (0 < step && step < steps)
                {
                    double tmpResult = d * y;
                    result += tmpResult;
                }
                else
                {
                    double tmpResult = d * y * 0.5;
                    result += tmpResult;
                }
                step++;
            }
            return(result);
        }
        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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
        }