Python for Engineering -

Minimum of Equation


Introduction

There are many methods to use with minimize. We will list the methods below. They all work similarly.

1. Unconstrained Optimization
"Nelder-Mead" — Simplex algorithm (derivative-fproblems, requires gradient or estimates it).
"Newton-CG" — Newton’s method using conjugate gradient (requires gradient and Hessian or Hessian-vector product).
"trust-ncg" — Trust-region Newton conjugate grree, robust but slower).
"Powell" — Powell’s conjugate direction method (derivative-free).
"CG" — Conjugate Gradient method (requires gradient or estimates it).
"BFGS" — Quasi-Newton method (good for smooth adient (requires gradient and Hessian or Hessian-vector product).
"dogleg" — Trust-region dogleg method (requires gradient and Hessian).
"trust-exact" — Trust-region exact Newton method (requires gradient and Hessian).
"trust-krylov" — Trust-region Newton method using Krylov subspace (requires gradient and Hessian-vector product).
2. Bound-Constrained Optimization
"L-BFGS-B" — Limited-memory BFGS (handles simple bounds on variables).
"TNC" — Truncated Newton Conjugate-Gradient (handles bounds).
"SLSQP" — Sequential Least Squares Programming (handles bounds and constraints).
"trust-constr" — Trust-region method for constrained optimization (handles bounds and constraints).
3. Constrained Optimization
"COBYLA" — Constrained Optimization BY Linear Approximations (derivative-free, inequality constraints only).
"SLSQP" — (also in bound-constrained) handles both equality and inequality constraints. "trust-constr" — (also in bound-constrained) handles general constraints.
Notes:
For smooth, unconstrained problems -> "BFGS" or "L-BFGS-B" (if bounds exist) are good starting points.
For non-smooth or noisy problems -> "Nelder-Mead" or "Powell".
For complex constraints -> "SLSQP" or "trust-constr".

First Example: Minimum of f(x) = (x-3)^2

Use scipy.optimize import minimize to find the minimum of an equation f(x) = (x-3)^2. We use bounds to limit the search between x = -5 and x = 5.


from scipy.optimize import minimize
import matplotlib.pyplot as plt
# Example function: f(x) = (x-3)^2
def f(x):
    return (x - 3)**2

# Initial guess is a value in an array
x0 = np.array([0.0])    

bounds = [(-5, 5)]  # lower=-5, upper=5
result = minimize(f, x0, bounds=bounds, method="L-BFGS-B")

print("Optimal x:", result.x)
print("Function value:", result.fun)
print("Success:", result.success)

#Plot the function
x = np.arange(-5,5,.1)  #start,stop,increment
funcx = (x-3)**2
plt.grid()
plt.plot(x,funcx)
plt.show()

#Output: Optimal x: [2.99999998]
#Function value: 5.318734349043978e-17
# Success: True



Min Plot f(x) = (x-3)^2

*************************************************************************************

The second example. Minimize f(x, y) = 5512026x^2 + 28471210y^2 - 5000x - 8660y
We will use SciPy’s optimization tools.

[ f(x, y) = 5512026x^2 + 28471210y^2 - 5000x - 8660y ]

Here’s a Python example using both fmin (Nelder–Mead) and minimize (BFGS) for comparison:


import numpy as np
from scipy.optimize import fmin, minimize

# Define the function to minimize
def f(vars):
    x, y = vars  # unpack variables
    return 5512026 * x**2 + 28471210 * y**2 - 5000 * x - 8660 * y

# Initial guess
x0 = [0.0001, 0.00001]

# --- Method 1: Using fmin (Nelder–Mead) ---
result_fmin = fmin(f, x0, disp=False)  # disp=False to suppress output
print("fmin result:")
print("x =", result_fmin[0], "y =", result_fmin[1])
print("Minimum value =", f(result_fmin))

# --- Method 2: Using minimize (BFGS) ---
result_minimize = minimize(f, x0, method='BFGS')
print("\nminimize (BFGS) result:")
print("x =", result_minimize.x[0], "y =", result_minimize.x[1])
print("Minimum value =", f(result_minimize.x))
print("Converged:", result_minimize.success)
   

Output:
fmin result:
x = 0.0004538586544943823 y = 0.0001508437352315518
Minimum value = -1.7923614390976188

minimize (BFGS) result:
x = 0.00045354629076498683 y = 0.00015207601048519083
Minimum value = -1.7924057072725197
Converged: False











Introduction to Engineering Python: For First Year Engineering Students