- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain how the minimum of a scalar function can be found 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 −
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))
Optimization terminated successfully. Current function value: -23.241676 Iterations: 4 Function evaluations: 18 Gradient evaluations: 6 [-1.67096375]
- 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.
Kickstart Your Career
Get certified by completing the courseGet Started