Numerical methods for the PG language
Usage:
plot_list([x0,y0,x1,y1,...]);
plot_list([(x0,y0),(x1,y1),...]);
plot_list(\x_y_array);
plot_list([x0,x1,x2...], [y0,y1,y2,...]);
plot_list(\@xarray,\@yarray);
It is important that the x values in any form are unique or this method fails. There is no check for this however.
Usage:
$fn = horner([x0,x1,x2, ...],[q0,q1,q2, ...]);
Produces the newton polynomial
&$fn(x) = q0 + q1*(x-x0) +q2*(x-x1)*(x-x0) + ...;
Generates a subroutine which evaluates a polynomial passing through the points (x0,q0), (x1,q1), (x2, q2)
, ... using Horner's method.
The array refs for x
and q
can be any length but must be the same length.
Example
$h = horner([0,1,2],[1,-1,2]);
Then &$h(num)
returns the polynomial at the value num
. For example, &$h(1.5)=1
.
Usage:
$poly = hermite([x0,x1...],[y0,y1...],[yp0,yp1,...]);
Produces a reference to polynomial function with the specified values and first derivatives at (x0,x1,...). &$poly(34)
gives a number
Generates a subroutine which evaluates a polynomial passing through the specified points with the specified derivatives: (x0,y0,yp0) ... The polynomial will be of high degree and may wobble unexpectedly. Use the Hermite splines described below and in Hermite.pm for most graphing purposes.
Example
$h = hermite([0,1],[0,0],[1,-1]);
&$h(num)
will evaluate the hermite polynomial at num
. For example, &$h(0.5)
returns 0.25
.
Usage
$spline = hermit_spline([x0,x1...],[y0,y1...],[yp0,yp1,...]);
Produces a reference to a piecewise cubic hermit spline with the specified values and first derivatives at (x0,x1,...).
&$spline(45)
evaluates to a number.
Generates a subroutine which evaluates a piecewise cubic polynomial passing through the specified points with the specified derivatives: (x0,y0,yp0) ...
An object oriented version of this is defined in Hermite.pm
Usage:
$fun_ref = cubic_spline(~~@x_values, ~~@y_values);
Where the x and y value arrays come from the function to be approximated. The function reference will take a single value x and produce value y.
$y = &$fun_ref($x);
You can also generate javaScript which defines a cubic spline:
$function_string = javaScript_cubic_spline(~~@_x_values, ~~@y_values,
name => 'myfunction1',
llimit => -3,
rlimit => 3,
);
This will return
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function myfunction1(x) {
...etc...
}
</SCRIPT>
and can be placed in the header of the HTML output using
HEADER_TEXT($function_string);
Left Hand Riemann Sum
Usage:
lefthandsum(function_reference, start, end, steps=>30 );
Implements the Left Hand sum using 30 intervals between 'start' and 'end'. The first three arguments are required. The final argument (number of steps) is optional and defaults to 30.
Right Hand Riemann Sum
Usage:
righthandsum(function_reference, start, end, steps=>30 );
Implements the right hand sum using 30 intervals between 'start' and 'end'. The first three arguments are required. The final argument (number of steps) is optional and defaults to 30.
Usage:
midpoint(function_reference, start, end, steps=>30);
Implements the Midpoint rule between 'start' and 'end'. The first three arguments are required. The final argument (number of steps) is optional and defaults to 30.
Usage:
simpson(function_reference, start, end, steps=>30 );
Implements Simpson's rule between 'start' and 'end'. The first three arguments are required. The final argument (number of steps) is optional and defaults to 30, but must be even.
Usage:
trapezoid(function_reference, start, end, steps=>30);
Implements the trapezoid rule using 30 intervals between 'start' and 'end'. The first three arguments are required. The final argument (number of steps) is optional and defaults to 30.
Usage:
romberg(function_reference, x0, x1, level);
Implements the Romberg integration routine through 'level' recursive steps. Level defaults to 6.
Inverse Romberg
Usage:
inv_romberg(function_reference, a, value);
Finds b such that the integral of the function from a to b is equal to value. Assumes that the function is continuous and doesn't take on the zero value. Uses Newton's method of approximating roots of equations, and Romberg to evaluate definite integrals.
Example
Find the value of b such that the integral of e^(-x^2/2)/sqrt(2*pi) from 0 to b is 0.25.
$f = sub { my $x = shift; return exp(-$x*$x/2)/sqrt(4*acos(0));};
$b = inv_romberg($f,0,0.45);
this returns 1.64485362695934
. This is the standard normal curve and this value is the z value for the 90th percentile.
Finds integral curve of a vector field using the 4th order Runge Kutta method by providing the function rungeKutta4
Usage:
rungeKutta4( &vectorField(t,x),%options);
Returns: array ref of points [t,y]
Default %options:
'initial_t' => 1,
'initial_y' => 1,
'dt' => 0.01,
'num_of_points' => 10, # number of reported points
'interior_points' => 5, # number of 'interior' steps between reported points
'debug'