In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
def plot_poly(poly_func, min_x=-6.0, max_x=2.0):
step = (max_x - min_x) / 200
x = np.arange(min_x, max_x, step)
y = poly_func(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, 'r-')
ax.set_xlabel('value of parameter')
ax.set_ylabel('objective')
ax.set_xlim([-6, 2])
ax.set_ylim([-60, 70])
ax.grid()
return ax
def poly1(x):
x2 = np.power(x, 2)
x3 = np.power(x, 3)
x4 = np.power(x, 4)
y = x4 + 7*x3 + 5*x2 - 17*x + 3
return y
# The gradient is 4x^3 + 21x^2 + 10x - 17
coeff = [4, 21, 10, -17]
stat_points = np.roots(coeff)
obj_val = poly1(stat_points)
print(stat_points)
print(obj_val)
In [3]:
ax = plot_poly(poly1)
ax.text(-4, 40, '$x^4 + 7x^3 + 5x^2 - 17x + 3$')
ax.plot([-7, stat_points[0], stat_points[0]], [obj_val[0], obj_val[0], -60], 'b--')
plt.savefig('gradient-descent-1d.pdf', dpi=600)
In [ ]: