/// <summary> /// Plots input function. /// </summary> /// <param name="_input"></param> public static void Plot(Input _input) { //If input function is not complete throw an error. if (_input.Args.Length == 0) { throw new Errors.MissingFunctionException(); } //Create a new function base with input function. FunctionBaseCore FB = CodeBaseCore.CreateFunction(_input.Args[0]); //Execute function across given bounds. NFunctionalEqCore FE = new NFunctionalEqCore(FB, FunctionBounds.CreateBounds(_input.Args), false); //Plot. FE.NPlot(); }
/// <summary> /// Finds the zeros of a function using a brute force approach. /// </summary> /// <param name="_input">Input string.</param> public static void NumericalZeros(Input _input) { int tol = 100; //Throws an error if input is not correct if (_input.Args.Length == 0) { throw new Errors.MissingFunctionException(); } //Evaluates the function FunctionBaseCore FB = CodeBaseCore.CreateFunction(_input.Args[0]); NFunctionalEqCore FE = new NFunctionalEqCore(FB, FunctionBounds.CreateBounds(_input.Args), false); //Gets tolerance level if defined if (_input.Modifiers.Length > 0) { foreach (string mods in _input.Modifiers) { if (mods.StartsWith("-t:")) { tol = Convert.ToInt32(mods.Remove(0, 3)); } } } //Continues process until tolerance is reached for (int i = 1; i < tol; i++) { //Numerically evaluates the function across the boundary conditions List<List<float>> curve = FE.evalFlat(); List<float> min = curve[0]; //Finds the point which is closest to zero foreach (List<float> x in curve.Skip(1)) { min = CompareZero(x, min); } //To be completed... } }
/// <summary> /// Finds the zeros of a function using the bisector method. /// </summary> /// <param name="_input">Input string.</param> public static void BisectorZeros(Input _input) { int maxiter = 100; float tol = 10e-4f; int i = 0; bool found = false; bool printeach = false; //Throws an error if input is not correct if (_input.Args.Length == 0) { throw new Errors.MissingFunctionException(); } //Gets tolerance level if defined if (_input.Modifiers.Length > 0) { foreach (string mods in _input.Modifiers) { if (mods.StartsWith("-n:")) { maxiter = Convert.ToInt32(mods.Remove(0, 3)); } if (mods.StartsWith("-t:")) { tol = (float)Convert.ToDouble(mods.Remove(0, 3)); } if (mods.StartsWith("-p")) { printeach = true; } } } //Creates a function to evaluate with specific boundary conditions FunctionBaseCore FB = CodeBaseCore.CreateFunction(_input.Args[0]); FunctionBounds Bounds = FunctionBounds.CreateBounds(_input.Args); //Continues process until tolerance is reached while ((i < maxiter) & (found == false)) { //Evaluates the function at each bound and the mid point. float abnd = FB.GetReturnFloat(Bounds.GetMaxBounds()); float bbnd = FB.GetReturnFloat(Bounds.GetMinBounds()); //Checks the initial boundary conditions if (i == 0) CheckBounds(abnd, bbnd); float mid = FB.GetReturnFloat(Bounds.GetMidPoint()); if (printeach == true) { PrintEach(i, abnd, bbnd, mid, Bounds.GetMidPoint()); } //checks all three points for a zero. found = CheckPoint(abnd, tol, Bounds.GetMaxBounds(), found); found = CheckPoint(bbnd, tol, Bounds.GetMinBounds(), found); found = CheckPoint(mid, tol, Bounds.GetMidPoint(), found); //replaces the boundary condition Bounds = GetNewBounds(abnd, bbnd, mid, Bounds); //increase counter i++; } }
/// <summary> /// Input loop. /// </summary> static void PollInput(bool benchOn) { System.Diagnostics.Stopwatch sWatch = new System.Diagnostics.Stopwatch(); if (benchOn == true) sWatch.Start(); try { Console.WriteLine(); Input input = new Input(Console.ReadLine()); Console.WriteLine(); if (input.Command == "Clr") { Console.Clear(); } else if (input.Command == "Plot") { PlotCore.Plot(input); } else if (input.Command == "BiZero") { SolverCore.BisectorZeros(input); } else if (input.Command == "/?") { UtilCore.HelpFunc("Functions"); } else if (input.Command == "NInt") { CalcCore.NInt(input); } else if (input.Raw.EndsWith("?")) { UtilCore.HelpFunc(input.Raw.Substring(0, input.Raw.Length - 1)); } else if (input.Raw == "MaSS -b") { if (benchOn == false) benchOn = true; else benchOn = false; Console.WriteLine("Benchmark debugging: " + benchOn); } else { RunCode(input.Raw); //Console.WriteLine("Function " + input.Split(' ')[0] + " is not a valid MaSS function."); } } catch (Exception ex) { Console.WriteLine(ex.TargetSite); Console.WriteLine(ex.Message); } finally { if (benchOn == true) { sWatch.Stop(); Console.WriteLine("Command runtime benchmark: " + sWatch.Elapsed.ToString()); } sWatch.Reset(); PollInput(benchOn); } }
//Numerical integration of N variable function public static void NInt(Input _input) { //Diagnostic testing System.Diagnostics.Stopwatch ST = new System.Diagnostics.Stopwatch(); ST.Start(); float tol = 0; //Gets tolerance level if defined if (_input.Modifiers.Length > 0) { foreach (string mods in _input.Modifiers) { if (mods.StartsWith("-t:")) { tol = (float)Convert.ToDouble(mods.Remove(0, 3)); } } } //if input is bad throw error and exit. if (_input.Args.Length == 0) { throw new Errors.MissingFunctionException(); } //create new function base and respective bounds based on input FunctionBaseCore FB = CodeBaseCore.CreateFunction(_input.Args[0]); FunctionBounds Bounds = FunctionBounds.CreateBounds(_input.Args.Take(_input.Args.Length).ToArray()); //gets base integration value based on input float baseval = NIntCore(Bounds, FB); //performs tolerance check, reintegrates in necessary int cycles = 1; if (tol > 0) { Bounds = ReduceBounds(Bounds); float testval = NIntCore(Bounds, FB); while (Math.Abs(baseval - testval) > tol) { Bounds = ReduceBounds(Bounds); baseval = testval; testval = NIntCore(Bounds, FB); cycles++; } } //outputs value with diagnostic information Console.WriteLine("Full integral found to: " + baseval); Console.WriteLine("Within a tolerance of: " + tol); string sbnd = ""; for (int i = 0; i < Bounds.BoundsList.Count; i++) { if (i == (Bounds.BoundsList.Count - 1)) { sbnd += Bounds.BoundsList[i].Step; } else { sbnd += Bounds.BoundsList[i].Step + ", "; } } Console.WriteLine("With a stepsize of: " + sbnd); Console.WriteLine("After " + cycles + " cycles"); ST.Stop(); Console.WriteLine("Taking a total time of: " + ST.Elapsed.ToString()); }