Gradient descent: Difference between revisions
Content added Content deleted
(Added Algol W) |
|||
Line 94: | Line 94: | ||
Testing steepest descent method: |
Testing steepest descent method: |
||
The minimum is at x[0] = 0.107627, x[1] = -1.223260 |
The minimum is at x[0] = 0.107627, x[1] = -1.223260 |
||
</pre> |
|||
=={{header|ALGOL W}}== |
|||
{{Trans|ALGOL 68}} which is a {{Trans|Go}} with the gradient function from {{Trans|Fortran}} |
|||
The results agree (to 6 places) with the Fortran and Julia samples. |
|||
<lang algolw>begin |
|||
procedure steepestDescent ( real array x ( * ); real value alphap, tolerance ) ; |
|||
begin |
|||
real array fi ( 0 :: 1 ); |
|||
real alpha, g0, g1, delG, b; |
|||
alpha := alphap; |
|||
g0 := g( x ); % Initial estimate of result. % |
|||
% Calculate initial gradient. % |
|||
gradG( fi, x ); |
|||
% Calculate initial norm. % |
|||
delG := 0.0; |
|||
for i := 0 until 1 do delG := delG + ( fi( i ) * fi( i ) ); |
|||
delG := sqrt( delG ); |
|||
b := alpha / delG; |
|||
% Iterate until value is <= tolerance. % |
|||
while delG > tolerance do begin |
|||
% Calculate next value. % |
|||
for i := 0 until 1 do x(i) := x( i ) - ( b * fi(i) ); |
|||
% Calculate next gradient. % |
|||
gradG( fi, x ); |
|||
% Calculate next norm. % |
|||
delG := 0; |
|||
for i := 0 until 1 do delG := delg + ( fi( i ) * fi( i ) ); |
|||
delG := sqrt( delG ); |
|||
if delG > 0 then begin |
|||
b := alpha / delG; |
|||
% Calculate next value. % |
|||
g1 := g( x ); |
|||
% Adjust parameter. % |
|||
if g1 > g0 |
|||
then alpha := alpha / 2 |
|||
else g0 := g1 |
|||
end if_delG_gt_0 |
|||
end while_delG_gt_tolerance |
|||
end steepestDescent ; |
|||
% Provides a rough calculation of gradient g(x). % |
|||
procedure gradG ( real array z, p ( * ) ) ; |
|||
begin |
|||
real x, y; |
|||
x := p( 0 ); |
|||
y := p( 1 ); |
|||
z( 0 ) := 2 * ( x - 1 ) * exp( - ( y * y ) ) |
|||
- 4 * x * exp( -2 * ( x * x ) ) * y * ( y + 2 ); |
|||
z( 1 ) := ( -2 * ( x - 1 ) * ( x - 1 ) * y ) * exp( - y * y ) |
|||
+ exp( -2 * x * x ) * ( y + 2 ) |
|||
+ exp( -2 * x * x ) * y |
|||
end gradG ; |
|||
% Function for which minimum is to be found. % |
|||
real procedure g ( real array x ( * ) ) ; |
|||
( x( 0 ) - 1 ) |
|||
* ( x( 0 ) - 1 ) |
|||
* exp( - x( 1 ) * x( 1 ) ) + x( 1 ) * ( x( 1 ) + 2 ) |
|||
* exp( - 2 * x( 0 ) * x( 0 ) ) |
|||
; |
|||
begin |
|||
real alpha, tolerance; |
|||
real array x ( 0 :: 1 ); |
|||
x( 0 ) := 0.1; % Initial guess of location of minimum. % |
|||
x( 1 ) := -1; |
|||
tolerance := 0.0000006; |
|||
alpha := 0.1; |
|||
steepestDescent( x, alpha, tolerance ); |
|||
r_format := "A"; r_w := 11; r_d := 7; s_w := 0; % output formatting % |
|||
write( "Testing steepest descent method:" ); |
|||
write( "The minimum is at x(0) = ", x( 0 ), ", x(1) = ", x( 1 ) ) |
|||
end |
|||
end.</lang> |
|||
{{out}} |
|||
<pre> |
|||
Testing steepest descent method: |
|||
The minimum is at x(0) = 0.1076268, x(1) = -1.2232596 |
|||
</pre> |
</pre> |
||