# NMath User's Guide

13.3 Differentiation (.NET, C#, CSharp, VB, Visual Basic, F#)

The Differentiate() method on OneVariableFunction (Section 13.1) computes the derivative of a function at a given x-value. For example, if f is OneVariableFunction, this code estimates the derivative at 0:

Code Example – C# calculus

```double d = f.Differentiate( 0 );
```

Code Example – VB calculus

```Dim D As Double = F.Differentiate(0)
```

NOTE—Class Polynomial provides a method for constructing the exact derivative of a polynomial. See Section 13.4 for more information.

To perform differentiation, every OneVariableFunction has an object associated with it. NMath provides class , 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.

The default for a OneVariableFunction is an instance of . To achieve more control over how differentiation is performed, you can instantiate your own . For instance, this code uses the Tolerance property to set the error tolerance to a non-default value, and the MaximumOrder property to set the maximum order, then calls the Differentiate() method to differentiate function f at π:

Code Example – C# calculus

```var ridders = new RiddersDifferentiator();
```
```ridders.Tolerance = 1e-6;
```
```ridders.MaximumOrder = 20;
```
```double d = ridders.Differentiate( f, Math.PI );
```

Code Example – VB calculus

```Dim Ridders As New RiddersDifferentiator()
```
```Ridders.Tolerance = "1e-6"
```
```Ridders.MaximumOrder = 20
```
```Dim D As Double = Ridders.Differentiate(F, Math.PI)
```

Setting the error tolerance to a value less than zero ensures that the Ridders differentiation is of the maximum order:

Code Example – C# calculus

```var ridders = new RiddersDifferentiator();
```
```ridders.Tolerance = -1;
```
```double d = ridders.Differentiate( f, 1 );
```

Code Example – VB calculus

```Dim Ridders As New RiddersDifferentiator()
```
```Ridders.Tolerance = -1
```
```Dim D As Double = Ridders.Differentiate(F, 1)
```

Read-only properties are provided for accessing information about a derivative approximation, once it has been computed:

ErrorEstimate gets an estimate of the error of the derivative just computed.

Order gets the order of the final polynomial extrapolation.

ToleranceMet gets a boolean value indicating whether or not the error estimate for the derivative approximation is less than the tolerance.

Tableau 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.

For instance, this code checks whether the error tolerance was met before proceeding:

Code Example – C# calculus

```var ridders = new RiddersDifferentiator();
```
```double d = ridders.Differentiate( f, Math.PI );

```

```if ( ridders.ToleranceMet ) {
```
```  // Do something here...
```

}

Code Example – VB calculus

```Dim Ridders As New RiddersDifferentiator()
```
```Dim D As Double = ridders.Differentiate(F, Math.PI)

```

```If Ridders.ToleranceMet Then
```
```  ' Do something here...
```
```End If
```

Top

Top