Advertisement
Dmitrey15

sin(x)/x minimum search

Feb 16th, 2012
168
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None
  1. from FuncDesigner import *
  2. x = oovar()
  3. f = sin(x) / x
  4.  
  5. S = 10000000
  6. cons1 = [x>=0, x<=S]
  7. cons2 = [x>=-S, x<=0]
  8. cons3 = [x>=-S, x<=S]
  9. startPoint = {x:0.1}
  10. for cons in [cons1, cons2, cons3]:
  11.     S = oosystem()
  12.     r = S.minimize(f, startPoint, solver='interalg', fTol = 0.000000001, constraints = cons, iprint = 0)
  13.     print('minimum value %e  at point %e' %(r.ff, r(x)))
  14. # minimum value -2.172336e-01  at point 4.493409e+00 , 1.38 sec
  15. # minimum value -2.172336e-01  at point -4.493409e+00, 3.27 sec
  16. # minimum value -2.172336e-01  at point -4.493409e+00, 4.78 sec
  17.  
  18. from numpy import linspace, argmin
  19. xx = linspace(-40, 40, 100000)
  20. y = f({x:xx})
  21. print('minimal value: % 0.7f' % min(y)) # minimal value: -0.2172336
  22. print('best point: % 0.7f' % xx[argmin(y)]) # best point: -4.4932449 (same to +4.4932449)
  23. from pylab import plot, show, grid
  24. plot(xx, y)
  25. grid('on')
  26. show()
  27.  
  28.  
  29. ------------------------- OpenOpt 0.37 -------------------------
  30. solver: interalg   problem: unnamed    type: NLP   goal: minimum
  31.  iter   objFunVal  
  32.     0  9.983e-01
  33. OpenOpt info: Solution with required tolerance 1.0e-09
  34.  is guarantied (obtained precision: 1.0e-09)
  35.   104  -2.172e-01
  36. istop: 1000 (solution has been obtained)
  37. Solver:   Time Elapsed = 1.39   CPU Time Elapsed = 1.38
  38. objFunValue: -0.21723363 (feasible, MaxResidual = 0)
  39. minimum value -2.172336e-01  at point 4.493409e+00
  40.  
  41. ------------------------- OpenOpt 0.37 -------------------------
  42. solver: interalg   problem: unnamed    type: NLP   goal: minimum
  43.  iter   objFunVal  
  44.     0  9.983e-01
  45. OpenOpt info: Solution with required tolerance 1.0e-09
  46.  is guarantied (obtained precision: 1.0e-09)
  47.   562  -2.172e-01
  48. istop: 1000 (solution has been obtained)
  49. Solver:   Time Elapsed = 3.45   CPU Time Elapsed = 3.27
  50. objFunValue: -0.21723363 (feasible, MaxResidual = 0)
  51. minimum value -2.172336e-01  at point -4.493409e+00
  52.  
  53. ------------------------- OpenOpt 0.37 -------------------------
  54. solver: interalg   problem: unnamed    type: NLP   goal: minimum
  55.  iter   objFunVal  
  56.     0  9.983e-01
  57. OpenOpt info: Solution with required tolerance 1.0e-09
  58.  is guarantied (obtained precision: 1.0e-09)
  59.   563  -2.172e-01
  60. istop: 1000 (solution has been obtained)
  61. Solver:   Time Elapsed = 4.78   CPU Time Elapsed = 4.78
  62. objFunValue: -0.21723363 (feasible, MaxResidual = 0)
  63. minimum value -2.172336e-01  at point -4.493409e+00
Advertisement
RAW Paste Data Copied
Advertisement