Skip to content

Instantly share code, notes, and snippets.

@tjvananne
Created August 7, 2017 19:38
Show Gist options
  • Save tjvananne/6ffba77cebbe89bf933b6c7135e924cc to your computer and use it in GitHub Desktop.
Save tjvananne/6ffba77cebbe89bf933b6c7135e924cc to your computer and use it in GitHub Desktop.
Gradient descent implemented simply in R -- this has a source but I forgot where I got this source from (I can't take credit for this)
# set up a stepsize
alpha = 0.003
# set up a number of iteration
iter = 500
# define the gradient of f(x) = x^4 - 3*x^3 + 2
f_of_x <- function(x) return((x^4) - (3*x^3) + 2)
gradient = function(x) return((4*x^3) - (9*x^2))
# understanding polynomials is a pretty huge barrier right now to my understanding of complex topics
plot(f_of_x, xlim=c(-10,10), ylim=c(-10, 10))
abline(v = 2.25, col='red')
# plot(gradient, xlim=c(-10,10), ylim=c(-10, 10))
# abline(v=2.25, col='red')
# randomly initialize a value to x
set.seed(100)
#x = floor(runif(1)*10)
x = 0.1
# create a vector to contain all xs for all steps
x.All = vector("numeric",iter)
# gradient descent method to find the minimum
for(i in 1:iter){
x = x - alpha*gradient(x)
x.All[i] = x
print(x)
# if(x > 2) {
# plot(f_of_x, xlim=c(-5,5), ylim=c(-6, 6), lwd=3)
# abline(a=x, b=f_of_x(x), col='red')
# break
# }
}
# print result and plot all xs for every iteration
print(paste("The minimum of f(x) is ", x, sep = ""))
plot(x.All, type = "l")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment