w3resource

Perform optimization using SciPy's optimize module to find function minimum

NumPy: Integration with SciPy Exercise-14 with Solution

Write a NumPy program to generate a set of points and perform optimization using SciPy's optimize module to find the minimum of a function.

Sample Solution:

Python Code:

import numpy as np  # Import NumPy library
from scipy.optimize import minimize  # Import minimize function from SciPy's optimize module

# Define the function to be minimized
def objective_function(x):
    return x[0]**2 + x[1]**2 + 3 * np.sin(x[0]) * np.cos(x[1])

# Generate a set of initial points
initial_guess = np.array([1.0, 1.0])

# Perform optimization using SciPy's minimize function
result = minimize(objective_function, initial_guess, method='BFGS')

# Print the optimization results
print("Optimization Result:")
print("Minimum Value of the Function:", result.fun)
print("Point at Minimum:", result.x)
print("Number of Iterations Performed:", result.nit)
print("Convergence Status:", result.success)
print("Optimization Message:", result.message)

Output:

Optimization Result:
Minimum Value of the Function: -1.5404628057094898
Point at Minimum: [-9.14856375e-01 -6.01451321e-08]
Number of Iterations Performed: 8
Convergence Status: True
Optimization Message: Optimization terminated successfully.

Explanation:

  • Import libraries:
    • Import the NumPy library for numerical operations.
    • Import the minimize function from SciPy's optimize module for performing optimization.
  • Define the Objective Function:
    • Create the function objective_function(x) to be minimized. This function takes a point x (array of coordinates) as input and returns the function value.
  • Generate Initial Points:
    • Define an initial guess as a NumPy array [1.0, 1.0] for the starting point of the optimization algorithm.
  • Perform optimization:
    • Use the minimize function from SciPy to find the minimum of the objective function starting from the initial guess. The method parameter is set to 'BFGS', a popular optimization algorithm.
  • Finally print the results of the optimization, including:
    • The minimum value of the function (result.fun).
    • The point at which the minimum occurs (result.x).
    • The number of iterations performed (result.nit).
    • The convergence status (result.success).
    • The optimization message (result.message).

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Compute various distance metrics using NumPy and SciPy.
Next: Perform image processing tasks using SciPy's ndimage module.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.