Menu

[r3313]: / trunk / py4science / book / examples / scipy / example5.5  Maximize  Restore  History

Download this file

22 lines (20 with data), 721 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
>>> from scipy.optimize import fmin_ncg
>>> def rosen_hess(x):
x = asarray(x)
H = diag(-400*x[:-1],1) - diag(400*x[:-1],-1)
diagonal = zeros(len(x),x.typecode())
diagonal[0] = 1200*x[0]-400*x[1]+2
diagonal[-1] = 200
diagonal[1:-1] = 202 + 1200*x[1:-1]**2 - 400*x[2:]
H = H + diag(diagonal)
return H
>>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
>>> xopt = fmin_ncg(rosen, x0, rosen_der, fhess=rosen_hess)
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 19
Function evaluations: 40
Gradient evaluations: 19
Hessian evaluations: 19
>>> print xopt
[ 0.9999 0.9999 0.9998 0.9996 0.9991]
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.