public
Description: Perl interface to the GNU Scientific Library
Homepage: http://leto.net/code/Math-GSL
Clone URL: git://github.com/leto/math--gsl.git
Click here to lend your support to: math--gsl and make a donation at www.pledgie.com !
math--gsl / examples / deriv / basic
100755 24 lines (20 sloc) 0.678 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl -w
use strict;
use Math::GSL::Deriv qw/:all/;
use Math::GSL::Errno qw/:all/;
 
# This is a numerical verification of the mathematical fact
# that the derivative of sin(x) is cos(x), i.e.
#
# d
# -( sin(x) ) = cos(x)
# dx
 
my ($x, $h) = (1.5, 0.01);
my ($status, $val,$err) = gsl_deriv_central ( sub { sin($_[0]) }, $x, $h);
my $res = abs($val - cos($x));
if ($status == $GSL_SUCCESS) {
    printf "deriv(sin((%g)) = %.18g, max error=%.18g\n", $x, $val, $err;
    printf " cos(%g)) = %.18g, residue= %.18g\n" , $x, cos($x), $res;
} else {
    my $gsl_error = gsl_strerror($status);
    print "Numerical Derivative FAILED, reason:\n $gsl_error\n\n";
}