Extreme Optimization™: Complexity made simple.

Math and Statistics
Libraries for .NET

  • Home
  • Features
    • Math Library
    • Vector and Matrix Library
    • Statistics Library
    • Performance
    • Usability
  • Documentation
    • Introduction
    • Math Library User's Guide
    • Vector and Matrix Library User's Guide
    • Data Analysis Library User's Guide
    • Statistics Library User's Guide
    • Reference
  • Resources
    • Downloads
    • QuickStart Samples
    • Sample Applications
    • Frequently Asked Questions
    • Technical Support
  • Order
  • Company
    • About us
    • Testimonials
    • Customers
    • Press Releases
    • Careers
    • Partners
    • Contact us
Introduction
Deployment Guide
Nuget packages
Configuration
Using Parallelism
Expand Mathematics Library User's GuideMathematics Library User's Guide
Expand Vector and Matrix Library User's GuideVector and Matrix Library User's Guide
Expand Data Analysis Library User's GuideData Analysis Library User's Guide
Expand Statistics Library User's GuideStatistics Library User's Guide
Expand Data Access Library User's GuideData Access Library User's Guide
Expand ReferenceReference

Skip Navigation LinksHome»Documentation»Mathematics Library User's Guide»Solving Equations»Methods that use the Derivative

Methods that use the Derivative

Extreme Optimization Numerical Libraries for .NET Professional

Newton devised a method to solve equations using values of the function and its first derivative. It is implemented by the NewtonRaphsonSolver class.

The NewtonRaphsonSolver class has two constructors. The first takes no arguments. The function to solve and its derivative must be set using the TargetFunction and DerivativeOfTargetFunction properties, which are both FuncT, TResult delegates. The initial guess must be set using the InitialGuess property.

The second constructor takes these three properties as its three parameters.

C#
Copy
// Use the parameterless constructor:
NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
// Then set the target function and its derivative:
solver.TargetFunction = Math.Sin;
solver.DerivativeOfTargetFunction = Math.Cos;
// and the initial guess:
solver.InitialGuess = 4;
// Or specify all three directly in the constructor:
NewtonRaphsonSolver solver2 = new NewtonRaphsonSolver(Math.Sin, Math.Cos, 4);

The iteration can be controlled as outlined before by setting the AbsoluteTolerance, RelativeTolerance, ConvergenceCriterion, MaxIterations properties. The Solve method calculates the zero of the target function:

C#
Copy
Console.WriteLine("Newton-Raphson Solver: sin(x) = 0");
Console.WriteLine("  Initial guess: 4");
double result = solver.Solve();
Console.WriteLine("  Result: {0}", solver.Status);
Console.WriteLine("  Solution: {0}", solver.Result);
Console.WriteLine("  Estimated error: {0}", solver.EstimatedError);
Console.WriteLine("  # iterations: {0}", solver.IterationsNeeded);
When you don't have the derivative...

You can still use this class if you don't have the derivative of the target function. In this case, use the static GetNumericalDifferentiator method of the FunctionMath class to create a FuncT, TResult delegate that represents the numerical derivative of the target function.

It should be noted, however, that this technique requires significantly more function evaluations. If it is possible to find an interval that contains the root, the preferred method is to use one of the root bracketing solvers from the previous section.

A better alternative, if the function is simple enough to express in a single expression, is to use automatic differentiation to have the derivative calculated for you. To do this, you pass a lambda expression that evaluates the function to the GetDerivative(ExpressionFuncDouble, Double) method, which also creates a FuncT, TResult delegate.

Both techniques are illustrated below:

C#
VB
C++
F#
Copy
solver.TargetFunction = Special.BesselJ0;
var derivative1 = FunctionMath.GetNumericalDifferentiator(Special.BesselJ0);
var derivative2 = SymbolicMath.GetDerivative(x => Special.BesselJ0(x));
solver.DerivativeOfTargetFunction = derivative2;
solver.InitialGuess = 5;
Console.WriteLine("Zero of Bessel function near x=5:");
result = solver.Solve();
Console.WriteLine("  Solution: {0}", solver.Result);
Console.WriteLine("  # iterations: {0}", solver.IterationsNeeded);
solver.TargetFunction = AddressOf Special.BesselJ0
Dim derivative1 =
    FunctionMath.GetNumericalDifferentiator(AddressOf Special.BesselJ0)
Dim derivative2 =
    SymbolicMath.GetDerivative(Function(x) Special.BesselJ0(x))
solver.DerivativeOfTargetFunction = derivative2
solver.InitialGuess = 5
Console.WriteLine("Zero of Bessel function near x=5:")
result = solver.Solve()
Console.WriteLine("  Solution: {0}", solver.Result)
Console.WriteLine("  # iterations: {0}", solver.IterationsNeeded)

No code example is currently available or this language may not be supported.

solver.TargetFunction <- Func<_,_>(Special.BesselJ0)
let derivative1 = 
    FunctionMath.GetNumericalDifferentiator(Func<_,_>(Special.BesselJ0))
let derivative2 = SymbolicMath.GetDerivative(fun x -> Special.BesselJ0(x))
solver.DerivativeOfTargetFunction <- derivative2
solver.InitialGuess <- 5.0
Console.WriteLine("Zero of Bessel function near x=5:")
let result = solver.Solve()
Console.WriteLine("  Solution: 0", solver.Result)
Console.WriteLine("  # iterations: 0", solver.IterationsNeeded)
Reference

Newton, I. Methodus fluxionum et serierum infinitarum. 1664-1671.

Copyright (c) 2004-2023 ExoAnalytics Inc.

Send comments on this topic to support@extremeoptimization.com

Copyright © 2004-2023, Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual Studio.NET, and the Optimized for Visual Studio logo
are registered trademarks of Microsoft Corporation.