| 1 | # functions for calculating derivatives of data | ||
| 2 | # $Id: Derivative.pm,v 1.1 1995/12/26 16:26:59 willijar Exp $ | ||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Math::Derivative - Numeric 1st and 2nd order differentiation | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Math::Derivative qw(Derivative1 Derivative2); | ||
| 10 | @dydx=Derivative1(\@x,\@y); | ||
| 11 | @d2ydx2=Derivative2(\@x,\@y); | ||
| 12 | @d2ydx2=Derivative2(\@x,\@y,$yp0,$ypn); | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This Perl package exports functions for performing numerical first | ||
| 17 | (B<Derivative1>) and second B<Derivative2>) order differentiation on | ||
| 18 | vectors of data. They both take references to two arrays containing | ||
| 19 | the x and y ordinates of the data and return an array of the 1st or | ||
| 20 | 2nd derivative at the given x ordinates. B<Derivative2> may optionally | ||
| 21 | be given values to use for the first dervivative at the start and end | ||
| 22 | points of the data - otherwiswe 'natural' values are used. | ||
| 23 | |||
| 24 | =head1 HISTORY | ||
| 25 | |||
| 26 | $Log: Derivative.pm,v $ | ||
| 27 | Revision 1.1 1995/12/26 16:26:59 willijar | ||
| 28 | Initial revision | ||
| 29 | |||
| 30 | |||
| 31 | =head1 BUGS | ||
| 32 | |||
| 33 | Let me know. | ||
| 34 | |||
| 35 | =head1 AUTHOR | ||
| 36 | |||
| 37 | John A.R. Williams <J.A.R.Williams@aston.ac.uk> | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | require Exporter; | ||
| 42 | package Math::Derivative; | ||
| 43 | @ISA=qw(Exporter); | ||
| 44 | @EXPORT_OK=qw(Derivative1 Derivative2); | ||
| 45 | use strict; | ||
| 46 | |||
| 47 | sub Derivative1 { | ||
| 48 | my ($x,$y)=@_; | ||
| 49 | my @y2; | ||
| 50 | my $n=$#{$x}; | ||
| 51 | $y2[0]=($y->[1]-$y->[0])/($x->[1]-$x->[0]); | ||
| 52 | $y2[$n]=($y->[$n]-$y->[$n-1])/($x->[$n]-$x->[$n-1]); | ||
| 53 | my $i; | ||
| 54 | for($i=1; $i<$n; $i++) { | ||
| 55 | $y2[$i]=($y->[$i+1]-$y->[$i-1])/($x->[$i+1]-$x->[$i-1]); | ||
| 56 | } | ||
| 57 | return @y2; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub Derivative2 { | ||
| 61 | my ($x,$y,$yp1,$ypn)=@_; | ||
| 62 | my $n=$#{$x}; | ||
| 63 | my ($i,@y2,@u); | ||
| 64 | if (!defined $yp1) { | ||
| 65 | $y2[0]=0; $u[0]=0; | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | $y2[0]=-0.5; | ||
| 69 | $u[0]=(3/($x->[1]-$x->[0]))*(($y->[1]-$y->[0])/($x->[1]-$x->[0])-$yp1); | ||
| 70 | } | ||
| 71 | for($i=1; $i<$n; $i++) { | ||
| 72 | my $sig=($x->[$i]-$x->[$i-1])/($x->[$i+1]-$x->[$i-1]); | ||
| 73 | my $p=$sig*$y2[$i-1]+2.0; | ||
| 74 | $y2[$i]=($sig-1.0)/$p; | ||
| 75 | $u[$i]=(6.0*( ($y->[$i+1]-$y->[$i])/($x->[$i+1]-$x->[$i])- | ||
| 76 | ($y->[$i]-$y->[$i-1])/($x->[$i]-$x->[$i-1]) | ||
| 77 | )/ | ||
| 78 | ($x->[$i+1]-$x->[$i-1])-$sig*$u[$i-1])/$p; | ||
| 79 | } | ||
| 80 | my ($qn,$un); | ||
| 81 | if (!defined $ypn) { | ||
| 82 | $qn=0; | ||
| 83 | $un=0; | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | $qn=0.5; | ||
| 87 | $un=(3.0/($x->[$n]-$x->[$n-1]))* | ||
| 88 | ($ypn-($y->[$n]-$y->[$n-1])/($x->[$n]-$x->[$n-1])); | ||
| 89 | } | ||
| 90 | $y2[$n]=($un-$qn*$u[$n-1])/($qn*$y2[$n-1]+1.0); | ||
| 91 | for($i=$n-1; $i>=0; --$i) { | ||
| 92 | $y2[$i]=$y2[$i]*$y2[$i+1]+$u[$i]; | ||
| 93 | } | ||
| 94 | return @y2; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | 1; |