Beispiel #1
0
        private void buttonSolve_Click(object sender, EventArgs e)
        {
            if (checkAutoSearch.Checked)
            {
                AutoFindRoots(); return;
            }

            double p0, p1, p2;
            double eps;

            errorProvider.Clear();

            try
            { eps = float.Parse(textE.Text); }
            catch (FormatException)
            { errorProvider.SetError(textE, "Wrong float number"); return; }
            if (eps < 0)
            {
                errorProvider.SetError(textE, "Has to be > 0");
                return;
            }

            double          res   = double.NaN;
            DekartForm      dForm = null;
            List <PointF[]> lines = null;

            switch (selectedSolMeth)
            {
                #region Method Still Point
            case SolutionMethod.StillPoint:

                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }

                dForm      = new DekartForm(100, 100, 320, 300);
                dForm.Size = new Size(700, 500);

                dForm.AddGraphic(g, -5f, 5f, DrawModes.DrawLines, Color.Green);
                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Gray);
                dForm.AddGraphic(bisectress, -5f, 5f, DrawModes.DrawLines, Color.Blue);

                res = FindRoot.StillPointMethod(f, p0, eps, out lines, false);
                break;
                #endregion

                #region Method Bisection
            case SolutionMethod.Bisection:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }
                try
                { p1 = float.Parse(textP1.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP1, "Wrong float number"); return; }

                dForm = new DekartForm(100, 100, 175, 300);
                dForm.Use_IsVisible = false;
                dForm.Size          = new Size(350, 600);

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.Bisection(f, p0, p1, eps, out lines, false);
                break;

                #endregion

                #region Method Newtone-Rafson
            case SolutionMethod.NewtoneRafson:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }

                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.NewtoneRafson(f, df, p0, eps, out lines, false);
                break;

                #endregion

                #region Method Cuttings
            case SolutionMethod.Cuttings:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }
                try
                { p1 = float.Parse(textP1.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP1, "Wrong float number"); return; }

                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.Cuttings(f, p0, p1, eps, out lines, false);
                break;

                #endregion

                #region Method Hord
            case SolutionMethod.Hord:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }
                try
                { p1 = float.Parse(textP1.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP1, "Wrong float number"); return; }

                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.Hord(f, d2f, p0, p1, eps, out lines, false);

                break;

                #endregion

                #region Method Muller
            case SolutionMethod.Muller:
                try
                { p0 = float.Parse(textP0.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP0, "Wrong float number"); return; }
                try
                { p1 = float.Parse(textP1.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP1, "Wrong float number"); return; }
                try
                { p2 = float.Parse(textP2.Text); }
                catch (FormatException)
                { errorProvider.SetError(textP2, "Wrong float number"); return; }

                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;

                dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);

                res = FindRoot.Muller(f, p0, p1, p2, eps, ref dForm, false);

                lines = null;
                break;
                #endregion

            default:
                return;
            }

            #region Print results

            if (lines != null)
            {
                foreach (PointF[] pts in lines)
                {
                    dForm.AddPolygon(Color.Red, DrawModes.DrawLines, pts);
                }
            }
            dForm.Show();
            dForm.Update2();

            listRoots.Items.Clear();
            listY.Items.Clear();

            if (double.IsNaN(res))
            {
                MessageBox.Show("Корни не найдены.");
                return;
            }

            listRoots.Items.Add("x" + (listRoots.Items.Count + 1) + " = "
                                + res.ToString("F16"));
            listY.Items.Add("y" + (listY.Items.Count + 1) + " = "
                            + f(res).ToString("F16"));
            #endregion
        }