简体   繁体   中英

How to use derivative and gradient decent to find the value of x that minimizes function

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM