Given the function f(x) = x^2 + 6x
,
How do I use derivative and gradient descent to find the value of x that minimizes this function in R or Python?
def F(x):
try:
return (x**2) + (6*x)
except:
return float('inf')
def f(x):
return 2*x + 6
x=0.0
eta=0.1
epsilon=1e-8
history_x=[x]
while True:
gradient=dJ(x)
last_x=x
x = x-eta * gradient
history_x.append(x)
if (abs(J(last_x)-J(x)) <epsilon):
break
print(history_x[-1])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.