# Minimizing a function using SciPy's minimize function

by Dejaswarooba Updated: Mar 30, 2023

Solution Kit

SciPy is an open-source Python scientific computing package. SciPy is used in scientific research, engineering, and data analysis. It includes optimization, integration, linear algebra, statistics, signal, and image processing modules. It is more for scientific and technical computing. It is based on the NumPy library and combines with other open-source tools. The tools include data visualization, interactive computing, and data analysis, such as Matplotlib.

The minimize() is a scipy library function that finds the minimum value of a specified function. It is an effective tool for optimizing or finding the optimum solution to a problem. Assume you have a function that takes one or more inputs and outputs a single value. You might be interested in determining the input values that minimize the function's output value. This is where minimize () comes into play.

To use minimize(), you must supply two arguments:

- The function is to be minimized.
- An initial guess for the input values.

The first guess serves only as a starting point for the optimization procedure. The minimize() will then experiment with different input values and calculate the result.

fig1 Example code 1 that exemplifies minimise() function.

fig2 Example code 2 that exemplifies minimise() function.

fig3 Expected output

**Code**

This code shows how to utilise the SciPy library's minimise function to minimise a specified objective function.

The first section of code defines the function **function(t, a)**, which accepts two arguments, t and a. The function's goal is to compute the sum of the squares of t's x and y and a times x. This function is then supplied to the minimise function as the first parameter, along with an initial approximation for the values of x and y.

The minimise function produces an OptimizeResult object containing information about the optimisation process as well as the optimised value of t.

The second section of code defines a function similar to function(x, y, a) that takes three inputs, x, y, and a. This function is then supplied to a wrapper function wrapper(f, a), which creates a new function that accepts a single parameter t and returns it as arguments to the original function f. This wrapper function is then utilised in the minimise function call to optimise the same objective function with a different signature as previously.

The information in the resultant OptimizeResult object is the same as earlier, but the optimisation was performed on a different function signature.

- Copy the above code from Kandi and paste it on any code editor.
- As mentioned earlier, this snippet contains two sections and any one is totally enough for minimising.
- Add
**print(res)**line as shown in the image to display the output.

### Dependent Libraries

If you do not have SciPy that is required to run this code, you can install it by clicking on the above link and copying the pip Install command from the SciPy page in kandi.

*You can search for any dependent library on kandi like **SciPy*

### Environment tested

- This code had been tested using python version 3.8.0
- scipy version 1.10.1 has been used.

### Support

- For any support on kandi solution kits, please use the chat
- For further learning resources, visit the Open Weaver Community learning page.