use v6.d;
sub steepestDescent(@x, $alpha is copy, $h is copy) {
my $g0 = g(@x) ;
my @fi = gradG(@x, $h, $g0) ;
my $b = $alpha / sqrt(my $delG = sum(map {$_²}, @fi));
while ( $delG > $h ) {
for @fi.kv -> $i, $j { @x[$i] -= $b * $j }
@fi = gradG(@x, $h /= 2, my $g1 = g(@x));
$b = $alpha / sqrt($delG = sum(map {$_²}, @fi) );
$g1 > $g0 ?? ( $alpha /= 2 ) !! ( $g0 = $g1 )
}
}
sub gradG(@x is copy, $h, $g0) {
return map { $_ += $h ; (g(@x) - $g0) / $h }, @x
}
sub g(\x) { (x[0]-1)² * exp(-x[1]²) + x[1]*(x[1]+2) * exp(-2*x[0]²) }
my $tolerance = 0.0000006 ; my $alpha = 0.1;
my @x = 0.1, -1;
steepestDescent(@x, $alpha, $tolerance);
say "Testing steepest descent method:";
say "The minimum is at x[0] = ", @x[0], ", x[1] = ", @x[1];
Output:
Testing steepest descent method:
The minimum is at x[0] = 0.10743450794656964, x[1] = -1.2233956711774543