Beispiel #1
0
        private void buttonTaylor_Click(object sender, EventArgs e)
        {
            try
            {
                a = float.Parse(textBoxA.Text);
                b = float.Parse(textBoxB.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Parsing error, aborted.");
                return;
            }

            DekartForm df = new DekartForm(150, 150, 50, 430);

            df.Size = new Size(440, 540);
            df.Text = "Red - error | Olive - Taylor's row | Green - f(x)";

            df.AddGraphic(new DoubleFunction(taylor_err), a, b, DrawModes.DrawLines,
                          Color.Red);
            df.AddGraphic(new DoubleFunction(taylor), a, b, DrawModes.DrawLines,
                          Color.Olive);
            df.AddGraphic(new DoubleFunction(f), a, b, DrawModes.DrawPoints,
                          Color.Green);
            //df.DrawAllGraphics();
            df.Show();
        }
Beispiel #2
0
        private void buttonTask1_Click(object sender, EventArgs e)
        {
            try
            {
                a = float.Parse(textBoxA.Text);
                b = float.Parse(textBoxB.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Parsing error, aborted.");
                return;
            }

            DekartForm df = new DekartForm(40, 40,
                                           Screen.PrimaryScreen.WorkingArea.Width / 2,
                                           Screen.PrimaryScreen.WorkingArea.Height / 2);

            df.WindowState = FormWindowState.Maximized;
            df.Text        = "Red - f(x) | Olive - df(x)/dx | Green - d²f(x)/dx²";

            df.AddGraphic(new DoubleFunction(f), a, b, DrawModes.DrawLines,
                          Color.Red);
            df.AddGraphic(new DoubleFunction(deriv_f), a, b, DrawModes.DrawLines,
                          Color.Olive);
            df.AddGraphic(new DoubleFunction(deriv2_f), a, b, DrawModes.DrawLines,
                          Color.Green);
            df.Show();
            //DrawAllGraphics();
        }
Beispiel #3
0
            public static double Muller(DoubleFunction f,
                                        double p1, double p2, double p3, double eps,
                                        ref DekartForm dForm, bool silent)
            {
                double p0;
                int    stepsMaden = 0;

                Parabola parabola = new Parabola(p1, f(p1), p2, f(p2), p3, f(p3));

                dForm.AddGraphic(parabola.Eval,
                                 (float)(double)Min(p1, p2, p3),
                                 (float)(double)Max(p1, p2, p3),
                                 DrawModes.DrawLines, Color.Red);

                do
                {
                    p0 = p1;
                    p1 = p2;
                    p2 = p3;
                    p3 = NextRoot(f, p0, p1, p2);

                    stepsMaden++;

                    parabola = new Parabola(p1, f(p1), p2, f(p2), p3, f(p3));

                    dForm.AddGraphic(parabola.Eval,
                                     (float)(double)Min(p1, p2, p3),
                                     (float)(double)Max(p1, p2, p3),
                                     DrawModes.DrawLines, Color.Red);

                    if (stepsMaden % 100 == 0)
                    {
                        if (silent)
                        {
                            return(double.NaN);
                        }

                        if (MessageBox.Show("Performed " + stepsMaden.ToString()
                                            + " steps, continue?", "Разошлось наверно?",
                                            MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
                                            MessageBoxDefaultButton.Button1) != DialogResult.OK)
                        {
                            return(double.NaN);
                        }
                    }
                }while ((Math.Abs(p3 - p2) >= eps) &&
                        (Math.Abs(f(p3)) > eps));

                return(p3);
            }
Beispiel #4
0
        private void RunWave_Click(object sender, EventArgs e)
        {
            DekartForm dForm = new DekartForm(150, 150, 50, 175);

            dForm.Size = new Size(800, 420);
            dForm.Text = "f(x,t) = |cos(t)|*sin(4x)";

            dForm.Show();

            int tmpID = -1;

            for (t = 0; !dForm.IsDisposed; t += 0.1f)
            {
                if (t > +2 * Math.PI)
                {
                    t -= 2 * (float)Math.PI;
                }

                if (tmpID >= 0)
                {
                    dForm.RemoveGraphic(tmpID);
                }
                tmpID = dForm.AddGraphic(fun, -5, 5,
                                         DrawModes.DrawLines, Color.Red);

                dForm.Update2();                //.DrawAllGraphics();
                Application.DoEvents();
            }
        }
Beispiel #5
0
        private void FindRoot()
        {
            double          res   = double.NaN;
            List <PointF[]> lines = null;

            #region prepare dform
            if (dForm == null)
            {
                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.Size          = new Size(750, 600);
                dForm.Use_IsVisible = false;
                dForm.toolStripContainer1.ContentPanel.MouseUp +=
                    new MouseEventHandler(dForm_MouseClick);
                dForm.FormClosed += new FormClosedEventHandler(delegate(object s, FormClosedEventArgs eva)
                {
                    ReadPolynom();
                    textBoxFx.Text = poly1.ToString();
                    dForm          = null;
                });
            }
            else
            {
                dForm.RemoveAllGraphics();
            }

            #endregion

            dForm.AddGraphic(f, x1, x2, DrawModes.DrawLines, Color.Green);
            dForm.Show();
            dForm.Update2();

            res      = NewtoneRafson(f, df, p0, eps, out lines, false);
            lastRoot = res;

            #region Print results

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

            dForm.Update2();

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

            listRoots.Items.Add("x" + (listRoots.Items.Count + 1) + " = "
                                + res.ToString("F16"));
            listY.Items.Add("y" + (listY.Items.Count + 1) + " = "
                            + poly1.Evaluate(res).ToString("F16"));
            #endregion
        }
Beispiel #6
0
        private void task11ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetDelta();
            DekartForm df = new DekartForm(30, 30, 100, 200);

            df.AddGraphic(new DoubleFunction(f11), -0.7f, 10f, DrawModes.DrawPoints,
                          Color.SpringGreen);
            df.Show2();
        }
Beispiel #7
0
        private void fxItem_Click(object sender, EventArgs e)
        {
            DekartForm dForm = new DekartForm(75, 75, 320, 300);

            dForm.Size = new Size(700, 600);
            dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
            dForm.Use_IsVisible = false;
            dForm.Show();
            dForm.Update2();
        }
Beispiel #8
0
        private void buttonChe_Click(object sender, EventArgs e)
        {
            try
            {
                a = float.Parse(textBoxA.Text);
                b = float.Parse(textBoxB.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Parsing error, aborted.");
                return;
            }

            n = (int)numericN.Value;
            float h = (b - a) / n;

            givenPoints = new PointF[n + 1];

            // fill table givenPoints with known values
            for (int i = 0; i <= n; i++)
            {
                double ksi = Math.Cos(Math.PI * (2 * i + 1) / (2 * n + 2));
                double xi  = (a + b) / 2.0 - (b - a) / 2.0 * ksi;
                givenPoints[i].X = (float)xi;
                givenPoints[i].Y = (float)f(givenPoints[i].X);
            }

            DekartForm df = new DekartForm(150, 150, 50, 430);

            df.Size = new Size(440, 540);
            df.Text = "n = " + n.ToString() + " | Red - error | " +
                      "Olive - Chebyshev's polynom | Green - f(x)";

            df.AddGraphic(new DoubleFunction(lagrange_err), a, b, DrawModes.DrawLines,
                          Color.Red);
            df.AddGraphic(new DoubleFunction(lagrange), a, b, DrawModes.DrawLines,
                          Color.Olive);
            df.AddGraphic(new DoubleFunction(f), a, b, DrawModes.DrawPoints,
                          Color.Green);
            df.Show();            //.DrawAllGraphics();
        }
Beispiel #9
0
        private void buttonNewtone_Click(object sender, EventArgs e)
        {
            try
            {
                a = float.Parse(textBoxA.Text);
                b = float.Parse(textBoxB.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Parsing error, aborted.");
                return;
            }

            n = (int)numericN.Value;
            float h = (b - a) / n;

            givenPoints = new PointF[n + 1];

            // fill table givenPoints with known values
            for (int i = 0; i <= n; i++)
            {
                givenPoints[i].X = a + i * h;
                givenPoints[i].Y = (float)f(givenPoints[i].X);
            }

            DekartForm df = new DekartForm(150, 150, 50, 430);

            df.Size = new Size(440, 540);
            df.Text = "n = " + n.ToString() + " | Red - error | " +
                      "Olive - Newton's interpolation polynom | Green - f(x)";

            df.AddGraphic(new DoubleFunction(newtone_err), a, b, DrawModes.DrawLines,
                          Color.Red);
            df.AddGraphic(new DoubleFunction(newtone), a, b, DrawModes.DrawLines,
                          Color.Olive);
            df.AddGraphic(new DoubleFunction(f), a, b, DrawModes.DrawPoints,
                          Color.Green);
            df.Show();            //.DrawAllGraphics();
        }
Beispiel #10
0
        private void AutoFindRoots()
        {
            float  x1, x2;
            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;
            }

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

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

            float step = (x2 - x1) / 100f;

            DekartForm dForm;

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

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

            roots = new List <double>();

            for (float x = x1; x < x2; x += step)
            {
                double          res   = double.NaN;
                List <PointF[]> lines = null;

                switch (selectedSolMeth)
                {
                    #region Method Still Point
                case SolutionMethod.StillPoint:
                    if (dForm.CountGraphics == 0)
                    {
                        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(h, x, eps, out lines, true);
                    break;
                    #endregion

                    #region Method Bisection
                case SolutionMethod.Bisection:
                    if (dForm.CountGraphics == 0)
                    {
                        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
                    }

                    res = FindRoot.Bisection(h, x, x + step, eps, out lines, true);
                    break;
                    #endregion

                    //#region Method Newtone-Rafson
                    //case SolutionMethod.NewtoneRafson:
                    //    MessageBox.Show("How to evaluate dh/dx?");
                    //    return;
                    //    if ( dForm.CountGraphics == 0 )

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

                    //    res = FindRoot.NewtoneRafson(f, df, x, eps, out lines);
                    //    break;

                    //#endregion

                    #region Method Cuttings
                case SolutionMethod.Cuttings:
                    if (dForm.CountGraphics == 0)
                    {
                        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
                    }
                    res = FindRoot.Cuttings(h, x, x + step, eps, out lines, true);
                    break;
                    #endregion

                    //#region Method Hord
                    //case SolutionMethod.Hord:
                    //    if ( dForm.CountGraphics == 0 )
                    //        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
                    //    res = FindRoot.Hord(f, d2f, x, x + step, eps, out lines);
                    //    break;
                    //#endregion

                    #region Method Muller
                case SolutionMethod.Muller:
                    if (dForm.CountGraphics == 0)
                    {
                        dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green);
                    }

                    res   = FindRoot.Muller(h, x - step, x, x + step, eps, ref dForm, true);
                    lines = null;
                    break;

                    #endregion
                default:
                    return;
                }

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

                if (double.IsNaN(res))
                {
                    continue;
                }
                if ((res > x2) || (res < x1))
                {
                    continue;
                }

                roots.Add(res);
            }
            roots.Sort();
            foreach (double p in roots)
            {
                listRoots.Items.Add("x" + (listRoots.Items.Count + 1) + " = "
                                    + p.ToString("F16"));
                listY.Items.Add("y" + (listY.Items.Count + 1) + " = "
                                + f(p).ToString("F16"));
            }

            dForm.Show();
            dForm.Update2();
        }
Beispiel #11
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
        }
Beispiel #12
0
        private void buttonSolve_Click(object sender, EventArgs e)
        {
            double x0, y0;
            double eps;
            bool   speedUp;

            #region get parameters from form
            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;
            }

            speedUp = checkSpeedUp.Checked;
            #endregion

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

            switch (selectedSolMeth)
            {
                #region Method Simple Iteration

            case SolutionMethod.StillPoint:

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

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

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

                dForm.AddGraphic(f1x, f1yS, -5f, 5f, DrawModes.DrawLines, Color.Green);
                dForm.AddGraphic(f2xS, f2y, -5f, 5f, DrawModes.DrawLines, Color.Blue);
                res = FindSysRoot.StillPointMethod(
                    new double[] { x0, y0 },
                    new DoubleFunction[] { g1S, g2S },
                    eps, out lines, speedUp, false);
                break;
                #endregion

                #region Method Newtone

            case SolutionMethod.Newtone:

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

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

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

                dForm.AddGraphic(f1x, f1y, -5f, 5f, DrawModes.DrawLines, Color.Green);
                dForm.AddGraphic(f2x, f2y, -5f, 5f, DrawModes.DrawLines, Color.Blue);

                DoubleMultiDimFunction[,] J = new DoubleMultiDimFunction[2, 2];
                J[0, 0] = df1x;
                J[0, 1] = df1y;
                J[1, 0] = df2x;
                J[1, 1] = df2y;

                DoubleMultiDimFunction[] F = new DoubleMultiDimFunction[2];
                F[0] = f1;
                F[1] = f2;

                res = FindSysRoot.Newtone(F, J,
                                          new double[] { x0, y0 }, eps, out lines, false);
                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[0]) || double.IsNaN(res[1]))
            {
                MessageBox.Show("Корни не найдены.");
                return;
            }

            listRoots.Items.Add("x" + (listRoots.Items.Count + 1) + " = "
                                + res[0].ToString("F16"));
            listY.Items.Add("y" + (listY.Items.Count + 1) + " = "
                            + res[1].ToString("F16"));
            #endregion
        }
Beispiel #13
0
        private void buttonDraw_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            bool ok = true;

            try
            {
                x1 = float.Parse(textBox1.Text);
            }
            catch (FormatException)
            {
                errorProvider1.SetError(textBox1, "Неверный формат вещественного числа");
                ok = false;
            }

            try
            {
                x2 = float.Parse(textBox2.Text);
            }
            catch (FormatException)
            {
                errorProvider1.SetError(textBox2, "Неверный формат вещественного числа");
                ok = false;
            }

            if (!ok)
            {
                return;
            }

            if (x1 > x2)
            {
                float t = x1;
                x1 = x2;
                x2 = t;

                textBox1.Text = x1.ToString();
                textBox2.Text = x2.ToString();
            }

            listBox1.Items.Add(string.Format("Интервал: [{0}; {1}]", x1, x2));

            DekartForm df = new DekartForm(50, 50, 30, 150);

            df.Text = "y ≈ sin(x)";
            df.AddGraphic(new DoubleFunction(delegate(double x)
            {
                double a, sum = x;
                uint n        = 2;
                a             = x;
                do
                {
                    a   *= -x * x / n / (n + 1);
                    sum += a;
                    n   += 2;
                }while (Math.Abs(a) >= eps);

                listBox1.Items.Add("x = " + x.ToString("f3") + ", n = " + n.ToString());
                return(sum);
            }), x1, x2, DrawModes.DrawPoints, Color.Green);

            df.Show();
            df.Update2();
        }
Beispiel #14
0
        private void FindRoot()
        {
            Complex res = Complex.NaN;

            #region prepare dform
            if (dForm == null)
            {
                dForm               = new DekartForm(100, 100, 300, 300);
                dForm.ClientSize    = new Size(600, 600);
                dForm.Use_IsVisible = false;
                dForm.FormClosed   += new FormClosedEventHandler(delegate(object s, FormClosedEventArgs eva)
                {
                    dForm = null;
                });
            }
            else
            {
                dForm.RemoveAllGraphics();
            }

            #endregion

            List <Root>   roots  = new List <Root>();
            List <PointF> pts    = new List <PointF>();
            List <Color>  colors = new List <Color>();

            Color[] baseColors = new Color[] {
                Color.Red,
                Color.Orange,
                Color.Yellow,
                Color.Green,
                Color.LightBlue,
                Color.Blue,
                Color.Violet
            };

            double y1 = x1, y2 = x2;

            DoubleOfVectorFunction[] dovf = new DoubleOfVectorFunction[] { f1, f2 };
            Complex c = new Complex(0);

            for (double y = y1; y < y2; y += hy)
            {
                for (double x = x1; x < x2; x += hx)
                {
                    int   iterations;
                    float percent;
                    c.re = x;
                    c.im = y;
                    res  = Newtone(dovf, c, eps, out iterations);

                    bool inRange = true;
                    inRange &= res.re > x1;
                    inRange &= res.re < x2;
                    inRange &= res.im > y1;
                    inRange &= res.im < y2;


                    if (Complex.IsNaN(res) || !inRange)
                    {
                        colors.Add(Color.Black);
                        pts.Add(new PointF((float)x, (float)y));
                    }
                    else
                    {
                        if (roots.Count == 0)
                        {
                            Root newRoot;
                            newRoot.color = baseColors[roots.Count % baseColors.Length];
                            newRoot.value = res;
                            roots.Add(newRoot);
                        }

                        percent = iterations / 15f;
                        if (percent > 1f)
                        {
                            percent = 1f;
                        }

                        bool needAdd = true;
                        foreach (Root r in roots)
                        {
                            if (Near(r.value, res))
                            {
                                colors.Add(ColorModifer.Brightness(r.color, percent));
                                pts.Add(new PointF((float)x, (float)y));

                                needAdd = false;
                                break;
                            }
                        }

                        if (needAdd)
                        {
                            Root newRoot;
                            newRoot.color = baseColors[roots.Count % baseColors.Length];
                            newRoot.value = res;
                            roots.Add(newRoot);

                            colors.Add(ColorModifer.Brightness(newRoot.color, percent));
                            pts.Add(new PointF((float)x, (float)y));
                        }
                    }
                }
            }

            DotGraphic dotGraphic = new DotGraphic(pts.ToArray(), colors.ToArray());
            dotGraphic.CurrentColorSchema = new MathGraphic.ColorSchema(
                Color.Black, Color.DimGray, Color.DimGray, Color.Gray);
            dForm.AddGraphic(dotGraphic);

            MathGraphic mg;
            mg = new MathGraphic(Color.White, DrawModes.DrawLines, f1x, f1y,
                                 -5f, 5f, 0.01f);
            dForm.AddGraphic(mg);

            mg = new MathGraphic(Color.White, DrawModes.DrawLines, f2x, f2y,
                                 -5f, 5f, 0.01f);
            dForm.AddGraphic(mg);

            dForm.Show();
            dForm.Update2();

            foreach (Root z in roots)
            {
                listRoots.Items.Add("z" + (listRoots.Items.Count + 1) + " = "
                                    + z.value.ToString("F6"));
                listY.Items.Add("f,g" + (listY.Items.Count + 1) + " = "
                                + f1(z.value.re, z.value.im).ToString("F6") + ", "
                                + f2(z.value.re, z.value.im).ToString("F6"));
            }
        }
Beispiel #15
0
        private void buttonSplines_Click(object sender, EventArgs e)
        {
            try
            {
                a = float.Parse(textBoxA.Text);
                b = float.Parse(textBoxB.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Parsing error, aborted.");
                return;
            }

            n = (int)numericN.Value;
            float h = (b - a) / n;

            givenPoints = new PointF[n + 1];

            // fill table givenPoints with known values
            for (int i = 0; i <= n; i++)
            {
                givenPoints[i].X = a + i * h;
                givenPoints[i].Y = (float)f(givenPoints[i].X);
            }

            double[] P = new double[n + 1];
            double[] q = new double[n + 1];
            m = new double[n + 1];

            double[] _a = new double[n];
            double[] _b = new double[n];
            double[] _c = new double[n];
            double[] _v = new double[n];

            m[0]  = m[n] = 0;
            _b[0] = 2 * (H(0) + H(1));

            for (int k = 0; k < n; k++)
            {
                _a[k] = H(k);
                if (k > 0)
                {
                    _b[k] = 2 * (H(k - 1) + H(k));
                }
                if (k < n - 1)
                {
                    _c[k] = H(k + 1);
                }
                if (k > 0)
                {
                    _v[k] = 6 * (D(k) - D(k - 1));
                }
            }

            P[n - 1] = givenPoints[n - 1].Y / H(n - 1);
            q[n - 1] = givenPoints[n].Y / H(n - 1);

            for (int k = n - 1; k > 0; k--)
            {
                P[k - 1] = -_a[k] / (_b[k] + _c[k] * P[k]);
                q[k - 1] = (_v[k] - _c[k] * q[k]) / (_b[k] + _c[k] * P[k]);
            }

            m[1] = (_v[1] - _c[1] * q[1]) / (_b[1] + _c[1] * P[1]);

            for (int k = 1; k < n - 1; k++)
            {
                m[k + 1] = P[k] * m[k] + q[k];
            }

            DekartForm df = new DekartForm(150, 150, 50, 430);

            df.Size = new Size(440, 540);
            df.Text = "n = " + n.ToString() + " | Red - error | " +
                      "Olive - Splines (natural), Green - f(x)";

            df.AddGraphic(new DoubleFunction(splines_err), a, b, DrawModes.DrawLines,
                          Color.Red);
            df.AddGraphic(new DoubleFunction(splines), a, b, DrawModes.DrawLines,
                          Color.Olive);
            df.AddGraphic(new DoubleFunction(f), a, b, DrawModes.DrawPoints,
                          Color.Green);
            df.Show();            //.DrawAllGraphics();
        }
Beispiel #16
0
        private void tool_f_Click(object sender, EventArgs e)
        {
            try
            {
                a = float.Parse(textBoxA.Text);
                b = float.Parse(textBoxB.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Parsing error, aborted.");
                return;
            }

            if ((!checkBoxReuse.Checked) || (dForm == null))
            {
                dForm      = new DekartForm(100, 100, 50, 400);
                dForm.Size = new Size(440, 540);
            }
            else
            {
                dForm.RemoveAllGraphics();
            }

            dForm.Text = "Green - f(x)";
            dForm.AddGraphic(new DoubleFunction(f), a, b, DrawModes.DrawLines,
                             Color.Green);
            dForm.Show();
        }