1

我需要限制某些数据的最小化(即,以便我获得一定范围内的最小值)。目前我只能得到所有空间的最小值。例如,如果手头问题的有效可能答案仅在 -5<=x<=5 范围内,那么让 fmin 告诉我函数的最小值在 -10505 是没有用的。我需要将可能的输出限制在问题范围内。

        p = [0,0,0]
        fit_quad = lambda p,w: p[2]*w**2 + p[1]*w + p[0]
        errfunc = lambda p,l,w: fit_quad(p,w) - l
        fit, success = leastsq(errfunc, p, args=(y,x), maxfev=5000) #x and y are the input datasets
        #find the minimum tilt
        quad = lambda w,p: p[2]*w**2 + p[1]*w + p[0]
        min_tilt = fmin(quad, 0.0, args=([fit]))[0]
        #check for range violations
        if min_tilt < min_angle #the minimum on the quadratic can sometimes end up negative, especially if there are not enough good points
            min_tilt = 0.0
        elif min_tilt > max_angle: #if things are extremely tilted the minimum of the fit quadratic can end up unrealistically high. This pulls it back.
            min_tilt = max_angle

请注意,仅像上面那样为上面或下面设置一个简单的检查是不够的。根据范围内函数的精确部分,我可能会在范围的错误一侧结束。

4

2 回答 2

1

使用fmin_cobylafmin_slsqp

于 2012-08-21T23:23:00.453 回答
-1

你可以过滤你的数据:

ll = np.array([100,20,-30,1,2,3,4,5])

min(np.array(filter(lambda x:-5 < x < 5, ll)))
于 2012-08-22T00:00:36.567 回答