Пример #1
0
        public string TestEquationGeneration(bool useCompiledEquation = false)
        {
            string result = "";

            foreach (float frequency in frequencies)
            {
                float  hz        = (float)(frequency);
                string equations = acGraph.EquationGeneration(frequency);
                result += equations + Environment.NewLine;
                if (useCompiledEquation)
                {
                    Compiler compiler = new Compiler();
                    NonlinearEquationDescription compiledEquation = compiler.CompileEquations(equations);
                    result += compiledEquation.PrintVariables() + Environment.NewLine;
                    result += compiledEquation.PrintEquations() + Environment.NewLine;
                    result += compiledEquation.PrintJacobiMatrix() + Environment.NewLine;
                }
                result += Environment.NewLine;
            }
            return(result);
        }
Пример #2
0
        static public void TestNonlinearEquationParser()
        {
            //create file

            /*
             * x*x+2=e^x*sin(x);
             * x(0)=0;
             * x(0)=2;
             * solution at approx 1.4 and 2.33
             *
             * 6x^5+-3x^4+7x^3+2x^2+-5x+7.13=0.
             * 1 root at -0.963
             */
            string equation1 = @"constant a = 2;x*x+a=e()^x*sin(x);x(0)=0;";
            string equation2 = @"x*x+2=e()^x*sin(x);x(0)=2;";
            string equation3 = @"x*x+2=e()^x*sin(x);x(0)=5;";
            string equation4 = @"6*x^5+-3*x^4+7*x^3+2*x^2+-5*x+7.13=0.;x(0)=0;";

            Stream       StdoutStream = Console.OpenStandardOutput();
            StreamWriter Stdout       = new StreamWriter(StdoutStream);

            Stdout.WriteLine("\tTest nonlinear equation parser");
            try
            {
                Equations.Nonlinear.Compiler compiler         = new Equations.Nonlinear.Compiler();
                NonlinearEquationDescription compiledEquation = compiler.CompileEquations(equation1);
                Stdout.WriteLine("Equation 1: x=0");
                Stdout.WriteLine("x*x+2-e^x*sin(x)");
                Stdout.WriteLine("Derivative: 2x-e^x*sin(x)-e^x*cos(x)");
                Stdout.WriteLine(compiledEquation.PrintVariables());
                Stdout.WriteLine(compiledEquation.PrintEquations());
                Stdout.WriteLine(compiledEquation.PrintJacobiMatrix());
                compiledEquation = compiler.CompileEquations(equation2);
                Stdout.WriteLine("Equation 2 x=2");
                Stdout.WriteLine(compiledEquation.PrintVariables());
                Stdout.WriteLine(compiledEquation.PrintEquations());
                Stdout.WriteLine(compiledEquation.PrintJacobiMatrix());
                compiledEquation = compiler.CompileEquations(equation3);
                Stdout.WriteLine("Equation 3 x=5");
                Stdout.WriteLine(compiledEquation.PrintVariables());
                Stdout.WriteLine(compiledEquation.PrintEquations());
                Stdout.WriteLine(compiledEquation.PrintJacobiMatrix());
                compiledEquation = compiler.CompileEquations(equation4);
                Stdout.WriteLine("Equation 4 x=0");
                Stdout.WriteLine(compiledEquation.PrintVariables());
                Stdout.WriteLine(compiledEquation.PrintEquations());
                Stdout.WriteLine(compiledEquation.PrintJacobiMatrix());
            }
            catch (CompilerException exc)
            {
                Stdout.WriteLine(exc.Message);
                var errors = exc.Errors;
                foreach (var error in errors)
                {
                    Stdout.WriteLine(error.Message + " Line: " + error.Line + " Position: " + error.Position);
                }
            }
            catch (Exception exc)
            {
                Stdout.WriteLine(exc.Message);
            }
            Stdout.Flush();
            Stdout.Close();
        }