Code for Gradient disent

Code for calculating local minima for $$f(x,y)= x^4 + y^4 - x^2 - y^2$$ with partial derivative wrt x : $$4x^3 - 2x$$ and partial derivative wrt y: $$4y^3 - 2y$$


In [ ]:
max_iter = 1000
x_o=0
y_o=0
alpha = 0.01 ## Step Size
x_k=2 ## Starting position of x coordinate 
y_k=2 ## Starting position of y coordinate

Here we set the Max iterations as 1000 and starting coordinte in ($x_k,y_k$) = (2,2) and Step size as 0.01 donated by alpha


In [ ]:
def devx(x): ## Defining partial derivative wrt x
    return 4*x**3 - 2*x
def devy(y): ## Defining partial derivation wrt y
    return 4*y**3 -2*y
for i in range(max_iter):
    x_o = x_k
    y_o = y_k
    x_k = x_o - alpha * devx(x_o)
    y_k = y_o - alpha * devy(y_o)

print "Local Minimum at",x_k,",",y_k

Here We define 2 functions devx(x) and devy(y) as the partial derivative with respect to x:$$4x^3 - 2x$$ and partial derivative with respect to y: $$4y^3 - 2y$$ respectively . In the following loop we calculate the local minima using equations : .