Maximum of Equation -

Engineering Numerical Methods


Find MAXIMUM for an Equation Using Fmin, and Minimize

We will use scipy.optimize minimize to solve for maximum of an equation. To do so we multiply the function by negative one, and proceed as usual.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 maximums of equations. To do so we multiply the function by negative one, and proceed as usual. Both are in the python library. Please note that many equations have more than one maximum. The goal here is to get you started with Python methods. Suggested reading is Numerical Methods for Engineers, Fifth Edition by Chapra and Canale.



Example of Finding Maximum of Equation Using Minimize and Fmin

In this example we will determine X to maximize the equation 2*sin(X) – X**2/2. There are two methods using fmin, and two methods using minimize to solve the problem. A method can be selected by adding and removing the pound signs. The pound signs comment out the lines you want to ignore. Notice that the program looks the same as that for minimization. The only difference is that we multiply the equation by negative one in the function definition. We must use the negative sign because scipy does not have a function for maximization.

First we import numpy as np for plotting, import math, import matplotlib for plotting, and scipy.optimize minimize. Next we define the function. Multiply the function by negative one. We enter an initial guess. The next four lines begin with result and a pound sign. (These four lines end with the X result). Add or remove pound signs to activate the line with the method you want to test. Print the results. Finally plot the function. Use np.arange with an initial value of x, a final value of x, and the interval. Enter the equation. Plot X on the horizontal axis, and Y on the vertical axis.

import numpy as np
from scipy.optimize import fmin
import math
import matplotlib.pyplot as plt
from scipy.optimize import minimize
# Max 2*sin(x)-x**2/2
# We take the -1 of the function for maximum
def f(x):
   return -1*(2*math.sin(x)-x**2/2)
x0=[1.5] #initial guess
#result=fmin(f,np.array([0])) #[1.029875]
#result=fmin(f,x0) # [1.0298584]
#result=minimize(f,x0,method="nelder-mead") # x: array([1.0298584])
result=minimize(f,x0,method="Powell") # x: array([1.02986653])
print(result)

x=np.arange(-5,5,.01)
y=2*np.sin(x)-x**2/2
plt.plot(x,y)
plt.show()
# Output: fun: -1.1841480025558875
# x: [ 1.030e+00]
# x=1.03, y=-1.184



Min Plot

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 have never used 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