static void Multiple_Roots(Expression f, float x0, float tol, int ite)
        {
            var xdict = new Dictionary <string, FloatingPoint> {
                { "x", x0 }
            };

            Expression x   = Expression.Symbol("x");
            Expression df  = Calculus.Differentiate(x, f);
            Expression d2f = Calculus.Differentiate(x, df);

            int   cont     = 0;
            float absError = tol + 1;
            float relError;
            float x1;

            float fx          = (float)Evaluate.Evaluate(xdict, f).RealValue;
            float dfx         = (float)Evaluate.Evaluate(xdict, df).RealValue;
            float d2fx        = (float)Evaluate.Evaluate(xdict, d2f).RealValue;
            float denominator = (float)Math.Pow(dfx, 2) - (fx * d2fx);

            Console.WriteLine("|{0,-4}|{1,-15}|{2,-15}|{3,-15}|{4,-15}|{5,-15}|{6,-15}|", "cont", "x", "fx", "dfx", "d2fx", "AbsError", "RelError");
            Console.WriteLine("|{0,-4}|{1,-15}|{2,-15}|{3,-15}|{4,-15}|{5,-15}|{6,-15}|", cont, x0, fx, dfx, d2fx, "", "");

            while (fx != 0 && absError > tol && denominator != 0 && cont < ite)
            {
                x1         = x0 - ((fx * dfx) / denominator);
                xdict["x"] = x1;

                fx          = (float)Evaluate.Evaluate(xdict, f).RealValue;
                dfx         = (float)Evaluate.Evaluate(xdict, df).RealValue;
                d2fx        = (float)Evaluate.Evaluate(xdict, d2f).RealValue;
                denominator = (float)Math.Pow(dfx, 2) - (fx * d2fx);

                absError = Math.Abs(x1 - x0);
                relError = absError / Math.Abs(x1);
                x0       = x1;
                cont++;
                Console.WriteLine("|{0,-4}|{1,-15}|{2,-15}|{3,-15}|{4,-15}|{5,-15}|{6,-15}|", cont, x0, fx, dfx, d2fx, absError, relError);
            }

            if (fx == 0)
            {
                Console.WriteLine("\n" + x0 + " is a root.");
            }
            else if (absError < tol)
            {
                Console.WriteLine("\n" + x0 + " approximates to the function's root with a tolerance of: " + tol.ToString());
            }
            else if (denominator == 0)
            {
                Console.WriteLine("\n" + x0 + " denominator became 0");
            }
            else
            {
                Console.WriteLine("\n" + "Maximum number of iterations exceeded.");
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            int[]    precios   = { 100, 200, 300 };
            string[] productos = { "vaso", "plato", "cuchara" };
            double   resultado = 0;

            for (int i = 0; i < productos.Length; i++)
            {
                resultado += Calculus.CalcularImpuesto(precios[i]);

                Console.WriteLine("Precio: " + precios[i]);
                Console.WriteLine("resultado: " + resultado);
            }

            Console.ReadKey();
        }