Untitled

mail@pastecode.io avatar
unknown
python
3 years ago
506 B
8
Indexable
Never
import sys

def function(x):
    return 4 * x ** 4 - 4 * x ** 3 - 24 * x ** 2 + 16 * x + 52
    
def gradient(x):
    return 16 * x ** 3 - 12 * x ** 2 - 48 * x + 16

def gradient_2(x):
    return 48 * x ** 2 - 24 * x - 48

# Try sys.maxsize for positive infinity and -sys.maxsize - 1 for negative infinity
x0 = -sys.maxsize - 1
temp = x0 - gradient(x0) / gradient_2(x0)
while abs(temp - x0) > 1e-14:
    x0 = temp
    temp = x0 - gradient(x0) / gradient_2(x0)
    
print(x0, function(x0))