Как найти максимум функции python
Перейти к содержимому

# Как найти максимум функции python

## 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:

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:

which yields the following output:

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.

## Finding the maximum of a function

How do I find the maximum of a function in Python? I could try to hack together a derivative function and find the zero of that, but is there a method in numpy (or other library) that can do it for me?

You can use scipy.optimize.fmin on the negative of your function.

If your function is solvable analytically try SymPy. I’ll use EMS’s example above.

Of course, you’ll still need to check that 1 is a maximizer and not a minimizer of f

I think scipy.optimize.minimize_scalar and scipy.optimize.minimize are the preferred ways now, that give you access to the range of techniques, e.g.

for a single variable function that must lie between 0 and 1.

You could try SymPy. SymPy might be able to provide you with the derivative symbolically, find its zeros, and so on.

Maximum of a function with parameters.

The Overflow Blog
##### Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.11.43304

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

## Пример использования библиотеки sympy для поиска экстремума функции

найдем значение x, которому соответсвует минимум или максимум функции.

Объявляем используемые в функции символьные параметры

Найдем её производную при помощи функции библиотеки sympy diff. Первый аргумент функции diff – дифференцируемое выражение, второй – переменная, по которой необходимо найти производную:

В результате переменная df будет содержать следующее выражение

В полученном выражении для производной заменим символы (параметры) a, b, h, k, v1 их значениями (a=10, b=10, h=10, k=5, v1=5). Для этого создаем словарь

## Как найти максимум функции python

How do I find the maximum of a function in Python? I could try to hack together a derivative function and find the zero of that, but is there a method in numpy (or other library) that can do it for me?

1 day later, before I forget that I even answered that specific question.

You can use scipy.optimize.fmin on the negative of your function.

If your function is solvable analytically try SymPy. I’ll use EMS’s example above.

Of course, you’ll still need to check that 1 is a maximizer and not a minimizer of f

I think scipy.optimize.minimize_scalar and scipy.optimize.minimize are the preferred ways now, that give you access to the range of techniques, e.g.

for a single variable function that must lie between 0 and 1.

You could try SymPy. SymPy might be able to provide you with the derivative symbolically, find its zeros, and so on.

Maximum of a function with parameters.