/home/jaubrey/webwork//pg_TRUNK/macros/PGnumericalmacros.pl

From WeBWorK

Jump to: navigation


NAME

        Numerical methods for the PG language

SYNPOSIS

DESCRIPTION

Interpolation methods

Plotting a list of points (piecewise linear interpolation)

    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);

Horner polynomial/ Newton polynomial

        Usege:  $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), ... using Horner's method.

Hermite polynomials

        Usage:  $poly = hermit([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.

Hermite splines

        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

Cubic spline approximation

    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,
                          );

The string contains

        <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);

Numerical Integration methods

Integration by Left Hand 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.

Integration by Right Hand 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.

Integration by Midpoint rule

        Usage:  midpoint(function_reference, start, end, steps=>30 );

Implements the Midpoint 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.

Integration by Simpson's rule

        Usage:  simpson(function_reference, start, end, steps=>30 );

Implements Simpson's 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, but must be even.

Integration by trapezoid rule

        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.

Romberg method of integration

        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.