use strict; use Statistics::Regression; my $reg= Statistics::Regression->new( 3, "quad regression", [ "const", "X", "X^2" ] ); my @array = ('62.6', '62.8', '63', '63.3', '63.5', '63.7', '64'); my @array2 = ('7.476', '7.5219', '7.5464', '7.5516', '7.5504', '7.5372', '7.518'); my $i; for $i (0..$#array) { my $x = $array[$i]; my $x2 = $x * $x; my $y = $array2[$i]; $reg->include($y, [1.0, $x, $x2]); } my $theta = $reg->theta(); print "Coefficients: ",join(",",@$theta),"\n"; my $c = $theta->[0]; my $b = $theta->[1]; my $a = $theta->[2]; print "\nComputed results:\n"; for $i (0..$#array) { my $x = $array[$i]; my $y = ($a*$x + $b)*$x + $c; my $ytrue = $array2[$i]; print "$x\t$y\t($ytrue)\n"; } # Solve for the derivative being 0. my $maxx = -$b/(2.0*$a); my $maxy = ($a*$maxx + $b)*$maxx + $c; print "\nPeak point:\n"; print "$maxx\t$maxy\n";