Extreme Optimization > User's Guide > Mathematics Library > Solving Equations > Methods Using Derivatives

Extreme Optimization User's Guide

User's Guide

Up: Solving Equations Next: Solving Systems of Nonlinear Equations Previous: Root Bracketing Solvers Contents

Methods that use the Derivative

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 parameters. The function to solve and its derivative must be set using the TargetFunction and DerivativeOfTargetFunction properties, which are both RealFunction delegates. The initial guess must be set using the InitialGuess property.

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

C# CopyCode imageCopy Code
RealFunction f = new RealFunction(Math.Sin);
RealFunction df = new RealFunction(Math.Cos);
// Use the parameterless constructor:
NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
// Then set the target function and its derivative:
solver.TargetFunction = f;
solver.DerivativeOfTargetFunction = df;
// and the initial guess:
solver.InitialGuess = 4;
// Or specify all three directly in the constructor:
NewtonRaphsonSolver solver2 = new NewtonRaphsonSolver(f, df, 4);
Visual Basic CopyCode imageCopy Code
Dim f As RealFunction = New RealFunction(AddressOf Math.Sin)
Dim df As RealFunction  = New RealFunction(AddressOf Math.Cos)
' Use the parameterless constructor:
Dim solver As NewtonRaphsonSolver = New NewtonRaphsonSolver()
' Then set the target function and its derivative:
solver.TargetFunction = f
solver.DerivativeOfTargetFunction = df
' and the initial guess:
solver.InitialGuess = 4
' Or specify all three directly in the constructor:
Dim solver2 As NewtonRaphsonSolver = New NewtonRaphsonSolver(f, df, 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# CopyCode imageCopy Code
Console.WriteLine("Newton-Raphson Solver: sin(x) = 0");
Console.WriteLine("  Initial guess: 4");
double result = solver.Solve();
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);
Visual Basic CopyCode imageCopy Code
Console.WriteLine("Newton-Raphson Solver: sin(x) = 0")
Console.WriteLine("  Initial guess: 4")
Dim result As Double = 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 CreateDelegate method of the NumericalDifferentiator class (Extreme.Mathematics.Calculus namespace) to create a RealFunction that represents the numerical derivative of the target function:

C# CopyCode imageCopy Code
solver.TargetFunction = f = new  RealFunction(Bessel.J0);
solver.DerivativeOfTargetFunction = NumericalDifferentiator.CreateDelegate(f);
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);
Visual Basic CopyCode imageCopy Code
solver.TargetFunction = f = New  RealFunction(Bessel.J0)
solver.DerivativeOfTargetFunction = NumericalDifferentiator.CreateDelegate(f)
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)

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.

Reference

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

Up: Solving Equations Next: Solving Systems of Nonlinear Equations Previous: Root Bracketing Solvers Contents

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army