Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Gradiant:
- M = 3
- @staticmethod
- def g(i, x):
- if i == 0:
- return x * x - 5 * x + 4
- elif i == 1:
- return x * x - 3 * x
- else:
- return x * x - 2 * x
- @staticmethod
- def grad(i, x):
- if i == 0:
- return 2 * x - 5
- elif i == 1:
- return 2 * x - 3
- else:
- return 2 * x - 2
- @staticmethod
- def nextX(i, x):
- g_val = Gradiant.g(i, x)
- if g_val <= 0:
- return x
- grd = Gradiant.grad(i, x)
- return x - (g_val / grd)
- @staticmethod
- def findZero(i, x):
- print()
- print(f"start finding zero point for g{i+1} from x={x}")
- while Gradiant.g(i, x) > 0:
- print(f"g{i+1}({x}) = {Gradiant.g(i, x)}")
- x = Gradiant.nextX(i, x)
- print(f"zero point found for g{i+1} at x={x}")
- return x
- @staticmethod
- def findRangEdge(x):
- for i in range(Gradiant.M):
- x = Gradiant.findZero(i, x)
- print(f"range edge found at x = {x}")
- return x
- low = Gradiant.findRangEdge(-5)
- print(f"Range low = {low}")
- high = Gradiant.findRangEdge(low + 2)
- print(f"Range high = {high}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement