Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import numpy as np
- def function(point, a, b, c, d):
- x, y = point
- return a*x + b*y + np.exp(c*x**2+d*y**2)
- def derivative(point, a, b, c, d):
- dx = a + np.exp(c*(point[0]**2)+d*(point[1]**2))*2*c*point[0]
- dy = b + np.exp(c*(point[0]**2)+d*(point[1]**2))*2*d*point[1]
- return [dx, dy]
- def grad(point, alpha, a, b, c, d):
- new_point = [None, None]
- der = derivative(point, a, b, c, d)
- new_point[0] = point[0] - alpha*der[0]
- new_point[1] = point[1] - alpha*der[1]
- return new_point
- def criterion(point, a, b, c ,d):
- der = derivative(point, a, b, c, d)
- if np.sqrt(der[0]**2 + der[1]**2) < 0.0001:
- return False
- elif (np.abs(der[0]) < 0.0001/2) and (np.abs(der[1]) < 0.0001/2):
- return False
- return True
- point = [0, 0]
- alpha = 1
- a, b, c, d = map(float, input("Введите a, b, c, d через пробел: ").split(" "))
- while criterion(point, a, b, c, d):
- new_point = grad(point, alpha, a, b, c, d)
- if function(new_point, a, b, c, d) > function(point, a, b, c, d):
- alpha/=2
- elif function(new_point, a, b, c, d) < function(point, a, b, c, d):
- point = new_point
- print(f'''
- x:{point[0]}, y:{point[1]}
- f(x):{function(point, a, b, c, d)}
- grad(x):{new_point[0]}, grad(y):{new_point[1]}
- alpha:{alpha}''')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement