The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Math::Derivative - Numeric 1st and 2nd order differentiation

SYNOPSIS

use Math::Derivative qw(:all);

@dydx = forwarddiff(\@x, \@y);

@dydx = centraldiff(\@x, \@y);

@dydx = Derivative1(\@x, \@y);     # A synonym for centraldiff()

@d2ydx2 = Derivative2(\@x, \@y, $yd0, $ydn);

DESCRIPTION

This Perl package exports functions that numerically approximate first and second order differentiation on vectors of data. The accuracy of the approximation will depend upon the differences between the successive values in the X array.

FUNCTIONS

The functions may be imported by name or by using the tag ":all".

forwarddiff()

@dydx = forwarddiff(\@x, \@y);

Take the references to two arrays containing the x and y ordinates of the data, and return an array of approximate first derivatives at the given x ordinates, using the forward difference approximation.

The last term is actually formed using a backward difference formula, there being no array item to subtract from at the end of the array. If you want to use derivatives strictly formed from the forward difference formula, use only the values from [0 .. #y-1], e.g.:

@dydx = (forwarddiff(\@x, \@y))[0 .. $#y-1];

or, more simply,

@dydx = forwarddiff(\@x, \@y);
pop @dydx;

centraldiff()

@dydx = centraldiff(\@x, \@y);

Take the references to two arrays containing the x and y ordinates of the data, and return an array of approximate first derivatives at the given x ordinates.

The algorithm used three data points to calculate the derivative, except at the end points, where by necessity the forward difference algorithm is used instead. If you want to use derivatives strictly formed from the central difference formula, use only the values from [1 .. #y-1], e.g.:

@dydx = (centraldiff(\@x, \@y))[1 .. $#y-1];

Derivative2()

@d2ydx2 = Derivative2(\@x, \@y);

or

@d2ydx2 = Derivative2(\@x, \@y, $yp0, $ypn);

Take references to two arrays containing the x and y ordinates of the data and return an array of approximate second derivatives at the given x ordinates.

You may optionally give values to use as the first derivatives at the start and end points of the data. If you don't, first derivative values will be assumed to be zero.

Derivative1()

A synonym for centraldiff().

REFERENCES

http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/central-differences/

http://www.robots.ox.ac.uk/~sjrob/Teaching/EngComp/ecl6.pdf

AUTHOR

John A.R. Williams J.A.R.Williams@aston.ac.uk

John M. Gamble jgamble@cpan.org (current maintainer)