In [1]:
1+3


Out[1]:
4

In [2]:
%matplotlib inline

In [3]:
import numpy
import matplotlib.pyplot as plt

In [4]:
x = numpy.arange(-10,10,0.01)

In [5]:
x


Out[5]:
array([-10.  ,  -9.99,  -9.98, ...,   9.97,   9.98,   9.99])

In [7]:
def quad_f(x):
    return x**2

plt.plot(x, quad_f(x))


Out[7]:
[<matplotlib.lines.Line2D at 0x5366a30>]

In [9]:
def d_f(x):
    return 2*x

#precision은 x값을 얼마나 갔을때 멈출것인가를 정한 값이다.
precision = 0.01

temp_x = 8
old_x = 15
lr = 0.2

xs = [8]

while abs(temp_x - old_x) > precision:
    gradient = d_f(temp_x)
    move = gradient * lr
    old_x = temp_x
    temp_x = temp_x - move
    xs.append(temp_x)
    
print("Local minimum occurs as {}".format(round(temp_x,2)))


Local minimum occurs as 0.01

In [10]:
plt.plot(x,quad_f(x),xs,quad_f(numpy.array(xs)),'rs')


Out[10]:
[<matplotlib.lines.Line2D at 0x53d3c30>,
 <matplotlib.lines.Line2D at 0x53d3d10>]

In [ ]:
}