Ejemplo n.º 1
0
        private void go_Click(object sender, EventArgs e)
        {
            result.Clear();
            Parser parser = new Parser();

            double[] interval = new double[2];
            if (SWANNEnabled.Checked == false)
            {
                if (!double.TryParse(a.Text, out interval[0])) throw new Exception("В поле a не число!");
                if (!double.TryParse(b.Text, out interval[1])) throw new Exception("В поле b не число!");
            }
            else interval = SWANN.getInterval(function.Text);

            double E;
             s: double L1 = interval[1] - interval[0];
            result.Text += "\r\nИнтервал: [ " + interval[0] + " ; " + interval[1] + " ]";
            if (!double.TryParse(ex.Text, out E)) throw new Exception("В поле E не число!");
            double Y = (interval[0] + interval[1] - E) / 2 + E / 2;
            double Z = (interval[0] + interval[1] - E) / 2 - E / 2;
            result.Text += "\r\nY: " + Y + " ; Z: " + Z + " ;";

            parser.Evaluate(SWANN.parseFunction(function.Text, Y));
            double y1 = parser.Result;

            parser.Evaluate(SWANN.parseFunction(function.Text, Z));
            double y2 = parser.Result;

            if (y1 <= y2) interval[1] = Z;
            else interval[0] = Y;

            double L = interval[1] - interval[0];
            if (L > L1)
            {
                result.Text += "\r\n<===========================================>\t";
                goto s;
            }
            else result.Text += "\r\nX* =  " + ((interval[0] + interval[1]) / 2) + "\r\n<=============== Конец! ===============>";
        }
Ejemplo n.º 2
0
        public static double[] getInterval(string function)
        {
            Parser parser = new Parser();

            double y1, y2, y3, x0 = 0, t = 0.5;

            parser.Evaluate(parseFunction(function, x0 - t));
            y1 = parser.Result;
            parser.Evaluate(parseFunction(function, x0));
            y2 = parser.Result;
            parser.Evaluate(parseFunction(function, x0 + t));
            y3 = parser.Result;

            if (y1 >= y2 && y2 <= y3) return new double[2] { y1, y3 };

            if (y1 <= y2 && y2 >= y3) throw new Exception("Интервал расходится!");

            double a = 0;
            double b = 0;

            double d;

            if (y1 >= y2 && y2 >= y3)
            {
                d = t;
                a = x0;
                x0 += d;
            }
            else
            {
                d = -t;
                b = x0;
                x0 += d;
            }

            for (int i = 0; true; i++)
            {
                double x1 = x0;
                x0 += Math.Pow(2, i + 1) * d;
                parser.Evaluate(parseFunction(function, x0));
                double f0 = parser.Result;
                parser.Evaluate(parseFunction(function, x1));
                double f1 = parser.Result;

                if (f0 < f1)
                {
                    if (d > 0)
                    {
                        a = x1;
                    }
                    else
                    {
                        b = x1;
                    }
                }
                if (f0 >= f1)
                {
                    if (d > 0)
                    {
                        b = x0;
                    }
                    else
                    {
                        a = x0;
                    }

                    return new double[2] { a, b };
                }
            }
        }