PDL::Polynomial(3pm)

PDL::Polynomial3pm

Routines for fitting with polynomials

Usage

  $x = sequence(20)-10;
  $coeff_orig = cdouble(30,-2,3,-2); # order used in this module
  $y = 30-2*$x+3*$x**2-2*$x**3; # or: $y = polyval($coeff_orig->slice("-1:0"), $x->r2C);
  $y += ($x->grandom - 0.5) * 100;
  ($yfit, $coeff) = fitpoly1d($x,$y,4);
  use PDL::Graphics::Simple;
  $w = pgswin();
  $xi = zeroes(100)->xlinvals(-10,9);
  $yi = polyval($coeff->r2C->slice("-1:0"), $xi->r2C);
  $w->plot(with=>'points',$x,$y,
    with=>'points',$x,$yfit, with=>'line',$xi,$yi);
  $yfit = fitpoly1d $data,2; # Least-squares line fit
  ($yfit, $coeffs) = fitpoly1d $x, $y, 4; # Fit a cubic

Overview

This module contains routines for doing simple polynomial fits to data

Functions

fitpoly1d

Fit 1D polynomials to data using min chi^2 (least squares)

 Usage: ($yfit, [$coeffs]) = fitpoly1d [$xdata], $data, $order, [Options...]
  Signature: (x(n); y(n); [o]yfit(n); [o]coeffs(order))

Uses a standard matrix inversion method to do a least squares/min chi^2 polynomial fit to data. Order=2 is a linear fit (two parameters).

Returns the fitted data and optionally the coefficients (in ascending order of degree, unlike "polyval" in PDL::Math).

One can broadcast over extra dimensions to do multiple fits (except the order can not be broadcasted over - i.e. it must be one fixed scalar number like "4").

The data is normalised internally to avoid overflows (using the mean of the abs value) which are common in large polynomial series but the returned fit, coeffs are in unnormalised units.

  $yfit = fitpoly1d $data,2; # Least-squares line fit
  ($yfit, $coeffs) = fitpoly1d $x, $y, 4; # Fit a cubic
  
  $fitimage = fitpoly1d $image,3  # Fit a quadratic to each row of an image
  
  $myfit = fitpoly1d $line, 2, {Weights => $w}; # Weighted fit
  Options:
     Weights    Weights to use in fit, e.g. 1/$sigma**2 (default=1)

Releases

13 releases since Debian 2.2 (Potato); the text changed 12 times.

  1. 2.2
  2. 3.0
  3. 3.1
  4. 4.0
  5. 5.0
  6. 6.0
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13

Bugs

May not work too well for data with large dynamic range.

Author

This file copyright (C) 1999, Karl Glazebrook ([email protected]). All rights reserved. There is no warranty. You are allowed to redistribute this software documentation under certain conditions. For details, see the file COPYING in the PDL distribution. If this file is separated from the PDL distribution, the copyright notice should be included in the file.

From libpdl-fit-perl 2.100-2 in Debian 13 (Trixie). Parsed by carta from the page's man source.