# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import numpy as np
def function(x):
return x[0]**2+x[1]**2
def numerical_gradient(x,f):
h = 0.0001
grad = np.zeros_like(x)
for index in range(x.size):#x.size()
temp_x = x[index]
x[index] = temp_x + h
fxh1= f(x)
x[index] = temp_x - h
fxh2 = f(x)
grad[index] = (fxh1 - fxh2)/(2*h)
x[index] = temp_x
return grad
def gradient_descent(init_x,y, lr = 0.1, step_num=100):
x = init_x
for i in range(step_num):
grad = numerical_gradient(x, y)
x = x - lr *grad
return x
if __name__ == "__main__":
int_x = np.array([-3.0, 4.0])
c = gradient_descent(int_x,function, lr = 0.01, step_num=100)
print(c)