Algorithm-CurveFit
view release on metacpan or search on metacpan
# 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'
...
params => $params,
...
);
The accuracy measure means that if the change of parameters from one
iteration to the next is below each accuracy measure for each
parameter, convergence is assumed and the algorithm stops iterating.
In order to prevent looping forever, you are strongly encouraged to
make use of the accuracy measure (see also: maximum_iterations).
The final set of parameters is not returned from the subroutine but
the parameters are modified in-place. That means the original data
structure will hold the best estimate of the parameters.
xdata
This should be an array reference to an array holding the data for the
variable of the function. (Which defaults to 'x'.)
ydata
This should be an array reference to an array holding the function
values corresponding to the x-values in 'xdata'.
maximum_iterations
Optional parameter to make the process stop after a given number of
iterations. Using the accuracy measure and this option together is
encouraged to prevent the algorithm from going into an endless loop in
some cases.
The subroutine returns the sum of square residuals after the final
iteration as a measure for the quality of the fit.
EXPORT
None by default, but you may choose to export "curve_fit" using the
standard Exporter semantics.
examples/examplefit.pl view on Meta::CPAN
chomp;
my @ary = split ' ';
push @xdata, $ary[0];
push @ydata, $ary[1];
}
my @parameters = (
# Name Guess Accuracy
['b', 10, 0.0001],
['c', 2, 0.0005],
);
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;
print Dumper $square_residual;
lib/Algorithm/CurveFit.pm view on Meta::CPAN
my $ydata = $args{ydata};
confess('Y-Data missing.')
if not defined $ydata
or not ref($ydata) eq 'ARRAY'
or not @$ydata;
confess('Y-Data and X-Data need to have the same number of elements.')
if not @$ydata == @xdata;
my @ydata = @$ydata;
# Max_Iter (optional)
my $max_iter = $args{maximum_iterations};
$max_iter = 0 if not defined $max_iter;
# Add third element (dlamda) to parameter arrays in case they're missing.
foreach my $param (@parameters) {
push @$param, 0 if @$param < 3;
}
# Array holding all first order partial derivatives of the function in respect
# to the parameters in order.
my @derivatives;
lib/Algorithm/CurveFit.pm view on Meta::CPAN
# 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'
lib/Algorithm/CurveFit.pm view on Meta::CPAN
...
params => $params,
...
);
The accuracy measure means that if the change of parameters from one iteration
to the next is below each accuracy measure for each parameter, convergence is
assumed and the algorithm stops iterating.
In order to prevent looping forever, you are strongly encouraged to make use of
the accuracy measure (see also: maximum_iterations).
The final set of parameters is B<not> returned from the subroutine but the
parameters are modified in-place. That means the original data structure will
hold the best estimate of the parameters.
=item xdata
This should be an array reference to an array holding the data for the
variable of the function. (Which defaults to 'x'.)
=item ydata
This should be an array reference to an array holding the function values
corresponding to the x-values in 'xdata'.
=item maximum_iterations
Optional parameter to make the process stop after a given number of iterations.
Using the accuracy measure and this option together is encouraged to prevent
the algorithm from going into an endless loop in some cases.
=back
The subroutine returns the sum of square residuals after the final iteration
as a measure for the quality of the fit.
=head2 EXPORT
t/01basic.t view on Meta::CPAN
['a', 2, 0.0000001],
['c', 20, 0.0000005],
);
my $sqr;
eval {
$sqr = Algorithm::CurveFit::curve_fit(
variable => $variable,
formula => $formula,
params => \@parameters,
maximum_iterations => $max_iter,
xdata => $xdata,
ydata => $ydata,
);
};
ok(!$@, "Call didn't die ($@).");
ok($parameters[0][1] > 0.2-0.1, "Param 1 not too small.");
ok($parameters[0][1] < 0.2+0.1, "Param 1 not too big.");
ok($parameters[1][1] > 2-0.5, "Param 2 not too small.");
ok($parameters[1][1] < 2+0.5, "Param 2 not too big.");
t/02bad_deriv.t view on Meta::CPAN
);
use Algorithm::CurveFit;
Algorithm::CurveFit->curve_fit(
formula => 'sqrt( s*(x-x0)^2 + c) + y0',
variable => 'x',
params => \@parameters,
xdata => \@xdata,
ydata => \@ydata,
maximum_iterations => 20,
);
#use Data::Dumper;
#print Dumper \@parameters;
ok(@parameters == 4);
my @val = (5.15, 0.01, 0.100, 1.045);
my @eps = (1.00, 0.10, 1.000, 0.100);
foreach my $par (0..$#parameters) {
( run in 1.034 second using v1.01-cache-2.11-cpan-71847e10f99 )