Update the Changes file for 0.21 and improve .gitignore
[Math-GSL.git] / examples / deriv / basic
blob055e7d4c3830b8b7d22c4b8f58a595e18b35f8c6
1 #!/usr/bin/perl -w
2 use strict;
3 use Math::GSL::Deriv qw/:all/;
4 use Math::GSL::Errno qw/:all/;
6 # This is a numerical verification of the mathematical fact
7 # that the derivative of sin(x) is cos(x), i.e.
9 # d
10 # -( sin(x) ) = cos(x)
11 # dx
13 my ($x, $h) = (1.5, 0.01);
14 my ($status, $val,$err) = gsl_deriv_central ( sub { sin($_[0]) }, $x, $h);
15 my $res = abs($val - cos($x));
16 if ($status == $GSL_SUCCESS) {
17 printf "deriv(sin((%g)) = %.18g, max error=%.18g\n", $x, $val, $err;
18 printf " cos(%g)) = %.18g, residue= %.18g\n" , $x, cos($x), $res;
19 } else {
20 my $gsl_error = gsl_strerror($status);
21 print "Numerical Derivative FAILED, reason:\n $gsl_error\n\n";