Пример #1
0
        static void Main()
        {
            Console.WriteLine("Lab6: Dichotomy method            \n-----------------------------------");
            try
            {
                var solution = DichotomyMethod.Calculate(FUNCTION, PRECISION);
                Console.WriteLine("Solution of {0} via Dichotmy method is: {1}", FUNCTION, solution);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }

            Console.Read();
        }
        private void Dychotomy()
        {
            try
            {
                var vm = DataContext as IntervalFunctionsViewModel;
                vm.Solutions.Clear();


                double a = double.Parse(textBoxA.Text);
                double b = double.Parse(textBoxB.Text);
                double c = double.Parse(textBoxC.Text);

                vm.Interval = new Interval(double.Parse(textBoxL.Text), double.Parse(textBoxR.Text));


                f = x => (x - a) * (x - b) * (x - c);

                vm.PlotModel.Series.Clear();
                vm.PlotModel.Series.Add(
                    new FunctionSeries(
                        f,
                        vm.Interval.Start,
                        vm.Interval.End,
                        0.1,
                        $"f = (x - {a})(x - {b})(x - {c})")
                    );

                vm.PlotModel.InvalidatePlot(true);

                dm = new DichotomyMethod(vm.Interval, x => (x - a) * (x - b) * (x - c));

                vm.Solutions = new ObservableCollection <Interval>(dm.Solve());

                string str = "";
                if (dm.Count <= 1 && vm.Solutions.Count == 0)
                {
                    str += "Коренів на вказаному проміжку немає. ";
                }
                textBlockCount.Text = str + $"К-ість ітерацій {dm.Count}";
            }
            catch (Exception)
            { }
        }
Пример #3
0
        private void CalculateButton_Click(object sender, EventArgs e)
        {
            OutputRichTextBox.Text = string.Empty;

            int N = 3;

            double[] x = new double[N];
            double[,] I = new double[, ]
            {
                { 1, 0, 0 },
                { 0, 1, 0 },
                { 0, 0, 1 }
            };
            double[,] H = (double[, ])I.Clone();

            x = StartPointsTextBox.Text.Split(new char[] { ' ', ';' }).Select(Convert.ToDouble).ToArray();

            var h = new double[] { 0, 0, 0 };
            var p = 0.5; //кроковий множник

            var xNext = (double[])x.Clone();

            for (int k = 0; k < 500; k++)
            {
                //крок 2
                var fD     = _selectedFunction.D(x[0], x[1], x[2]);
                var fDNext = _selectedFunction.D(xNext[0], xNext[1], xNext[2]);

                x = (double[])xNext.Clone();

                //крок 3
                if (Math.Round(fDNext[0], 5) == 0 && Math.Round(fDNext[1], 5) == 0 && Math.Round(fDNext[2], 5) == 0)
                {
                    OutputRichTextBox.Text += $"x* = ({x[0]}; {x[1]}; {x[2]})";
                    return;
                }

                //крок 4
                if (k != 0)
                {
                    //крок 5
                    if (k % N == 0)
                    {
                        H = (double[, ])I.Clone();
                    }
                    //крок 6
                    else
                    {
                        var r = VectorMultiplicationOnScalar(h, p);
                        var g = VectorDifference(fDNext, fD);

                        //крок 7
                        H = CalculateNextMatrix(I, fDNext, fD, h);
                    }
                }

                //крок 8
                h = NegMatrixMultiplication(MatrixTransposition(H, N), fDNext, N);

                //крок 9
                Func <double, double> compressedFunk = arg => _selectedFunction.F(x[0] + arg * h[0], x[1] + arg * h[1], x[2] + arg * h[2]);
                (var a, var b) = DCKMethod.FindSegment(compressedFunk);
                p = DichotomyMethod.FindMinimumArgument(compressedFunk, a, b);

                //крок 10
                xNext = VectorSum(x, VectorMultiplicationOnScalar(h, p));

                OutputRichTextBox.Text += $"k{k} = ({x[0]:f5}; {x[1]:f5}; {x[2]:f5})\n";
            }
        }