import numpy as np from scipy.optimize import minimize # Define the objective function to minimize def objective(x): ''' Minimize f(x1, x2) = x1^2 + 2 * x2^2 (similar to minimizing the volume) x[0] is x1, x[1] is x2 ''' return (x[0]**2 + 2 * x[1]**2) def constraint1(x): ''' Define non-linear constraints (SciPy requires them as >= 0). Algebraically rewrite your formulas to match this format. Minimum area requirement: x1 * x2 >= 4 should be reformatted as x1 * x2 - 4 >= 0 ''' return (x[0] * x[1] - 4) def constraint2(x): ''' Parabolic boundary x1^2 + 2 * x2 <= 12 should be changed 12 - x1^2 - 2 * x2 >= 0 ''' return 12 - x[0]**2 - 2 * x[1] # Setup configuration initial_guess = np.array([0.0, 1.0]) bounds = ((0.5, None), (0.5, None)) # Combine multiple constraints into a list of dictionaries constraints = [ {'type': 'ineq', 'fun': constraint1}, {'type': 'ineq', 'fun': constraint2} ] # Run the SLSQP optimizer result = minimize( fun=objective, x0=initial_guess, method='SLSQP', bounds=bounds, constraints=constraints ) # Print the optimized output if result.success: print(f"Optimal x1: {result.x[0]:.3f}") print(f"Optimal x2: {result.x[1]:.3f}") print(f"Minimum Objective Value: {result.fun:.2f}") else: print("Optimization failed:", result.message)