## File: Numerical-Differentiation-Examples.html

package info (click to toggle)
gsl-ref-html 2.3-1
• area: non-free
• in suites: bullseye, buster, sid
• size: 6,876 kB
• ctags: 4,574
• sloc: makefile: 35
 file content (133 lines) | stat: -rw-r--r-- 5,311 bytes parent folder | download
 `123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133` `````` GNU Scientific Library – Reference Manual: Numerical Differentiation Examples

29.2 Examples

The following code estimates the derivative of the function f(x) = x^{3/2} at x=2 and at x=0. The function f(x) is undefined for x<0 so the derivative at x=0 is computed using gsl_deriv_forward.

#include <stdio.h> #include <gsl/gsl_math.h> #include <gsl/gsl_deriv.h>  double f (double x, void * params) {   (void)(params); /* avoid unused parameter warning */   return pow (x, 1.5); }  int main (void) {   gsl_function F;   double result, abserr;    F.function = &f;   F.params = 0;    printf ("f(x) = x^(3/2)\n");    gsl_deriv_central (&F, 2.0, 1e-8, &result, &abserr);   printf ("x = 2.0\n");   printf ("f'(x) = %.10f +/- %.10f\n", result, abserr);   printf ("exact = %.10f\n\n", 1.5 * sqrt(2.0));    gsl_deriv_forward (&F, 0.0, 1e-8, &result, &abserr);   printf ("x = 0.0\n");   printf ("f'(x) = %.10f +/- %.10f\n", result, abserr);   printf ("exact = %.10f\n", 0.0);    return 0; }

Here is the output of the program,

\$ ./a.out
f(x) = x^(3/2) x = 2.0 f'(x) = 2.1213203120 +/- 0.0000005006 exact = 2.1213203436  x = 0.0 f'(x) = 0.0000000160 +/- 0.0000000339 exact = 0.0000000000
``````