Optimization of Objective Function Using SciPy#s Minimize

  • Share this:

Code introduction


This function uses the minimize function from the SciPy library to find the minimum of a function. The user can provide an initial guess x0, and the function will return the optimal solution.


Technology Stack : SciPy, NumPy

Code Type : Mathematical optimization

Code Difficulty : Intermediate


                
                    
import numpy as np
from scipy.optimize import minimize

def optimize_function(x0):
    # Define the objective function to be minimized
    def objective_function(x):
        return (x[0] - 1)**2 + (x[1] - 2)**2
    
    # Initial guess
    x0 = np.array([x0, x0])
    
    # Call the minimize function from scipy.optimize
    result = minimize(objective_function, x0)
    
    # Return the optimal solution
    return result.x                
              
Tags: