7 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
13 Math::GSL::Deriv - Numerical Derivatives
17 use Math::GSL::Deriv qw/:all/;
18 use Math::GSL::Errno qw/:all/;
20 my ($x, $h) = (1.5, 0.01);
21 my ($status, $val,$err) = gsl_deriv_central ( sub { sin($_[0]) }, $x, $h);
22 my $res = abs($val - cos($x));
23 if ($status == $GSL_SUCCESS) {
24 printf "deriv(sin((%g)) = %.18g, max error=%.18g\n", $x, $val, $err;
25 printf " cos(%g)) = %.18g, residue= %.18g\n" , $x, cos($x), $res;
27 my $gsl_error = gsl_strerror($status);
28 print "Numerical Derivative FAILED, reason:\n $gsl_error\n\n";
34 This module allows you to take the numerical derivative of a Perl subroutine. To find
35 a numerical derivative you must also specify a point to evaluate the derivative and a
36 "step size". The step size is a knob that you can turn to get a more finely or coarse
37 grained approximation. As the step size $h goes to zero, the formal definition of a
38 derivative is reached, but in practive you must choose a reasonable step size to get
39 a reasonable answer. Usually something in the range of 1/10 to 1/10000 is sufficient.
41 So long as your function returns a single scalar value, you can differentiate as
42 complicated a function as your heart desires.
46 =item * C<gsl_deriv_central($function, $x, $h)>
48 use Math::GSL::Deriv qw/gsl_deriv_central/;
49 my ($x, $h) = (1.5, 0.01);
50 sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };
52 my ($status, $val,$err) = gsl_deriv_central ( \&func , $x, $h);
54 This method approximates the central difference of the subroutine reference
55 $function, evaluated at $x, with "step size" $h. This means that the
56 function is evaluated at $x-$h and $x+h.
59 =item * C<gsl_deriv_backward($function, $x, $h)>
61 use Math::GSL::Deriv qw/gsl_deriv_backward/;
62 my ($x, $h) = (1.5, 0.01);
63 sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };
65 my ($status, $val,$err) = gsl_deriv_backward ( \&func , $x, $h);
67 This method approximates the backward difference of the subroutine
68 reference $function, evaluated at $x, with "step size" $h. This means that
69 the function is evaluated at $x-$h and $x.
71 =item * C<gsl_deriv_forward($function, $x, $h)>
73 use Math::GSL::Deriv qw/gsl_deriv_forward/;
74 my ($x, $h) = (1.5, 0.01);
75 sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };
77 my ($status, $val,$err) = gsl_deriv_forward ( \&func , $x, $h);
79 This method approximates the forward difference of the subroutine reference
80 $function, evaluated at $x, with "step size" $h. This means that the
81 function is evaluated at $x and $x+$h.
85 For more informations on the functions, we refer you to the GSL offcial
86 documentation: L<http://www.gnu.org/software/gsl/manual/html_node/>
90 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
92 =head1 COPYRIGHT AND LICENSE
94 Copyright (C) 2008 Jonathan Leto and Thierry Moisan
96 This program is free software; you can redistribute it and/or modify it
97 under the same terms as Perl itself.