static bool TryScanForCrossingsWithRoots(Func <double, double> f, Func <double, double> df, double lowerBound, double upperBound, double accuracy, int maxIterations, int subdivision, out double root) { var zeroCrossings = ZeroCrossingBracketing.FindIntervalsWithin(f, lowerBound, upperBound, subdivision); foreach (Tuple <double, double> bounds in zeroCrossings) { if (TryFindRoot(f, df, bounds.Item1, bounds.Item2, accuracy, maxIterations, subdivision, out root)) { return(true); } } root = double.NaN; return(false); }
/// <summary>Find a solution of the equation f(x)=0.</summary> /// <param name="f">The function to find roots from.</param> /// <param name="guessLowerBound">Guess for the low value of the range where the root is supposed to be. Will be expanded if needed.</param> /// <param name="guessUpperBound">Guess for the high value of the range where the root is supposed to be. Will be expanded if needed.</param> /// <param name="accuracy">Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8.</param> /// <param name="maxIterations">Maximum number of iterations. Default 100.</param> /// <param name="expandFactor">Factor at which to expand the bounds, if needed. Default 1.6.</param> /// <param name="maxExpandIteratons">Maximum number of expand iterations. Default 100.</param> /// <returns>Returns the root with the specified accuracy.</returns> /// <exception cref="NonConvergenceException"></exception> public static double FindRootExpand(Func <double, double> f, double guessLowerBound, double guessUpperBound, double accuracy = 1e-8, int maxIterations = 100, double expandFactor = 1.6, int maxExpandIteratons = 100) { ZeroCrossingBracketing.Expand(f, ref guessLowerBound, ref guessUpperBound, expandFactor, maxExpandIteratons); return(FindRoot(f, guessLowerBound, guessUpperBound, accuracy, maxIterations)); }