Python for Engineering -

Minimum of Equation


Find Minimum for an Equation Using Fmin, and Minimize

We will use scipy.optimize minimize to solve for minimum for an equation. There are several different methods available to use with scipy.optimize minimize. They will be used in our examples. We will also use scipy.optimize fmin to find the minimums of equations. Both are in the python library. Please note that many equations have more than one minimum. The goal here is to get you started with Python methods. Suggested reading is Numerical Methods for Engineers, Fifth Edition by Chapra and Canale.

Examples of Finding Minimum for an Equation Using Fmin

The first example program uses fmin to find a minimum. Read the equation and comments in the code. Import numpy as np. Import scipy.optimize with fmin. Define the function. Create an initial guess. Use fmin with the function and best guess arguments. Print the result.

import numpy as np
from scipy.optimize import fmin

#Find minimum of x**2 + y**2 = 25
def f(x):
   return x[0]**2+x[1]**2-25
x0=[.0001,.00001] #initial guess
result=fmin(f,x0)
print(result)

#Output [1.e-04 1.e-05]
# Or at the coordinates x=0 and y=0



The second example uses scipy.optimize minimize to find the minimum. For brevity I have only shown the output for x. The fun value (y value) for the minimum is displayed in the full output which you will see when you run the program. Remember the Python programs can be copy and pasted into you PyCharm Community interface. Import minimize. Import matplotlib and numpy for plotting. Define your function. Plot your function. Make an initial guess for the x value. Run minimize. There are many methods for minimize. We have selected nelder-mead. Print the results.

#Find the minimum for the equation y=x**2 using nelder-mead
from scipy.optimize import minimize
import matplotlib.pyplot as plt
import numpy as np

def func(x):
   z = x ** 2
   return z

x = np.arange(-2, 2, 0.01)
y = func(x)
plt.title("X*X Plot")
plt.plot(x, y)
plt.grid()
plt.show()

x0 = -1 #initial guess
result = minimize(func, x0, method="nelder-mead")
print(result)

# Output x: array([8.8817842e-16])



Min Plot

The third example uses scipy.optimize minimize to find the minimum. For brevity I have only shown the output for x. The fun value for the minimum is displayed in the full output which you will see when you run the program. Remember the Python programs can be copy and pasted into you PyCharm Community interface. Import minimize. Import matplotlib and numpy for plotting. Define your function.(Note that you will see many looks to functions in programs. Here, return x refers only to the x = equation above it. Do not let it confuse you.) Plot your function. Make an initial guess for the x value. Run minimize. Print the results.

from scipy.optimize import minimize
import matplotlib.pyplot as plt
import numpy as np

def func(x):
   x = x ** 2 + 2 * np.sin(x * np.pi)
   return x

x = np.arange(-2, 2, 0.01)
y = func(x)
plt.title("Given function in x range")
plt.plot(x, y)
plt.grid()
plt.show()

x0 = -1 #initial guess
result = minimize(func, x0, method="nelder-mead")
print(result)

#Output x: array([-0.45380859])



Min Plot



The fourth example uses both scipy.optimize minimize and scipy.optimize import fmin to solve for two independent variables which give a minimum for a 3D surface. I have shown the output for the two independent variables for one method. The fun value (y value)for the minimum is displayed in the full output which you will see when you run the program. The function is fun = 5512026*x**2+28471210*y**2-5000*x-8660*y. Do not get confused with the x and y in the function. X and y are just place holders for two independent variables. When we define our function, we will set x=x[0] and y=x[1] to give fun = 5512026*x[0]**2+28471210*x[1]**2-5000*x[0]-8660*x[1] . The program uses the following steps. Import numpy, import fmin, import minimize. Define the function. The first two commented lines use fmin to determine the independent variables. The third commented line uses minimize to determine the two independent variables. The commented lines can have the hash tag removed to activate them. The results for the activated programs are shown in comments to the right of the lines. The fourth line uses minimize in the program to determine the two independent variables. Notice the results are same for the four methods.

from scipy.optimize import fmin
from scipy.optimize import minimize
# Min 5512026*x**2+28471210*y**2-5000*x-8660*y
# x=x[0] and y=x[1]
def f(x):
   return 5512026*x[0]**2+28471210*x[1]**2-5000*x[0]-8660*x[1]
x0=[.0001,.00001] #initial guess
#result=fmin(f,np.array([0,0]))
#result=fmin(f,x0) #[0.00045386 0.00015084]
#result=minimize(f,x0,method="nelder-mead") #x: array[0.00045386, 0.00015084]
result=minimize(f,x0,method="Powell") #array([0.00045355, 0.00015208])
print(result)
# Output of three values for one minimum on 3D surface.
# fun: -1.7924057091564034
# x: [ 4.536e-04 1.521e-04]



The fifth example shows how different methods of minimize give the same answer for an equation. As in the fourth example there are two independent variables. Note the two lines with the Powell method. One line contains bounds and the other has no bounds. The bounded line gives the correct answers. The unbounded line does not. Again, you can try different methods by uncommenting lines.

import numpy as np
from scipy.optimize import minimize
import math

def f(x):
   exp = (math.pow(x[0], 2) + math.pow(x[1], 2)) * -1
   return math.exp(exp) * math.cos(x[0] * x[1]) * math.sin(x[0] * x[1])
x0=[1,1] #initial guess
bnds=((0,2),(0,2)) #bounds for Powell method
#result=minimize(f,x0,method="nelder-mead") #array([1.40127745, 1.40121965])
#result=minimize(f,x0,method="Powell") #array([ 0.62665683, -0.62665707])
# *No Bounds
result=minimize(f,x0,method="Powell",bounds=bnds) #array([1.40115062, 1.4010796 ])
# *With bounds
#result=minimize(f,x0,method="CG") # array([1.40126335, 1.40126335])
#result=minimize(f,x0,method="BFGS") #array([1.40124756, 1.40124756])
#result=minimize(f,x0,method="TNC") #array([1.40124527, 1.40124527])
#result=minimize(f,x0,method="L-BFGS-B") #array([1.40128514, 1.40128514])
#result=minimize(f,x0,method="COBYLA") #array([1.40136809, 1.40095086])
#result=minimize(f,x0,method="SLSQP") #array([1.4001977, 1.4001977])
#result=minimize(f,x0,method="trust-constr") #array([1.4001977, 1.4001977])

print(result)




This website consists of example problems from numerical methods for engineers. The first examples apply to roots, plotting roots, maximums, mininums, and optimization problems. You have enough examples so that you become familiar with the syntax used in Python. The examples have been tested and the output of the programs are listed in the comments for each. All programs were run on Python using the PyCharm Community interface. They were run on an older laptop with 8GB of RAM. If you are new to Python, I recommend the manual Introduction to Engineering Python by Steve Larsen. It is availble on Amazon.











Introduction to Engineering Python: For First Year Engineering Students