Exemplo n.º 1
0
        public IEnumerable <IShape> GenerateIllustrationForCurrentStep(IEnumerable <double> actualPoints, IDifferentiableTwice analyzedFunction, int iterationNumber)
        {
            if (!actualPoints.IsEnoughOfParams(4))
            {
                MethodsHelper.ThrowGreedlyException(actualPoints, 4);
            }

            var lastPoints = actualPoints.GetLastN(4);

            var x1 = lastPoints[0];
            var x2 = lastPoints[1];
            var x3 = lastPoints[2];

            var y1 = analyzedFunction.Compute(lastPoints[0]);
            var y2 = analyzedFunction.Compute(lastPoints[1]);
            var y3 = analyzedFunction.Compute(lastPoints[2]);

            double B1;
            double C1;
            var    A1 = ParabolaCoeffs(x1, x2, x3, y2, y1, y3, out B1, out C1);

            yield return(new ShapeKeeper <IFunction>(new SimpleFunctionKeeper(x => x * x * A1 + x * B1 + C1),
                                                     new UsualExpandingDomain().Update(lastPoints[0]).Update(lastPoints[1]).Update(lastPoints[2]).Update(lastPoints[3]),
                                                     string.Format("Parabola on {0}x*x+{1}*x+{2} N{3}", A1, B1, C1, iterationNumber)
                                                     ));

            var rightRoot = (-B1 + Sign * Math.Sqrt(B1 * B1 - 4 * A1 * C1)) / (2 * A1);

            yield return(new VerticalSegment(0, analyzedFunction.Compute(rightRoot), rightRoot, string.Format("Parabola step N{0}", iterationNumber)));
        }
Exemplo n.º 2
0
        public IEnumerable <IShape> GenerateIllustrationForCurrentStep(IEnumerable <double> actualPoints, IFunction analyzedFunction, int iterationNumber)
        {
            if (!actualPoints.IsEnoughOfParams(3))
            {
                MethodsHelper.ThrowGreedlyException(actualPoints, 3);
            }

            var start      = actualPoints.First();
            var lastPoints = actualPoints.GetLastN(2);

            yield return(new LineShape(new UsualExpandingDomain().Update(start).Update(lastPoints[0]).Update(lastPoints[1]),
                                       new Point2D
            {
                X = start,
                Y = analyzedFunction.Compute(start)
            },
                                       new Point2D()
            {
                X = lastPoints[0], Y = analyzedFunction.Compute(lastPoints[0])
            },
                                       string.Format("Chord of {0} step", iterationNumber)
                                       ));

            yield return(new VerticalSegment(0,
                                             analyzedFunction.Compute(lastPoints[1]),
                                             lastPoints[1], string.Format("Chord step N{0}", iterationNumber)));
        }
Exemplo n.º 3
0
        public IEnumerable <IShape> GenerateIllustrationForCurrentStep(IEnumerable <double> actualPoints, IDifferentiableTwice analyzedFunction, int iterationNumber)
        {
            if (!actualPoints.IsEnoughOfParams(4))
            {
                MethodsHelper.ThrowGreedlyException(actualPoints, 4);
            }

            var lastPoints = actualPoints.GetLastN(4);

            var x1 = lastPoints[0];
            var x2 = lastPoints[1];
            var x3 = lastPoints[2];

            var y1 = analyzedFunction.Compute(lastPoints[0]);
            var y2 = analyzedFunction.Compute(lastPoints[1]);
            var y3 = analyzedFunction.Compute(lastPoints[2]);

            var denom = (x1 - x2) * (x1 - x3) * (x2 - x3);
            var A1    = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom;
            var B1    = (Math.Pow(x3, 2) * (y1 - y2) + Math.Pow(x2, 2) * (y3 - y1) + Math.Pow(x1, 2) * (y2 - y3)) / denom;
            var C1    = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom;

            yield return(new ShapeKeeper <IFunction>(new SimpleFunctionKeeper(x => analyzedFunction.Compute(x3)
                                                                              + analyzedFunction.FirstDerivative.Compute(x3) * (x - x3)
                                                                              - MaximumOfDoubleDiff * Math.Pow(x - x3, 2) / 2),
                                                     new UsualExpandingDomain().Update(lastPoints[0]).Update(lastPoints[1]).Update(lastPoints[2]).Update(lastPoints[3]),
                                                     string.Format("Porabola on {0}x*x+{1}*x+{2} N{3}", A1, B1, C1, iterationNumber)
                                                     ));

            yield return(new VerticalSegment(0, analyzedFunction.Compute(lastPoints[3]), lastPoints[3], string.Format("Mullers step N{0}", iterationNumber)));
        }
Exemplo n.º 4
0
        public IEnumerable <double> ComputeNext(IEnumerable <double> previuosPoints, IFunction analyzedFunction)
        {
            if (!previuosPoints.IsEnoughOfParams(2))
            {
                MethodsHelper.ThrowGreedlyException(previuosPoints, 2);
            }

            yield return(MethodStep(previuosPoints.First(), previuosPoints.Last(), analyzedFunction.Compute));
        }
Exemplo n.º 5
0
        public IEnumerable <double> ComputeNext(IEnumerable <double> previuosPoints, IFunction analyzedFunction)
        {
            if (!previuosPoints.IsEnoughOfParams(2))
            {
                MethodsHelper.ThrowGreedlyException(previuosPoints, 2);
            }

            var lastPoints = previuosPoints.GetLastN(2);

            yield return(MethodStep(lastPoints[0], lastPoints[1], analyzedFunction.Compute));
        }
Exemplo n.º 6
0
        public IEnumerable <double> ComputeNext(IEnumerable <double> previuosPoints, IDifferentiableTwice analyzedFunction)
        {
            if (!previuosPoints.IsEnoughOfParams(3))
            {
                MethodsHelper.ThrowGreedlyException(previuosPoints, 3);
            }

            var lastPoints = previuosPoints.GetLastN(3);

            var differed = analyzedFunction.FirstDerivative.Compute(lastPoints[2]);

            var deltaXi = (-differed + Sign * Math.Sqrt(differed * differed + 2 * MaximumOfDoubleDiff * (analyzedFunction.Compute(lastPoints[2])))) / (-MaximumOfDoubleDiff);

            yield return(deltaXi + lastPoints[2]);
        }
Exemplo n.º 7
0
        public IEnumerable <IShape> GenerateIllustrationForCurrentStep(IEnumerable <double> actualPoints, IFunction analyzedFunction, int iterationNumber)
        {
            if (!actualPoints.IsEnoughOfParams(2))
            {
                MethodsHelper.ThrowGreedlyException(actualPoints, 2);
            }

            var lastPoints = actualPoints.GetLastN(2);

            for (var i = 0; i < 2; i++)
            {
                var onLinePoint = analyzedFunction.Compute(lastPoints[i]);

                yield return(new VerticalSegment(onLinePoint + 1, onLinePoint - 1, lastPoints[i], String.Format("Bound({0}) N{1}", lastPoints[i], iterationNumber)));
            }
        }
Exemplo n.º 8
0
        public IEnumerable <double> ComputeNext(IEnumerable <double> previuosPoints, IDifferentiableOnce analyzedFunction)
        {
            if (!previuosPoints.IsEnoughOfParams(1))
            {
                MethodsHelper.ThrowGreedlyException(previuosPoints, 1);
            }

            var prevPoint = previuosPoints.Last();

            if (prevPoint < 0)
            {
                throw new Exception("Метод Ньютона - тыква. Касательная ушла не туда. Надо попробовать начать метод с другой стороны от корня.");
            }

            var inPoint           = analyzedFunction.Compute(prevPoint);
            var inPointDerivative = analyzedFunction.FirstDerivative.Compute(prevPoint);



            yield return(prevPoint - inPoint / inPointDerivative);
        }
Exemplo n.º 9
0
        public IEnumerable <double> ComputeNext(IEnumerable <double> previuosPoints, IFunction analyzedFunction)
        {
            if (!previuosPoints.IsEnoughOfParams(2))
            {
                MethodsHelper.ThrowGreedlyException(previuosPoints, 2);
            }

            var lastPoints = previuosPoints.GetLastN(2);

            var bisect = lastPoints.Average();

            if (Math.Abs(analyzedFunction.Compute(bisect)) < double.Epsilon)
            {
                return(Enumerable.Empty <double>());
            }

            var firstSegment = analyzedFunction.Compute(lastPoints[0]) * analyzedFunction.Compute(bisect);

            if (firstSegment < 0)
            {
                return new[] { lastPoints[0], bisect }
            }
            ;

            var seconsSegment = analyzedFunction.Compute(bisect) * analyzedFunction.Compute(lastPoints[1]);

            if (seconsSegment < 0)
            {
                return new[] { bisect, lastPoints[1] }
            }
            ;

            throw new Exception(string.Format("Метод деления пополам сломался. Выбран отрезок с концами одного знака(f({0}) = {1},f({2}) = {3})",
                                              lastPoints[0],
                                              analyzedFunction.Compute(lastPoints[0]),
                                              lastPoints[1],
                                              analyzedFunction.Compute(lastPoints[1])));
        }
Exemplo n.º 10
0
        public IEnumerable <IShape> GenerateIllustrationForCurrentStep(IEnumerable <double> actualPoints, IDifferentiableOnce analyzedFunction, int iterationNumber)
        {
            if (!actualPoints.IsEnoughOfParams(2))
            {
                MethodsHelper.ThrowGreedlyException(actualPoints, 2);
            }

            var lastPoints = actualPoints.GetLastN(2);

            yield return(new LineShape(new UsualExpandingDomain().Update(lastPoints[0]).Update(lastPoints[1]),
                                       new Point2D {
                X = lastPoints[0], Y = analyzedFunction.Compute(lastPoints[0])
            },
                                       new Point2D {
                X = lastPoints[1], Y = 0
            },
                                       string.Format("Secant N{0}", iterationNumber)));

            yield return(new VerticalSegment(0,
                                             analyzedFunction.Compute(lastPoints[1]),
                                             lastPoints[1],
                                             string.Format("Newton step N{0}", iterationNumber)));
        }
Exemplo n.º 11
0
        public IEnumerable <double> ComputeNext(IEnumerable <double> previuosPoints, IDifferentiableTwice analyzedFunction)
        {
            if (!previuosPoints.IsEnoughOfParams(3))
            {
                MethodsHelper.ThrowGreedlyException(previuosPoints, 3);
            }

            var lastPoints = previuosPoints.GetLastN(3).ToList();

            lastPoints.Sort();

            var x1 = lastPoints[0];
            var x2 = lastPoints[1];
            var x3 = lastPoints[2];

            var y1 = analyzedFunction.Compute(lastPoints[0]);
            var y2 = analyzedFunction.Compute(lastPoints[1]);
            var y3 = analyzedFunction.Compute(lastPoints[2]);


            double B1;
            double C1;
            var    A1 = ParabolaCoeffs(x1, x2, x3, y2, y1, y3, out B1, out C1);

            var root = (-B1 + Sign * Math.Sqrt(B1 * B1 - 4 * A1 * C1)) / (2 * A1);

            if (x1 < root && root < x2)
            {
                return new List <double> {
                           x1, root, x2
                }
            }
            ;
            if (x2 < root && root < x3)
            {
                return new List <double> {
                           x2, root, x3
                }
            }
            ;

            root = (-B1 - Sign * Math.Sqrt(B1 * B1 - 4 * A1 * C1)) / (2 * A1);

            if (x1 < root && root < x2)
            {
                return new List <double> {
                           x1, root, x2
                }
            }
            ;
            if (x2 < root && root < x3)
            {
                return new List <double> {
                           x2, root, x3
                }
            }
            ;

            return(new List <double> {
                x2, root, x3
            });

            throw new Exception();
        }