Explain how the minimum of a scalar function can be found in SciPy using Python?

PythonServer Side ProgrammingProgramming

Finding the minimum of a scalar function is an optimization problem. Optimization problems help improve the quality of the solution, thereby yielding better results with higher performances. Optimization problems are also used for curve fitting, root fitting, and so on.

Let us see an example −

Example

import matplotlib.pyplot as plt
from scipy import optimize
import numpy as np
print("The function is defined")
def my_func(a):
   return a*2 + 20 * np.sin(a)
plt.plot(a, my_func(a))
print("Plotting the graph")
plt.show()
print(optimize.fmin_bfgs(my_func, 0))

Output

Optimization terminated successfully.
   Current function value: -23.241676
   Iterations: 4
   Function evaluations: 18
   Gradient evaluations: 6
[-1.67096375]

Explanation

  • The required packages are imported.
  • A function is defined that generates data.
  • This data is plotted on the graph using matplotlib library.
  • Next, the ‘fmin_bgs’ function is used by passing the function as a parameter.
  • This data is displayed on the console.
raja
Published on 10-Dec-2020 13:45:53
Advertisements