|
|
|
@ -103,15 +103,15 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests |
|
|
|
[Test] |
|
|
|
public void Cubic() |
|
|
|
{ |
|
|
|
// with complex roots (looking for the real root only)
|
|
|
|
Func<double, double> f1 = x => 3*x*x*x + 4*x*x + 5*x + 6; |
|
|
|
Func<double, double> df1 = x => 9*x*x + 8*x + 5; |
|
|
|
// with complex roots (looking for the real root only): 3x^3 + 4x^2 + 5x + 6, derivative 9x^2 + 8x + 5
|
|
|
|
Func<double, double> f1 = x => Evaluate.Polynomial(x, 6, 5, 4, 3); |
|
|
|
Func<double, double> df1 = x => Evaluate.Polynomial(x, 5, 8, 9); |
|
|
|
Assert.AreEqual(-1.265328088928, HybridNewtonRaphson.FindRoot(f1, df1, -2, -1, 1e-10, 100, 20), 1e-6); |
|
|
|
Assert.AreEqual(-1.265328088928, HybridNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-10, 100, 20), 1e-6); |
|
|
|
|
|
|
|
// real roots only
|
|
|
|
Func<double, double> f2 = x => 2*x*x*x + 4*x*x - 50*x + 6; |
|
|
|
Func<double, double> df2 = x => 6*x*x + 8*x - 50; |
|
|
|
// real roots only: 2x^3 + 4x^2 - 50x + 6, derivative 6x^2 + 8x - 50
|
|
|
|
Func<double, double> f2 = x => Evaluate.Polynomial(x, 6, -50, 4, 2); |
|
|
|
Func<double, double> df2 = x => Evaluate.Polynomial(x, -50, 8, 6); |
|
|
|
Assert.AreEqual(-6.1466562197069, HybridNewtonRaphson.FindRoot(f2, df2, -8, -5, 1e-10, 100, 20), 1e-6); |
|
|
|
Assert.AreEqual(0.12124737195841, HybridNewtonRaphson.FindRoot(f2, df2, -1, 1, 1e-10, 100, 20), 1e-6); |
|
|
|
Assert.AreEqual(4.0254088477485, HybridNewtonRaphson.FindRoot(f2, df2, 3, 5, 1e-10, 100, 20), 1e-6); |
|
|
|
|