Professional Documents
Culture Documents
Cubic Bspline - PL
Cubic Bspline - PL
cubic_bspline.pl
Page 1/2
cubic_bspline.pl
Page 2/2
#!/usr/bin/perl w
use strict;
use
use
use
use
use
use
use
Math::GSL::BSpline qw/:all/;
Math::GSL::Vector qw/:all/;
Math::GSL::Matrix qw/:all/;
Math::GSL::RNG qw/:all/;
Math::GSL::Multifit qw /:all/;
Math::GSL::Randist qw/:all/;
Math::GSL::Statistics qw /:all/;
# do the fit
$chisq=gsl_multifit_wlinear($X>raw(), $w>raw(), $y>raw(), $c>raw(), $cov>ra
w(), $mw);
$dof = $n $ncoeffs;
my $n = $N;
my $ncoeffs = $NCOEFFS;
my $nbreak = $NBREAK;
my ($chisq, $Rsq, $dof, $tss);
my $seed=5;
#my $r = Math::GSL::RNG>new;
my $r = Math::GSL::RNG>new($gsl_rng_default,$seed);
my @w_data=$w>as_list();
my @y_data=$y>as_list();
my $y_size=$y>length();
$tss = gsl_stats_wtss(\@w_data, 1, \@y_data, 1, $y>length());
$Rsq = 1.0 $chisq / $tss;
printf STDERR "chisq/dof = %e, Rsq = %f\n",$chisq / $dof, $Rsq;
#
{
$B = Math::GSL::Vector>new($ncoeffs);
$x = Math::GSL::Vector>new($n);
$y = Math::GSL::Vector>new($n);
$X = Math::GSL::Matrix>new($n, $ncoeffs);
$c = Math::GSL::Vector>new($ncoeffs);
$w = Math::GSL::Vector>new($n);
$cov = Math::GSL::Matrix>new($ncoeffs, $ncoeffs);
$mw = gsl_multifit_linear_alloc($n, $ncoeffs);
printf("#m=0,S=0\n");
# this is the data to be fitted
for (my $i = 0; $i < $n; ++$i) {
my $xi = (15.0 / ($N 1)) * $i;
my $yi = cos($xi) * exp(0.1 * $xi);
my $sigma = 0.1 * $yi;
my $dy = gsl_ran_gaussian($r>raw(), $sigma);
$yi += $dy;
$x>set([$i], [$xi]);
$y>set([$i], [$yi]);
$w>set([$i], [1.0 / ($sigma * $sigma)]);
printf("%f %f\n", $xi, $yi);
}
cubic_bspline.pl
1/1