C# Differentiation Example

← All NMath Code Examples

 

using System;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to create differentiator objects for greater control
  /// over how numerical differentiation is performed.
  /// </summary>
  class DifferentiationExample
  {

    static double MyFunction( double x )
    {
      return Math.Sin( Math.Sqrt( x + 1 ) ) * ( 2 * Math.PI * x );
    }

    static void Main( string[] args )
    {
      Console.WriteLine();

      // As shown in the OneVariableFunctionExample, the Differentiate() method
      // on OneVariableFunction computes the derivative of a function at a
      // given x-value.
      var d = new Func<double, double>( MyFunction );
      OneVariableFunction f = d;
      Console.WriteLine( "Derivative of f at Pi = {0}\n", f.Differentiate( Math.PI ) );

      // To perform differentiation, every OneVariableFunction has an IDifferentiator
      // object associated with it. NMath Core provides class RiddersDifferentiator,
      // which computes the derivative of a given function at a given x-value by
      // Ridders’ method of polynomial extrapolation, and implements the
      // IDifferentiator interface.
      //
      // Extrapolations of higher and higher order are produced. Iteration stops when
      // either the estimated error is less than a specified error tolerance, the error
      // estimate is significantly worse than the previous order, or the maximum order
      // is reached.
      // 
      // To achieve more control over how differentiation is performed, you can
      // instantiate your own RiddersDifferentiator.
      var ridder = new RiddersDifferentiator();

      // The Tolerance property gets and sets the error tolerance used in computing
      // differentiations. 
      ridder.Tolerance = 1e-10;

      // Maximum order gets and sets the maximum order. 
      ridder.MaximumOrder = 20;

      // The Differentiate() method on RiddersDifferentiator accepts a
      // OneVariableFunction and an x-value at which to differentiate.
      Console.WriteLine( "Derivative of f at Pi = {0}", ridder.Differentiate( f, Math.PI ) );

      // The ErrorEstimate property gets an estimate of the error of the derivative
      // just computed.
      Console.WriteLine( "Estimated error = {0}", ridder.ErrorEstimate );

      // ToleranceMet gets a boolean value indicating whether or not the error
      // estimate for the derivative approximation is less than the tolerance.
      if ( ridder.ToleranceMet )
      {
        Console.WriteLine( "The estimate is within the error tolerance." );
      }
      else
      {
        Console.WriteLine( "The estimate is NOT within the error tolerance." );
      }

      // The Order property gets the order of the final polynomial extrapolation.
      Console.WriteLine( "Final order = {0}", ridder.Order );

      // The Tableau property gets a matrix of successive approximations produced
      // while computing the derivative. Successive columns in the matrix contain
      // higher orders of extrapolation; successive rows decreasing step size.
      Console.WriteLine( "Tableau = " );
      Console.WriteLine( ridder.Tableau.ToTabDelimited( "F5" ) );

      // Setting the error tolerance to a value less than zero ensures that the
      // Ridders differentiation is of the maximum order.
      ridder.Tolerance = -1;
      Console.WriteLine( "Derivative of f at Pi = {0}", ridder.Differentiate( f, Math.PI ) );
      Console.WriteLine( "Final order = {0}\n", ridder.Order );

      // Note that higher orders are not necessarily better. In most cases,
      // therefore, youre better off letting the differentiator decide when to
      // stop.

      Console.WriteLine();
      Console.WriteLine( "Press Enter Key" );
      Console.Read();

    }

  }// class

}// namespace

← All NMath Code Examples
Top