示例#1
0
        public NewtonsViewModel Calculate(NewtonsModel model)
        {
            decimal DDFX0, DFX0, DP = 0, DP0 = 0, x0 = 0, x1 = 0, fx1 = 0, DFX1 = 0, DDFX1 = 0, RelError = 0;
            int     cond        = 0;
            string  derivative1 = GetDerivativeString(model.Func);
            string  derivative2 = GetDerivativeString(derivative1);
            decimal fx          = FunctionCalculate(model.PointX, model.Func);

            x0 = model.PointX;

            DFX0  = FunctionCalculate(model.PointX, derivative1);
            DDFX0 = FunctionCalculate(model.PointX, derivative2);
            int k = 0;

            do
            {
                if (cond == 2)
                {
                    break;
                }

                k++;
                ProgressBarIncrement?.Invoke(1);
                DDFX0 = FunctionCalculate(x0, derivative2);

                if (Math.Abs(DDFX0) <= model.Epsilon)
                {
                    cond = 1;
                }
                else
                {
                    DP = DFX0 / DDFX0;
                }
                if (k == 1)
                {
                    DP0 = DP;
                }

                if (Math.Sign(DP0) == Math.Sign(DP))
                {
                    x1 = x0 - DP;
                }
                else
                {
                    x1 = x0 - DP / model.R;
                }

                DP0 = DP;

                fx1      = FunctionCalculate(x1, model.Func);
                DFX1     = FunctionCalculate(x1, derivative1);
                RelError = 2 * Math.Abs(DP) / Math.Abs(x1) + model.Epsilon;

                if (RelError < model.Delta && cond != 1)
                {
                    cond = 2;
                }
                x0   = x1;
                DFX0 = DFX1;
            } while (k <= model.IterationMax);


            if (k < model.IterationMax)
            {
                ProgressBarIncrement?.Invoke(model.IterationMax - k + 1);
            }

            return(new NewtonsViewModel
            {
                RelError = RelError,
                Fx = fx,
                Iteration = k,
                X = x1,
                Dfx1 = DFX1,
                Cond = cond,
            });
        }
示例#2
0
        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                labelerr.Text = "";
                _fx           = comboBoxf.Text;
                if (v() != 0)
                {
                    var model = new NewtonsModel
                    {
                        Func         = _fx,
                        Tol          = double.Parse(tolBox.Text),
                        IterationMax = int.Parse(k_maxBox.Text),
                        PointX       = decimal.Parse(aBox.Text),
                        Delta        = (decimal)double.Parse(DeltaBox.Text),
                        Epsilon      = (decimal)double.Parse(tolBox.Text),
                        R            = decimal.Parse(RBox.Text)
                    };

                    progressBar1.Visible = true;
                    progressBar1.Maximum = model.IterationMax + 1;
                    progressBar1.Value   = 0;

                    var stopWatch = new Stopwatch();
                    stopWatch.Start();

                    var newtonsMethod = new NewtonsMethod();

                    newtonsMethod.ProgressBarIncrement += ProgressBarIncrement;

                    var result = await Task.Run(() => newtonsMethod.Calculate(model));

                    stopWatch.Stop();
                    var ts = stopWatch.Elapsed;

                    if (!string.IsNullOrWhiteSpace(result.Error))
                    {
                        MessageBox.Show(result.Error);
                    }
                    else
                    {
                        Sec.Text          = ts.TotalSeconds.ToString("0.0");
                        fx1outBox.Text    = result.Fx.ToString(" 0e0");
                        x1uotFBox.Text    = result.X.ToString(CultureInfo.InvariantCulture);
                        outTolBox.Text    = result.RelError.ToString("0e0");
                        countinerBox.Text = result.Iteration.ToString();
                        // fx1outBox.Text = result.Fx.ToString(CultureInfo.InvariantCulture);
                        outdfx1.Text = result.Dfx1.ToString(CultureInfo.InvariantCulture);

                        if (result.Iteration == model.IterationMax && result.RelError > (decimal)model.Tol)
                        {
                            labelerr.Text = @"Решение с заданной точностью \n за K_Max(" + model.IterationMax +
                                            @")итераций не удалось найти.";
                        }
                    }

                    newtonsMethod.ProgressBarIncrement -= ProgressBarIncrement;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(@"Не удалось распознать F. " + ex.Message);
            }
        }