File: Deriv.pod

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 (103 lines) | stat: -rw-r--r-- 3,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
%perlcode %{
@EXPORT_OK = qw/
               gsl_deriv_central
               gsl_deriv_backward
               gsl_deriv_forward
             /;
%EXPORT_TAGS = ( all => [ @EXPORT_OK ] );

__END__

=encoding utf8

=head1 NAME

Math::GSL::Deriv - Numerical Derivatives

=head1 SYNOPSIS

    use Math::GSL::Deriv qw/:all/;
    use Math::GSL::Errno qw/:all/;

    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";
    }


=head1 DESCRIPTION

This module allows you to take the numerical derivative of a Perl subroutine. To find
a numerical derivative you must also specify a point to evaluate the derivative and a
"step size". The step size is a knob that you can turn to get a more finely or coarse
grained approximation. As the step size $h goes to zero, the formal definition of a
derivative is reached, but in practive you must choose a reasonable step size to get
a reasonable answer. Usually something in the range of 1/10 to 1/10000 is sufficient.

So long as your function returns a single scalar value, you can differentiate as
complicated a function as your heart desires.

=over

=item * C<gsl_deriv_central($function, $x, $h)>

    use Math::GSL::Deriv qw/gsl_deriv_central/;
    my ($x, $h) = (1.5, 0.01);
    sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };

    my ($status, $val,$err) = gsl_deriv_central ( \&func , $x, $h);

This method approximates the central difference of the subroutine reference
$function, evaluated at $x, with "step size" $h. This means that the
function is evaluated at $x-$h and $x+h.


=item * C<gsl_deriv_backward($function, $x, $h)>

    use Math::GSL::Deriv qw/gsl_deriv_backward/;
    my ($x, $h) = (1.5, 0.01);
    sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };

    my ($status, $val,$err) = gsl_deriv_backward ( \&func , $x, $h);

This method approximates the backward difference of the subroutine
reference $function, evaluated at $x, with "step size" $h. This means that
the function is evaluated at $x-$h and $x.

=item * C<gsl_deriv_forward($function, $x, $h)>

    use Math::GSL::Deriv qw/gsl_deriv_forward/;
    my ($x, $h) = (1.5, 0.01);
    sub func { my $x=shift; $x**4 - 15 * $x + sqrt($x) };

    my ($status, $val,$err) = gsl_deriv_forward ( \&func , $x, $h);

This method approximates the forward difference of the subroutine reference
$function, evaluated at $x, with "step size" $h. This means that the
function is evaluated at $x and $x+$h.

=back

For more information on the functions, we refer you to the GSL official
documentation: L<http://www.gnu.org/software/gsl/manual/html_node/>

=head1 AUTHORS

Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

%}