GradientDescentThirdDegreePolynomialV1-checkpoint.py (901B)
1 import random 2 print("ax^3 + bx^2 + cx + d") 3 4 a = float(input("Enter a: ")) 5 b = float(input("Enter b: ")) 6 c = float(input("Enter c: ")) 7 d = float(input("Enter d: ")) 8 9 10 def calculateValue(x): 11 return x**3 * a + x**2 * b + x * c + d 12 13 14 def printResult(x, y): 15 print("x: " + str(x) + " y: " + str(y)) 16 17 18 # Find the x at the bottom 19 def descend(x, depth): 20 21 if depth > 100: 22 return x 23 24 midX = x 25 rightX = x + .00000001 26 leftX = x - .00000001 27 28 midY = calculateValue(midX) 29 rightY = calculateValue(rightX) 30 leftY = calculateValue(leftX) 31 32 printResult(leftX, leftY) 33 printResult(midX, midY) 34 printResult(rightX, rightY) 35 36 depth += 1 37 if rightY > leftY: 38 return descend(x - (1 / depth), depth) 39 else: 40 return descend(x + (1/depth), depth) 41 42 43 currSearch = random.random() * 10 44 xVal = descend(currSearch, 0) 45 46 printResult(xVal, calculateValue(xVal))