in reply to Re^2: Stumped with a math question in my Perl program (log scale)
in thread Stumped with a math question in my Perl program
tye:
Yeah, I knew you weren't trying to find the curve for the sequences. The formula I gave was for minimizing the sum of the square of the difference between series 1 and A * series 2. That's so you could find A, the scaling factor you were looking for.
Having said that, however, when I tried to code it up, I found that I couldn't figure out how to express it as a curve fit problem. So instead I hacked up an iterative approach:
#!/usr/bin/perl -w use strict; use warnings; my @E1 = (2, 3.23, 7, 9, 11.3479); my @E2 = (3.3333, 1.433, 8.0577, 9.7344, 13.3377); my ($Alow, $Ahi, $Astep) = # (.5, 5, .1); # (0.8, 1.0, 0.025); # (0.85, 0.9, 0.005); (0.87, 0.88, 0.001); for (my $A=$Alow; $A <= $Ahi; $A+=$Astep) { printf "%7.4f %5.3f\n", $A, current_error($A); } sub current_error { my $A = shift; my $err=0; for (my $i=0; $i<@E1; ++$i) { my $t = $E1[$i] - $A*$E2[$i]; $err += $t*$t; } return $err; }
And the last run gave me:
Roboticus@Roboticus-PC ~ $ ./curvefit.pl 0.8700 5.091 0.8710 5.088 0.8720 5.086 0.8730 5.085 0.8740 5.084 0.8750 5.085 0.8760 5.085 0.8770 5.087 0.8780 5.089 0.8790 5.092 0.8800 5.096
So it looks like your multiplier is going to be roughly 0.874. If you need to consider a constant offset, you'll (obviously) have to nest in another loop to vary the constant as well. Since you didn't indicate the need for one, I didn't bother.
...roboticus
|
|---|