Beispiel #1
0
        private void SetBorderGrid(int k)
        {
            var a = _equation.a;
            var b = _equation.b;
            var c = _equation.c;

            var h   = (_params.SpaceBoundRight - _params.SpaceBoundLeft) / _params.SpaceStepCount;
            var tau = _params.TimeLimit / _params.TimeStepCount;

            var alpha = _conditions.FirstConditionParameters[0];
            var betta = _conditions.FirstConditionParameters[1];

            var gamma = _conditions.SecondConditionParameters[0];
            var delta = _conditions.SecondConditionParameters[1];

            var f0 = _conditions.FirstCondition(0, GetTimeCoordinate(k));
            var fn = _conditions.SecondCondition(0, GetTimeCoordinate(k));

            switch (_params.BoundaryApproximation)
            {
            case BoundaryApproximationType.FirstDegreeTwoPoints:
                _grid[0, k] = -(alpha / h) / (betta - alpha / h) * _grid[1, k] +
                              f0 / (betta - alpha / h);
                _grid[_params.SpaceStepCount, k] = (gamma / h) / (delta + gamma / h) * _grid[_params.SpaceStepCount - 1, k] +
                                                   fn / (delta + gamma / h);
                break;

            case BoundaryApproximationType.SecondDegreeThreePoints:
                _grid[0, k] = 1 / (-3 * alpha / (2 * h) + betta) *
                              (f0 + alpha / (2 * h) * (_grid[2, k] - 4 * _grid[1, k]));
                _grid[_params.SpaceStepCount, k] = 1 / (3 * gamma / (2 * h) + delta) *
                                                   (fn + gamma / (2 * h) * (4 * _grid[_params.SpaceStepCount - 1, k] - _grid[_params.SpaceStepCount - 2, k]));
                break;

            case BoundaryApproximationType.SecondDegreeTwoPoints:
                var lam0 = (-2 * a / h - h / tau + c * h) / (2 * a - b * h);
                var ro0  = (2 * a * _grid[1, k] / h
                            + h * _grid[0, k - 1] / tau
                            + _equation.f(0, GetTimeCoordinate(k)) * h)
                           / (2 * a - b * h);

                var lam1 = (2 * a / h + h / tau - c * h) / (2 * a + b * h);
                var ro1  = (-2 * a * _grid[_params.SpaceStepCount - 1, k] / h
                            - 1 * h * _grid[_params.SpaceStepCount, k - 1] / tau
                            + _equation.f(GetSpaceCoordinate(_params.SpaceStepCount), GetTimeCoordinate(k)) * h)
                           / (2 * a + b * h);

                _grid[0, k] = (f0 - alpha * ro0) / (alpha * lam0 + betta);
                _grid[_params.SpaceStepCount, k] = (fn - gamma * ro1) / (gamma * lam1 + delta);
                break;
            }

            //Func<double, double, double> f = (x, t) => Math.Exp(-2 * t) * Math.Sin(x + t);

            //_grid[0, k] = f(0, GetTimeCoordinate(k));
            //_grid[_params.SpaceStepCount, k] = f(GetSpaceCoordinate(_params.SpaceStepCount), GetTimeCoordinate(k));
        }
Beispiel #2
0
        private double[] SetBorderGrid(int k, Matrix matrix)
        {
            var a = _equation.a;
            var b = _equation.b;
            var c = _equation.c;

            var h   = (_params.SpaceBoundRight - _params.SpaceBoundLeft) / _params.SpaceStepCount;
            var tau = _params.TimeLimit / _params.TimeStepCount;

            var alpha = _conditions.FirstConditionParameters[0];
            var betta = _conditions.FirstConditionParameters[1];

            var gamma = _conditions.SecondConditionParameters[0];
            var delta = _conditions.SecondConditionParameters[1];

            var f0 = _conditions.FirstCondition(0, GetTimeCoordinate(k));
            var fn = _conditions.SecondCondition(0, GetTimeCoordinate(k));

            var N = _params.SpaceStepCount;

            var d = new double[_params.SpaceStepCount + 1];

            for (int j = 1; j < N; j++)
            {
                d[j] = _grid[j, k - 1] / tau
                       + _equation.f(GetSpaceCoordinate(j), GetTimeCoordinate(k - 1))
                       + GetExplicitPart(j, k);
            }

            switch (_params.BoundaryApproximation)
            {
            case BoundaryApproximationType.FirstDegreeTwoPoints:
                d[0] = f0;
                d[N] = fn;

                matrix[0, 0] = betta - alpha / h;
                matrix[0, 1] = alpha / h;

                matrix[N, N - 1] = -gamma / h;
                matrix[N, N]     = delta + gamma / h;

                break;

            case BoundaryApproximationType.SecondDegreeThreePoints:
                d[0] = f0;
                d[N] = fn;

                matrix[0, 0] = betta - 3 * alpha / (2 * h);
                matrix[0, 1] = 2 * alpha / h;
                matrix[0, 2] = (-alpha / (2 * h));

                var sim = matrix[0, 2] / matrix[1, 2];
                matrix[0, 0] -= sim * matrix[1, 0];
                matrix[0, 1] -= sim * matrix[1, 1];
                matrix[0, 2] -= sim * matrix[1, 2];
                d[0]         -= sim * d[1];

                matrix[N, N - 2] = gamma / (2 * h);
                matrix[N, N - 1] = -2 * gamma / h;
                matrix[N, N]     = delta + 3 * gamma / (2 * h);

                sim               = matrix[N, N - 2] / matrix[N - 1, N - 2];
                matrix[N, N]     -= sim * matrix[N - 1, N];
                matrix[N, N - 1] -= sim * matrix[N - 1, N - 1];
                matrix[N, N - 2] -= sim * matrix[N - 1, N - 2];
                d[N]             -= sim * d[N - 1];

                break;

            case BoundaryApproximationType.SecondDegreeTwoPoints:
                var lam0 = (-2 * a / h - h / tau + c * h) / (2 * a - b * h);
                var ro0  = (h * _grid[0, k - 1] / tau
                            + _equation.f(0, GetTimeCoordinate(k)) * h)
                           / (2 * a - b * h);

                var lam1 = (2 * a / h + h / tau - c * h) / (2 * a + b * h);
                var ro1  = (-2 * a * _grid[_params.SpaceStepCount - 1, k] / h
                            - 1 * h * _grid[_params.SpaceStepCount, k - 1] / tau
                            + _equation.f(GetSpaceCoordinate(_params.SpaceStepCount), GetTimeCoordinate(k)) * h)
                           / (2 * a + b * h);

                d[0] = f0 - alpha * ro0;
                d[N] = fn - gamma * ro1;

                matrix[0, 0] = alpha * lam0 + betta;
                matrix[0, 1] = alpha * 2 * a / (h * (2 * a - b * h));

                matrix[N, N - 1] = -gamma * 2 * a / (h * (2 * a + b * h));
                matrix[N, N]     = gamma * lam1 + delta;

                break;
            }

            return(d);
        }