sub findMax { my( $f, $x, $step, $eps )= @_; while( 1 ) { my $y1= $f->( $x ); $x += $step; # March forward one more step. my $y2= $f->( $x ); if( $y2 < $y1 ) { # Oops, marched past it: $step /= -2; # Turn around, smaller steps. if( $step < $eps ) { # Close enough: return $x + $step; # Midpoint of last two steps. } } } } my $func= sub { ... }; my $x= findMax( $func, 63.3, 0.1, 0.000001 ); print "Max of ", $func->($x), " at $x.\n";