File: basic

package info (click to toggle)
libmath-gsl-perl 0.45-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192,156 kB
  • sloc: ansic: 895,524; perl: 24,682; makefile: 12
file content (23 lines) | stat: -rwxr-xr-x 678 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/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";
}