Hi Gregory,

Here's a program that shows fitpoly1d in action. You may also be
interested in the subroutine pcoeff, which evaluates $coeffs for an
arbitrary iinput piddle of values.

Cheers,

Matt


#!/usr/bin/perl
#===============================================================================
# test_fitpoly1d
#===============================================================================
use strict;
use warnings;

use PDL;
use PDL::Fit::Polynomial;

use PDL::Graphics::PGPLOT;

dev("/xs",{Color=>1,AxisColour=>1, Linewidth=>1, WindowWidth=>8, Aspect=>1.0});

my $x = zeroes(20)->xlinvals(-1,5);

my $y = pow($x,2) + grandom($x)/5;

env (-1, 4, -1, 10);

# here's a parabola with some noise added to it

points $x, $y,{COLOR=>"YELLOW"};

# fit the data and return the coefficients and best y fit

my $order = 3;

my ($yfit, $coeffs) = fitpoly1d $x, $y, $order;


hold;

# green points are $yfit, which are the evaluation of $coeffs at $x

points $x, $yfit,{COLOR=>"GREEN"};

# if you want to evaluate the $coeffs at an arbitrary set of points, try
# this routine:

my $x_new = zeroes(100)->xlinvals(-1,5);


my $y_new = pcoeff($x_new, $coeffs);


line $x_new, $y_new, {COLOR=>"RED"};


sub pcoeff {
    # pcoeff - take an N-D array of values, and a 1D set ofpolynomial
coefficients
    #          from fitpoly1d and return an N-D array with the calculated values
    use strict;
    my ($vals, $coeff) = @_;

    # here is the x**0, x**1, x**2,.....
    my $pow = $vals->dummy(0)**xvals($coeff);
    # here is the C_0 , C_1, C_2, ....
    my $a = $vals->dummy(0)*$coeff;
    # result is C_0 x**0 + C_1 * x**1 + ....
    my $result = ($pow*$coeff)->sumover;

    return($result);
}

_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to