public void Pole() { Func <double, double> f1 = x => 1 / (x - 2) + 2; Func <double, double> df1 = x => - 1 / (x * x - 4 * x + 4); Assert.AreEqual(1.5, HybridNewtonRaphson.FindRoot(f1, df1, 1, 2, 1e-14, 100, 20)); Assert.AreEqual(1.5, HybridNewtonRaphson.FindRoot(f1, df1, 1, 6, 1e-14, 100, 20)); Assert.AreEqual(1.5, RealRoots.OfFunctionAndDerivative(f1, df1, 1, 6)); Func <double, double> f2 = x => - 1 / (x - 2) + 2; Func <double, double> df2 = x => 1 / (x * x - 4 * x + 4); Assert.AreEqual(2.5, HybridNewtonRaphson.FindRoot(f2, df2, 2, 3, 1e-14, 100, 20)); Assert.AreEqual(2.5, HybridNewtonRaphson.FindRoot(f2, df2, -2, 3, 1e-14, 100, 20)); Assert.AreEqual(2.5, RealRoots.OfFunctionAndDerivative(f2, df2, -2, 3)); Func <double, double> f3 = x => 1 / (x - 2) + x + 2; Func <double, double> df3 = x => - 1 / (x * x - 4 * x + 4) + 1; Assert.AreEqual(-Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f3, df3, -2, -1, 1e-14, 100, 20), 1e-14); Assert.AreEqual(Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f3, df3, 1, 1.99, 1e-14, 100, 20)); Assert.AreEqual(Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f3, df3, -1.5, 1.99, 1e-14, 100, 20)); Assert.AreEqual(Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f3, df3, 1, 6, 1e-14, 100, 20)); Func <double, double> f4 = x => 1 / (2 - x) - x + 6; Func <double, double> df4 = x => 1 / (x * x - 4 * x + 4) - 1; Assert.AreEqual(4 + Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f4, df4, 5, 6, 1e-14, 100, 20), 1e-14); Assert.AreEqual(4 - Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f4, df4, 2.01, 3, 1e-14, 100, 20)); Assert.AreEqual(4 - Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f4, df4, 2.01, 5, 1e-14, 100, 20)); Assert.AreEqual(4 - Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f4, df4, -2, 4, 1e-14, 100, 20)); }
public void GetX_Test() { // Two real roots. double expectedRoot = -0.044555558333472335; double actualRoot = RealRoots.GetX(15, 68, 3); Assert.AreEqual(expectedRoot, actualRoot); // Two Real roots. double expectedRoot1 = -1.5417080848653129; double actualRoot1 = RealRoots.GetX(1, 45, 67); Assert.AreEqual(expectedRoot1, actualRoot1); // Two complex roots. Return -1 double expectedRoot2 = -1; double actualRoot2 = RealRoots.GetX(13, 24, 15); Assert.AreEqual(expectedRoot2, actualRoot2); // One real root. double expectedRoot3 = -2; double actualRoot3 = RealRoots.GetX(1, 4, 4); Assert.AreEqual(expectedRoot3, actualRoot3); // Not a valid quadratic equation. Return -1 double expectedRoot4 = -1; double actualRoot4 = RealRoots.GetX(0, 24, 17); Assert.AreEqual(expectedRoot4, actualRoot4); }