commit d31a740d5bbc19434b2a813799b230bec8537507
parent c84f4c84ad727ab3593f8f33614bd664f162b10e
Author: Andrew <andrewlaack1@gmail.com>
Date: Thu, 18 Apr 2024 18:08:25 -0500
Improved clarity
Diffstat:
1 file changed, 45 insertions(+), 0 deletions(-)
diff --git a/GradientDescentThirdDegreePolynomialV2.py b/GradientDescentThirdDegreePolynomialV2.py
@@ -0,0 +1,45 @@
+import random
+print("ax^3 + bx^2 + cx + d")
+
+a = float(input("Enter a: "))
+b = float(input("Enter b: "))
+c = float(input("Enter c: "))
+d = float(input("Enter d: "))
+
+
+def calculateValue(x):
+ return x**3 * a + x**2 * b + x * c + d
+
+
+def printResult(x, y):
+ print("x: " + str(x) + " y: " + str(y))
+
+
+def limit(x):
+ rightX = x + .00000001
+ leftX = x - .00000001
+
+ rightY = calculateValue(rightX)
+ leftY = calculateValue(leftX)
+
+ return ((rightY - leftY) / .00000002)
+
+
+def descend(x, depth):
+ print(x)
+ if depth > 100:
+ return x
+ lim = limit(x)
+ print(lim)
+ depth += 1
+
+ if lim > 0:
+ return descend(x - .001, depth)
+ else:
+ return descend(x + .001, depth)
+
+
+currSearch = random.random() * 10
+xVal = descend(currSearch, 0)
+
+printResult(xVal, calculateValue(xVal))