# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.7381586,"math_prob":0.9506568,"size":3314,"snap":"2019-13-2019-22","text_gpt3_token_len":730,"char_repetition_ratio":0.18610272,"word_repetition_ratio":0.14046122,"special_character_ratio":0.20488836,"punctuation_ratio":0.14973262,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9949767,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-05-26T05:18:45Z\",\"WARC-Record-ID\":\"<urn:uuid:5d850cf0-2a77-4d65-8579-a8fc30e9e96e>\",\"Content-Length\":\"19458\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:92c0eb98-d6ce-4ba4-adba-96136c9489bc>\",\"WARC-Concurrent-To\":\"<urn:uuid:35d9ade4-e1ac-46ed-94cb-2b50601fb791>\",\"WARC-IP-Address\":\"72.47.229.209\",\"WARC-Target-URI\":\"https://www.centerspace.net/doc/NMath/user/calculus-80877.htm\",\"WARC-Payload-Digest\":\"sha1:HZR66KMVUUIOPIFAU6G5WIKNAIUWP7Y3\",\"WARC-Block-Digest\":\"sha1:RJFK3VYGGKW6SFUD3D5D22PS6TXJ6FSV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-22/CC-MAIN-2019-22_segments_1558232258849.89_warc_CC-MAIN-20190526045109-20190526071109-00414.warc.gz\"}"}