Advertisement
High_Light

Минимум функции

Apr 11th, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4.  
  5. import scipy.interpolate
  6. import scipy.integrate
  7. from scipy.optimize import minimize
  8.  
  9. def func(x):
  10.     y=3*x*x+2*x
  11.     return y
  12.  
  13. print func(4)
  14.  
  15. def minimize_own(x):
  16.     dx=0.1
  17.     eps=1e-6
  18.     x0=5
  19.     while (dx > eps):
  20.         f_left =func(x0- dx)
  21.         f_right = func(x0+ dx)
  22.         f_midle = func(x0)
  23.         if (f_left< f_midle):
  24.             x0=x0-dx
  25.         elif(f_right< f_midle):
  26.             x0=x0+dx
  27.         else:
  28.             dx*=0.5
  29.         print x0,dx
  30.  
  31.  
  32. #print minimize(3)
  33. res = minimize(func, (5))
  34. print res.x
  35. xar=np.arange(-10,10,0.01)
  36. plt.plot(xar, func(xar))
  37. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement