Exemplo n.º 1
0
        public double SetEps(double eps0)
        {
            if (_Values.Count < 2)
            {
                Initialize();
            }
            var n1 = _Values.First;
            var n2 = n1.Next;

            var I       = 0d;
            var v1      = n1.Value;
            var x1      = v1.Key;
            var y1      = _DoubleConverter(v1.Value);
            var eps1    = 0d;
            var min_max = new MinMaxValue();
            var dI      = y1 / 2;

            min_max.AddValue(y1);
            do
            {
                var v2 = n2.Value;
                var x2 = v2.Key;
                var y2 = _DoubleConverter(v2.Value);
                min_max.AddValue(y2);

                var eps = GetEps(x1, x2, y1, y2);

                if (eps < eps0)
                {
                    if (eps > eps1)
                    {
                        eps1 = eps;
                    }
                    n1  = n2;
                    dI += y2 / 2;
                    I  += dI * (x2 - x1);
                    dI  = y2 / 2;
                    y1  = y2;
                    x1  = x2;
                    n2  = n1.Next;
                    continue;
                }

                x2 = (x1 + x2) / 2;
                v2 = new KeyValuePair <double, T>(x2, _Function(x2));
                n2 = _Values.AddAfter(n1, v2);
            } while (n2 != null);

            if (eps1.Equals(0d))
            {
                eps1 = eps0;
            }
            Set(ref _Eps0, eps1, nameof(Eps0));
            Set(ref _Min, min_max.Min, nameof(Min));
            Set(ref _Max, min_max.Max, nameof(Max));
            Set(ref _Integral, I, nameof(Integral));
            return(eps1);
        }
Exemplo n.º 2
0
        public async Task <double> SetEpsAsync(double eps0, IProgress <double> progress = null, CancellationToken cancel = default)
        {
            await Task.Yield();

            if (_Values.Count < 2)
            {
                await Task.Factory.StartNew(Initialize, cancel).ConfigureAwait(true);
            }

            var n1 = _Values.First;
            var n2 = n1.Next;

            var I       = 0d;
            var v1      = n1.Value;
            var x1      = v1.Key;
            var y1      = _DoubleConverter(v1.Value);
            var eps1    = 0d;
            var min_max = new MinMaxValue();
            var dI      = y1 / 2;

            min_max.AddValue(y1);
            var Dx = _X2 - _X1;

            progress?.Report(0d);
            do
            {
                var v2 = n2.Value;
                var x2 = v2.Key;
                var y2 = _DoubleConverter(v2.Value);
                min_max.AddValue(y2);

                var eps = GetEps(x1, x2, y1, y2);

                if (eps < eps0)
                {
                    if (eps > eps1)
                    {
                        eps1 = eps;
                    }
                    n1  = n2;
                    dI += y2 / 2;
                    I  += dI * (x2 - x1);
                    dI  = y2 / 2;
                    y1  = y2;
                    x1  = x2;
                    n2  = n1.Next;
                    progress?.Report((x1 - _X1) / Dx);
                    continue;
                }

                x2 = (x1 + x2) / 2;
                v2 = new KeyValuePair <double, T>(x2, _Function(x2));
                n2 = _Values.AddAfter(n1, v2);
            } while (n2 != null);
            progress?.Report(1d);
            if (eps1.Equals(0d))
            {
                eps1 = eps0;
            }
            Set(ref _Eps0, eps1, nameof(Eps0));
            Set(ref _Min, min_max.Min, nameof(Min));
            Set(ref _Max, min_max.Max, nameof(Max));
            Set(ref _Integral, I, nameof(Integral));
            return(eps1);
        }