| 1 | package Math::Derivative; | ||||||||||
| 2 | |||||||||||
| 3 | 6 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 6 | our @ISA = qw(Exporter); | |||||||||
| 7 | 6 | our %EXPORT_TAGS = (all => [qw( | |||||||||
| 8 | Derivative1 | ||||||||||
| 9 | Derivative2 | ||||||||||
| 10 | centraldiff | ||||||||||
| 11 | forwarddiff | ||||||||||
| 12 | )]); | ||||||||||
| 13 | |||||||||||
| 14 | 6 | our @EXPORT_OK = (@{$EXPORT_TAGS{all}}); | |||||||||
| 15 | |||||||||||
| 16 | 6 | our $VERSION = 1.01; | |||||||||
| 17 | |||||||||||
| 18 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | =head1 NAME | ||||||||||
| 23 | |||||||||||
| 24 | Math::Derivative - Numeric 1st and 2nd order differentiation | ||||||||||
| 25 | |||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||
| 27 | |||||||||||
| 28 | use Math::Derivative qw(:all); | ||||||||||
| 29 | |||||||||||
| 30 | @dydx = forwarddiff(\@x, \@y); | ||||||||||
| 31 | |||||||||||
| 32 | @dydx = centraldiff(\@x, \@y); | ||||||||||
| 33 | |||||||||||
| 34 | @dydx = Derivative1(\@x, \@y); # A synonym for centraldiff() | ||||||||||
| 35 | |||||||||||
| 36 | @d2ydx2 = Derivative2(\@x, \@y, $yd0, $ydn); | ||||||||||
| 37 | |||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||
| 39 | |||||||||||
| 40 | This Perl package exports functions that numerically approximate first | ||||||||||
| 41 | and second order differentiation on vectors of data. The accuracy of | ||||||||||
| 42 | the approximation will depend upon the differences between the | ||||||||||
| 43 | successive values in the X array. | ||||||||||
| 44 | |||||||||||
| 45 | =head2 FUNCTIONS | ||||||||||
| 46 | |||||||||||
| 47 | The functions may be imported by name or by using the tag ":all". | ||||||||||
| 48 | |||||||||||
| 49 | =head3 forwarddiff() | ||||||||||
| 50 | |||||||||||
| 51 | @dydx = forwarddiff(\@x, \@y); | ||||||||||
| 52 | |||||||||||
| 53 | Take the references to two arrays containing the x and y ordinates of | ||||||||||
| 54 | the data, and return an array of approximate first derivatives at the | ||||||||||
| 55 | given x ordinates, using the forward difference approximation. | ||||||||||
| 56 | |||||||||||
| 57 | The last term is actually formed using a backward difference formula, | ||||||||||
| 58 | there being no array item to subtract from at the end of the array. | ||||||||||
| 59 | If you want to use derivatives strictly formed from the forward | ||||||||||
| 60 | difference formula, use only the values from [0 .. #y-1], e.g.: | ||||||||||
| 61 | |||||||||||
| 62 | @dydx = (forwarddiff(\@x, \@y))[0 .. $#y-1]; | ||||||||||
| 63 | |||||||||||
| 64 | or, more simply, | ||||||||||
| 65 | |||||||||||
| 66 | @dydx = forwarddiff(\@x, \@y); | ||||||||||
| 67 | pop @dydx; | ||||||||||
| 68 | |||||||||||
| 69 | =cut | ||||||||||
| 70 | |||||||||||
| 71 | sub forwarddiff | ||||||||||
| 72 | 1 | ▶ | { | ||||||||
|
Subroutine
forwarddiff: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 73 | 1 | my($x, $y) = @_; | |||||||||
| 74 | 1 | my @y2; | |||||||||
| 75 | 1 | my $n = $#{$x}; | |||||||||
| 76 | |||||||||||
| 77 | 1 | ▶ | croak "X and Y array lengths don't match." unless ($n == $#{$y}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 1 | $y2[$n] = ($y->[$n] - $y->[$n-1])/($x->[$n] - $x->[$n-1]); | |||||||||
| 80 | |||||||||||
| 81 | 1 | for my $i (0 .. $n-1) | |||||||||
| 82 | { | ||||||||||
| 83 | 8 | $y2[$i] = ($y->[$i+1] - $y->[$i])/($x->[$i+1] - $x->[$i]); | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 1 | return @y2; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | =head3 centraldiff() | ||||||||||
| 90 | |||||||||||
| 91 | @dydx = centraldiff(\@x, \@y); | ||||||||||
| 92 | |||||||||||
| 93 | Take the references to two arrays containing the x and y ordinates of | ||||||||||
| 94 | the data, and return an array of approximate first derivatives at the | ||||||||||
| 95 | given x ordinates. | ||||||||||
| 96 | |||||||||||
| 97 | The algorithm used three data points to calculate the derivative, except | ||||||||||
| 98 | at the end points, where by necessity the forward difference algorithm | ||||||||||
| 99 | is used instead. If you want to use derivatives strictly formed from | ||||||||||
| 100 | the central difference formula, use only the values from [1 .. #y-1], | ||||||||||
| 101 | e.g.: | ||||||||||
| 102 | |||||||||||
| 103 | @dydx = (centraldiff(\@x, \@y))[1 .. $#y-1]; | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||
| 106 | |||||||||||
| 107 | sub centraldiff | ||||||||||
| 108 | 3 | ▶ | { | ||||||||
|
Subroutine
centraldiff: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 109 | 3 | my($x, $y) = @_; | |||||||||
| 110 | 3 | my @y2; | |||||||||
| 111 | 3 | my $n = $#{$x}; | |||||||||
| 112 | |||||||||||
| 113 | 3 | ▶ | croak "X and Y array lengths don't match." unless ($n == $#{$y}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | |||||||||||
| 115 | 3 | $y2[0] = ($y->[1] - $y->[0])/($x->[1] - $x->[0]); | |||||||||
| 116 | 3 | $y2[$n] = ($y->[$n] - $y->[$n-1])/($x->[$n] - $x->[$n-1]); | |||||||||
| 117 | |||||||||||
| 118 | 3 | for my $i (1 .. $n-1) | |||||||||
| 119 | { | ||||||||||
| 120 | 12 | $y2[$i] = ($y->[$i+1] - $y->[$i-1])/($x->[$i+1] - $x->[$i-1]); | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 3 | return @y2; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | =head3 Derivative2() | ||||||||||
| 127 | |||||||||||
| 128 | @d2ydx2 = Derivative2(\@x, \@y); | ||||||||||
| 129 | |||||||||||
| 130 | or | ||||||||||
| 131 | |||||||||||
| 132 | @d2ydx2 = Derivative2(\@x, \@y, $yp0, $ypn); | ||||||||||
| 133 | |||||||||||
| 134 | Take references to two arrays containing the x and y ordinates of the | ||||||||||
| 135 | data and return an array of approximate second derivatives at the given | ||||||||||
| 136 | x ordinates. | ||||||||||
| 137 | |||||||||||
| 138 | You may optionally give values to use as the first derivatives at the | ||||||||||
| 139 | start and end points of the data. If you don't, first derivative values | ||||||||||
| 140 | will be assumed to be zero. | ||||||||||
| 141 | |||||||||||
| 142 | =cut | ||||||||||
| 143 | |||||||||||
| 144 | sub seconddx | ||||||||||
| 145 | 1 | ▶ | { | ||||||||
|
Subroutine
seconddx: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||
| 146 | 1 | my($x, $y, $yp1, $ypn) = @_; | |||||||||
| 147 | 1 | my(@y2, @u); | |||||||||
| 148 | 1 | my $n = $#{$x}; | |||||||||
| 149 | |||||||||||
| 150 | 1 | ▶ | croak "X and Y array lengths don't match." unless ($n == $#{$y}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | |||||||||||
| 152 | 1 | ▶ | if (defined $yp1) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | { | ||||||||||
| 154 | 1 | $y2[0] = -0.5; | |||||||||
| 155 | 1 | $u[0] = (3/($x->[1] - $x->[0])) * | |||||||||
| 156 | (($y->[1] - $y->[0])/($x->[1] - $x->[0]) - $yp1); | ||||||||||
| 157 | } | ||||||||||
| 158 | else | ||||||||||
| 159 | { | ||||||||||
| 160 | 0 | $y2[0] = 0; | |||||||||
| 161 | 0 | $u[0] = 0; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 1 | for my $i (1 .. $n-1) | |||||||||
| 165 | { | ||||||||||
| 166 | 23 | my $sig = ($x->[$i] - $x->[$i-1])/($x->[$i+1] - $x->[$i-1]); | |||||||||
| 167 | 23 | my $p = $sig * $y2[$i-1] + 2.0; | |||||||||
| 168 | |||||||||||
| 169 | 23 | $y2[$i] = ($sig - 1.0)/$p; | |||||||||
| 170 | 23 | $u[$i] = (6.0 * ( | |||||||||
| 171 | ($y->[$i+1] - $y->[$i])/($x->[$i+1] - $x->[$i]) - | ||||||||||
| 172 | ($y->[$i] - $y->[$i-1])/($x->[$i] - $x->[$i-1]))/ | ||||||||||
| 173 | ($x->[$i+1] - $x->[$i-1]) - $sig * $u[$i-1])/$p; | ||||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | 1 | ▶ | if (defined $ypn) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | { | ||||||||||
| 178 | 1 | my $qn = 0.5; | |||||||||
| 179 | 1 | my $un = (3.0/($x->[$n]-$x->[$n-1])) * | |||||||||
| 180 | ($ypn - ($y->[$n] - $y->[$n-1])/($x->[$n] - $x->[$n-1])); | ||||||||||
| 181 | 1 | $y2[$n] = ($un - $qn * $u[$n-1])/($qn * $y2[$n-1] + 1.0); | |||||||||
| 182 | } | ||||||||||
| 183 | else | ||||||||||
| 184 | { | ||||||||||
| 185 | 0 | $y2[$n] = 0; | |||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 1 | for my $i (reverse 0 .. $n-1) | |||||||||
| 189 | { | ||||||||||
| 190 | 24 | $y2[$i] = $y2[$i] * $y2[$i+1] + $u[$i]; | |||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | 1 | return @y2; | |||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | =head3 Derivative1() | ||||||||||
| 197 | |||||||||||
| 198 | A synonym for centraldiff(). | ||||||||||
| 199 | |||||||||||
| 200 | =cut | ||||||||||
| 201 | |||||||||||
| 202 | # | ||||||||||
| 203 | # Alias Derivative1() to centraldiff(), and Derivative2() to | ||||||||||
| 204 | # seconddx(), preserving the old names. Not exporting the | ||||||||||
| 205 | # seconddx name now, as I'm not convinced it's a good name. | ||||||||||
| 206 | # | ||||||||||
| 207 | 6 | *Derivative1 = \¢raldiff; | |||||||||
| 208 | 6 | *Derivative2 = \&seconddx; | |||||||||
| 209 | |||||||||||
| 210 | =head1 REFERENCES | ||||||||||
| 211 | |||||||||||
| 212 | L<http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/central-differences/> | ||||||||||
| 213 | |||||||||||
| 214 | L<http://www.robots.ox.ac.uk/~sjrob/Teaching/EngComp/ecl6.pdf> | ||||||||||
| 215 | |||||||||||
| 216 | =head1 AUTHOR | ||||||||||
| 217 | |||||||||||
| 218 | John A.R. Williams B<J.A.R.Williams@aston.ac.uk> | ||||||||||
| 219 | |||||||||||
| 220 | John M. Gamble B<jgamble@cpan.org> (current maintainer) | ||||||||||
| 221 | |||||||||||
| 222 | =cut | ||||||||||
| 223 | |||||||||||
| 224 | 6 | 1; | |||||||||