Gradient descent: Difference between revisions
→{{header|Go}}: Updated solution.
(Added Algol W) |
(→{{header|Go}}: Updated solution.) |
||
Line 288:
This is a translation of the C# code in the book excerpt linked to above and hence also of the first Typescript example below.
However, since it was originally written, I've substituted Fortran's gradient function for the original one (see Talk page) which now gives results which agree (to 6 decimal places) with those of the Fortran, Julia, Algol 68 and Algol W solutions. As a number of other solutions are based on this one, I suggest their authors update them accordingly.
<lang go>package main
Line 302:
// Calculate initial gradient.
fi := gradG(x
// Calculate initial norm.
Line 321:
// Calculate next gradient.
fi = gradG(x
// Calculate next norm.
Line 343:
}
// Provides a rough calculation of gradient g(
func gradG(
y :=
z[0] = 2*(x-1)*math.Exp(-y*y) - 4*x*math.Exp(-2*x*x)*y*(y+2)
z[1] = -2*(x-1)*(x-1)*y*math.Exp(-y*y) + math.Exp(-2*x*x)*(y+2) + math.Exp(-2*x*x)*y
return z
}
Line 372 ⟶ 367:
steepestDescent(x, alpha, tolerance)
fmt.Println("Testing steepest descent method:")
fmt.
}</lang>▼
▲</lang>
{{out}}
<pre>
Testing steepest descent method:
The minimum is at x
</pre>
|