
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum of Scalar Function in SciPy Using Python
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.
Advertisements