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"; #### Coefficients: -451.490758046837,14.4832720403469,-0.11423976795752 Computed results: 62.6 7.48383859766494 (7.476) 62.8 7.51535962535968 (7.5219) 63 7.53774147161789 (7.5464) 63.3 7.55417827581152 (7.5516) 63.5 7.55371216847811 (7.5504) 63.7 7.54410687970818 (7.5372) 64 7.51256298135968 (7.518) Peak point: 63.3897998012935 7.5550995057929