Numerical Differentiation

The functions described in this chapter compute numerical derivatives by finite differencing. An adaptive algorithm is used to find the best choice of finite difference and to estimate the error in the derivative.

Contentes:

  1. Deriv methods

  2. Diff methods

Deriv methods (for GSL 1.4.90 or later)

Numerical derivatives should now be calculated using the GSL::Deriv.forward, GSL::Deriv.central and GSL::Deriv.backward methods, which accept a step-size argument in addition to the position x. The original GSL::Diff methods (without the step-size) are deprecated.




Diff Methods (obsolete)




Example

#!/usr/bin/env ruby
require "gsl"

f = GSL::Function.alloc { |x|
  pow(x, 1.5)
}

printf ("f(x) = x^(3/2)\n");

x = 2.0
h = 1e-8
result, abserr = f.deriv_central(x, h)
printf("x = 2.0\n");
printf("f'(x) = %.10f +/- %.10f\n", result, abserr);
printf("exact = %.10f\n\n", 1.5 * Math::sqrt(2.0));

x = 0.0
result, abserr = Deriv.forward(f, x, h) # equivalent to f.deriv_forward(x, h)
printf("x = 0.0\n");
printf("f'(x) = %.10f +/- %.10f\n", result, abserr);
printf("exact = %.10f\n", 0.0);

The results are

f(x) = x^(3/2)
x = 2.0
f'(x) = 2.1213203120 +/- 0.0000004064
exact = 2.1213203436

x = 0.0
f'(x) = 0.0000000160 +/- 0.0000000339
exact = 0.0000000000

prev next

Reference index top