共轭梯度算法求最小值-scipy

 1 # coding=utf-8
 2 
 3 #共轭梯度算法求最小值
 4 import numpy as np
 5 
 6 from scipy import optimize
 7 
 8 
 9 
10 
11 def f(x, *args):
12     u, v = x
13     a, b, c, d, e, f,g,h = args
14     return a*u**g+ b*u*v + c*v**h + d*u + e*v + f
15 
16 
17 def gradf(x, *args):
18     u, v = x
19     a, b, c, d, e, f,g,h = args
20     gu = g*a*u + b*v + d     # u-component of the gradient
21     gv = b*u +h*c*v + e     # v-component of the gradient
22     return np.asarray((gu, gv))
23 
24 
25 
26 
27 if __name__ == "__main__":
28     args = (2, 3, 7, 8, 9, 10, 2, 2)  # parameter values
29     x0 = np.asarray((0, 0))  # Initial guess.
30     res1 = optimize.fmin_cg(f, x0, fprime=gradf, args=args)
31     #res2 = optimize.fmin(f,x0,args)
32     print(res1)
33     #print(res2)
34     print(f(res1,*args))

 

posted @ 2018-01-03 16:30  shizhenqiang  阅读(1470)  评论(0编辑  收藏  举报