#! /usr/bin/env python3 # def sag_gradient1 ( ): #*****************************************************************************80 # ## sag_gradient1 applies gradient descent to the sag function. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 01 October 2019 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np from sag import sag from sag import sag_df from gradient_descent1 import gradient_descent1 a = 0.0 b = 10.0; print ( '' ) print ( 'sag_gradient1:' ) print ( ' Seek local minimizer of a scalar function f(x) in [',a,',',b,']' ) # # Plot the function. # xvec = np.linspace ( a, b, 101 ) fxvec = sag ( xvec ) plt.plot ( xvec, fxvec, linewidth = 3 ) plt.plot ( [ a, b ], [ 0, 0 ], 'k', linewidth = 3 ) plt.grid ( True ) plt.xlabel ( '<-- X -->' ) plt.ylabel ( '<-- F(X) -->' ) plt.title ( 'sag function', fontsize = 16 ) filename = 'sag.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved in "%s"' % ( filename ) ) plt.show ( ) plt.clf ( ) # # Choose a random initial guess. # x0 = a + ( b - a ) * np.random.rand ( ) # # Choose a small learning rate. # r = 0.05 # # Choose a small tolerance for the stepsize. # dxtol = 0.000001 # # Choose a small tolerance for the derivative. # dftol = 0.00001 # # Maximum number of iterations. # itmax = 100 x, it = gradient_descent1 ( sag, sag_df, x0, r, dxtol, dftol, itmax ) print ( '' ) print ( ' ', it, 'gradient descent steps were taken.' ) print ( ' Initial x = ', x0, ' f(x) = ', sag ( x0 ), ' f\'(x) = ', sag_df(x0) ) print ( ' Final x = ', x, ' f(x) = ', sag ( x ), ' f\'(x) = ', sag_df(x) ) # # Plot the function with initial point and local minimizer # xvec = np.linspace ( a, b, 101 ) fxvec = sag ( xvec ) plt.plot ( xvec, fxvec, linewidth = 3 ) plt.plot ( [ x0, x0 ], [ 0, sag ( x0 ) ], 'b', linewidth = 3 ) plt.plot ( x0, 0.0, 'bo', markersize = 10 ) plt.plot ( x0, sag ( x0 ), 'bo', markersize = 10 ) plt.plot ( [ x, x ], [ 0, sag ( x ) ], 'r', linewidth = 3 ) plt.plot ( x, 0.0, 'ro', markersize = 10 ) plt.plot ( x, sag ( x ), 'ro', markersize = 10 ) plt.plot ( [ a, b ], [ 0, 0 ], 'k', linewidth = 3 ) plt.grid ( True ) plt.xlabel ( '<-- X -->' ) plt.ylabel ( '<-- F(X) -->' ) plt.title ( 'Initial point in blue, local minimizer in red', fontsize = 16 ) filename = 'sag_minimizer.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved in "%s"' % ( filename ) ) plt.show ( ) plt.clf ( ) # # Terminate. # print ( '' ) print ( 'sag_gradient1:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): sag_gradient1 ( )