优化演示¶
日期 | 2009-07-13(最后修改),2006-02-10(创建) |
---|
SciPy 的优化包是 scipy.optimize。最基本的非线性优化函数是
* optimize.fmin(func, x0), which finds the minimum of func(x) starting x with x0 (x can be a vector)
* optimize.fsolve(func, x0), which finds a solution to func(x) = 0 starting with x = x0 (x can be a vector)
* optimize.fminbound(func, x1, x2), which finds the minimum of a scalar function func(x) for the range [x1,x2] (x1,x2 must be a scalar and func(x) must return a scalar)
有关详细信息,请参阅 scipy.optimze 文档。
这是一个快速演示,演示了如何从几个贝塞尔函数生成数据,并使用 fminbound 找到一些局部最大值。这使用带有 -pylab 开关的 ipython。
In [1]
from scipy import optimize, special
from numpy import *
from pylab import *
x = arange(0,10,0.01)
for k in arange(0.5,5.5):
y = special.jv(k,x)
plot(x,y)
f = lambda x: -special.jv(k,x)
x_max = optimize.fminbound(f,0,6)
plot([x_max], [special.jv(k,x_max)],'ro')
title('Different Bessel functions and their local maxima')
show()
章节作者:SciPyDotOrg、Unknown[134]、Unknown[135]、Unknown[136]