简体   繁体   中英

Find minimum and maximum values of a function

I have a function and I would like to find its maximum and minimum values. My function is this:

def function(x, y):
    exp = (math.pow(x, 2) + math.pow(y, 2)) * -1
    return math.exp(exp) * math.cos(x * y) * math.sin(x * y)

I have an interval for x [-1, 1] and y [-1, 1]. I would like to find a way, limited to this interval, to discover the max and min values of this function.

Using, for instance, scipy 's fmin (which contains an implementation of the Nelder-Mead algorithm), you can try this:

import numpy as np
from scipy.optimize import fmin
import math

def f(x):
    exp = (math.pow(x[0], 2) + math.pow(x[1], 2)) * -1
    return math.exp(exp) * math.cos(x[0] * x[1]) * math.sin(x[0] * x[1])

fmin(f,np.array([0,0]))

which yields the following output:

Optimization terminated successfully.
     Current function value: -0.161198
     Iterations: 60
     Function evaluations: 113 
array([ 0.62665701, -0.62663095])

Please keep in mind that:

1) with scipy you need to convert your function into a function accepting an array (I showed how to do it in the example above);

2) fmin uses, like most of its pairs, an iterative algorithm, therefore you must provide a starting point (in my example, I provided (0,0) ). You can provide different starting points to obtain different minima/maxima.

Here is something which gives fairly close estimates (not exact).

import math
import random
import sys

def function(x, y):
    exp = (math.pow(x, 2) + math.pow(y, 2)) * -1
    return math.exp(exp) * math.cos(x * y) * math.sin(x * y)

max_func = - sys.maxint - 1
min_func = sys.maxint
maximal_x, maximal_y = None, None
minimal_x, minimal_y = None, None

for i in xrange(1000000):
    randx = random.random()*2 - 1
    randy = random.random()*2 - 1
    result = function(randx, randy)
    max_func = max(max_func, result)
    if max_func == result:
        maximal_x, maximal_y = randx, randy
    min_func = min(min_func, result)
    if min_func == result:
        minimal_x, minimal_y = randx, randy

print "Maximal (x, y):", (maximal_x, maximal_y)
print "Max func value:", max_func, '\n'
print "Minimal (x, y):", (minimal_x, minimal_y)
print "Min func value:", min_func

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM