diff --git a/src/Numerics/RootFinding/Bisection.cs b/src/Numerics/RootFinding/Bisection.cs
index 4a7fe984..6a7b3acb 100644
--- a/src/Numerics/RootFinding/Bisection.cs
+++ b/src/Numerics/RootFinding/Bisection.cs
@@ -41,7 +41,7 @@ namespace MathNet.Numerics.RootFinding
/// The function to find roots from.
/// Guess for the low value of the range where the root is supposed to be. Will be expanded if needed.
/// Guess for the high value of the range where the root is supposed to be. Will be expanded if needed.
- /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8.
+ /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8. Must be greater than 0.
/// Maximum number of iterations. Default 100.
/// Factor at which to expand the bounds, if needed. Default 1.6.
/// Maximum number of expand iterations. Default 100.
@@ -49,6 +49,11 @@ namespace MathNet.Numerics.RootFinding
///
public static double FindRootExpand(Func f, double guessLowerBound, double guessUpperBound, double accuracy = 1e-8, int maxIterations = 100, double expandFactor = 1.6, int maxExpandIteratons = 100)
{
+ if (accuracy <= 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(accuracy), "Must be greater than zero.");
+ }
+
ZeroCrossingBracketing.ExpandReduce(f, ref guessLowerBound, ref guessUpperBound, expandFactor, maxExpandIteratons, maxExpandIteratons*10);
return FindRoot(f, guessLowerBound, guessUpperBound, accuracy, maxIterations);
}
@@ -57,12 +62,17 @@ namespace MathNet.Numerics.RootFinding
/// The function to find roots from.
/// The low value of the range where the root is supposed to be.
/// The high value of the range where the root is supposed to be.
- /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8.
+ /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8. Must be greater than 0.
/// Maximum number of iterations. Default 100.
/// Returns the root with the specified accuracy.
///
public static double FindRoot(Func f, double lowerBound, double upperBound, double accuracy = 1e-14, int maxIterations = 100)
{
+ if (accuracy <= 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(accuracy), "Must be greater than zero.");
+ }
+
double root;
if (TryFindRoot(f, lowerBound, upperBound, accuracy, maxIterations, out root))
{
@@ -76,12 +86,17 @@ namespace MathNet.Numerics.RootFinding
/// The function to find roots from.
/// The low value of the range where the root is supposed to be.
/// The high value of the range where the root is supposed to be.
- /// Desired accuracy for both the root and the function value at the root. The root will be refined until the accuracy or the maximum number of iterations is reached.
+ /// Desired accuracy for both the root and the function value at the root. The root will be refined until the accuracy or the maximum number of iterations is reached. Must be greater than 0.
/// Maximum number of iterations. Usually 100.
/// The root that was found, if any. Undefined if the function returns false.
/// True if a root with the specified accuracy was found, else false.
public static bool TryFindRoot(Func f, double lowerBound, double upperBound, double accuracy, int maxIterations, out double root)
{
+ if (accuracy <= 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(accuracy), "Must be greater than zero.");
+ }
+
if (upperBound < lowerBound)
{
var t = upperBound;
diff --git a/src/Numerics/RootFinding/Brent.cs b/src/Numerics/RootFinding/Brent.cs
index aa493f91..74ab28a5 100644
--- a/src/Numerics/RootFinding/Brent.cs
+++ b/src/Numerics/RootFinding/Brent.cs
@@ -42,7 +42,7 @@ namespace MathNet.Numerics.RootFinding
/// The function to find roots from.
/// Guess for the low value of the range where the root is supposed to be. Will be expanded if needed.
/// Guess for the high value of the range where the root is supposed to be. Will be expanded if needed.
- /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8.
+ /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8. Must be greater than 0.
/// Maximum number of iterations. Default 100.
/// Factor at which to expand the bounds, if needed. Default 1.6.
/// Maximum number of expand iterations. Default 100.
@@ -58,12 +58,17 @@ namespace MathNet.Numerics.RootFinding
/// The function to find roots from.
/// The low value of the range where the root is supposed to be.
/// The high value of the range where the root is supposed to be.
- /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8.
+ /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Default 1e-8. Must be greater than 0.
/// Maximum number of iterations. Default 100.
/// Returns the root with the specified accuracy.
///
public static double FindRoot(Func f, double lowerBound, double upperBound, double accuracy = 1e-8, int maxIterations = 100)
{
+ if (accuracy <= 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(accuracy), "Must be greater than zero.");
+ }
+
double root;
if (TryFindRoot(f, lowerBound, upperBound, accuracy, maxIterations, out root))
{
@@ -77,12 +82,17 @@ namespace MathNet.Numerics.RootFinding
/// The function to find roots from.
/// The low value of the range where the root is supposed to be.
/// The high value of the range where the root is supposed to be.
- /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached.
+ /// Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Must be greater than 0.
/// Maximum number of iterations. Usually 100.
/// The root that was found, if any. Undefined if the function returns false.
/// True if a root with the specified accuracy was found, else false.
public static bool TryFindRoot(Func f, double lowerBound, double upperBound, double accuracy, int maxIterations, out double root)
{
+ if (accuracy <= 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(accuracy), "Must be greater than zero.");
+ }
+
double fmin = f(lowerBound);
double fmax = f(upperBound);
double froot = fmax;