Minimizing a function using SciPy's minimize function

share link

by Dejaswarooba dot icon Updated: Mar 30, 2023

technology logo
technology logo

Solution Kit 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:

  1. The function is to be minimized.
  2. 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


scipyby scipy

Python doticonstar image 11340 doticonVersion:v1.11.0rc1doticon
License: Permissive (BSD-3-Clause)

SciPy library main repository

Support
    Quality
      Security
        License
          Reuse

            scipyby scipy

            Python doticon star image 11340 doticonVersion:v1.11.0rc1doticon License: Permissive (BSD-3-Clause)

            SciPy library main repository
            Support
              Quality
                Security
                  License
                    Reuse

                      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

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

                      Support

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

                      See similar Kits and Libraries