Algorithm-CurveFit
view release on metacpan or search on metacpan
NAME
Algorithm::CurveFit - Nonlinear Least Squares Fitting
SYNOPSIS
use Algorithm::CurveFit;
# Known form of the formula
my $formula = 'c + a * x^2';
my $variable = 'x';
my @xdata = read_file('xdata'); # The data corresponsing to $variable
my @ydata = read_file('ydata'); # The data on the other axis
my @parameters = (
# Name Guess Accuracy
['a', 0.9, 0.00001], # If an iteration introduces smaller
['c', 20, 0.00005], # changes that the accuracy, end.
);
my $max_iter = 100; # maximum iterations
my $square_residual = Algorithm::CurveFit->curve_fit(
formula => $formula, # may be a Math::Symbolic tree instead
params => \@parameters,
variable => $variable,
xdata => \@xdata,
ydata => \@ydata,
maximum_iterations => $max_iter,
);
use Data::Dumper;
print Dumper \@parameters;
# Prints
# $VAR1 = [
# [
# 'a',
# '0.201366784209602',
# '1e-05'
# ],
# [
# 'c',
# '1.94690440147554',
# '5e-05'
# ]
# ];
#
# Real values of the parameters (as demonstrated by noisy input data):
# a = 0.2
# c = 2
DESCRIPTION
"Algorithm::CurveFit" implements a nonlinear least squares curve fitting
algorithm. That means, it fits a curve of known form (sine-like,
exponential, polynomial of degree n, etc.) to a given set of data
points.
For details about the algorithm and its capabilities and flaws, you're
encouraged to read the MathWorld page referenced below. Note, however,
that it is an iterative algorithm that improves the fit with each
iteration until it converges. The following rule of thumb usually holds
true:
* A good guess improves the probability of convergence and the quality
of the fit.
* Increasing the number of free parameters decreases the quality and
convergence speed.
* Make sure that there are no correlated parameters such as in 'a + b *
e^(c+x)'. (The example can be rewritten as 'a + b * e^c * e^x' in
which 'c' and 'b' are basically equivalent parameters.
The curve fitting algorithm is accessed via the 'curve_fit' subroutine.
It requires the following parameters as 'key => value' pairs:
formula
The formula should be a string that can be parsed by Math::Symbolic.
Alternatively, it can be an existing Math::Symbolic tree. Please refer
to the documentation of that module for the syntax.
Evaluation of the formula for a specific value of the variable
(X-Data) and the parameters (see below) should yield the associated
Y-Data value in case of perfect fit.
variable
The 'variable' is the variable in the formula that will be replaced
with the X-Data points for evaluation. If omitted in the call to
"curve_fit", the name 'x' is default. (Hence 'xdata'.)
params
The parameters are the symbols in the formula whose value is varied by
( run in 0.494 second using v1.01-cache-2.11-cpan-524268b4103 )