YouTip LogoYouTip

Scipy Optimize

SciPy's `optimize` module provides implementations of commonly used numerical optimization and equation solving algorithms. We can directly call these functions to solve practical problems, such as finding the minimum/maximum value of a function, or solving equation roots, etc. NumPy itself mainly focuses on array computing and linear algebra operations, it provides tools for **polynomial root finding** (such as `numpy.roots`), but does not provide a general **nonlinear equation numerical root finding interface**. When we need to solve general nonlinear equations, we need to use SciPy's optimization module. For example, the following nonlinear equation: x + cos(x) For such problems, you can use the `scipy.optimize.root` function to solve. The two core parameters of this function are: * **fun** - represents the equation function (returning 0 means the root of the equation is found) * **x0** - initial guess for the root The `root` function returns a result object (`OptimizeResult`), which contains solution status, success flag, function value, and final solution information. The actual solution is stored in the `x` attribute of the returned object, as shown in the example: ## Example Find the root of equation `x + cos(x) = 0`: from scipy.optimize import root from math import cos def eqn(x): return x + cos(x) myroot = root(eqn,0) print(myroot.x) # View more information # print(myroot) Execute the above code, and the output is as follows: [-0.73908513] If you directly print the returned object, you can view more complete solution information: ## Example from scipy.optimize import root from math import cos def eqn(x): return x + cos(x) myroot = root(eqn,0) print(myroot) Execute the above code, and the output is as follows: fjac: array([[-1.]]) fun: array([0.]) message: 'The solution converged.' nfev: 9 qtf: array([-2.66786593e-13]) r: array([-1.67361202]) status: 1 success: True x: array([-0.73908513]) Among them: * **success** indicates whether the algorithm converged successfully * **message** describes the result of the solving process * **x** is the final solved equation root ### Minimizing Functions In mathematics and engineering problems, functions can be represented as curves, and curves often have highs and lows. The high points of a curve are called **maximum values**. The low points of a curve are called **minimum values**. The highest point in the entire curve is called the **global maximum**, and the other points that are higher but not the highest are called **local maximums**. Similarly, the lowest point in the entire curve is called the **global minimum**, and the other points that are lower but not the lowest are called **local minimums**. In SciPy, you can use the scipy.optimize.minimize() function to find the minimum value of a function. The commonly used parameters of the `minimize()` function are as follows: * **fun** - the objective function to be minimized * **x0** - initial guess for the independent variable * **method** - the name of the optimization algorithm to use, common optional values include: `'CG'`, `'BFGS'`, `'Newton-CG'`, `'L-BFGS-B'`, `'TNC'`, `'COBYLA'`, `'SLSQP'` * **callback** - callback function called after each iteration * **options** - a dictionary of other control parameters, for example: { "disp": boolean, # whether to print detailed optimization process information "gtol": number # gradient convergence tolerance (applicable for some algorithms)} ## Example Minimize the function `xΒ² + x + 2` using the BFGS method: from scipy.optimize import minimize def eqn(x): return x**2 + x + 2 mymin = minimize(eqn,0, method='BFGS') print(mymin) Execute the above code, and the output is as follows: fun: 1.75 hess_inv: array([[0.50000001]]) jac: array([0.]) message: 'Optimization terminated successfully.' nfev: 8 nit: 2 njev: 4 status: 0 success: True x: array([-0.50000001]) From the result, you can see that the function achieves its minimum value at `x β‰ˆ -0.5`, with the corresponding minimum function value being `1.75`, which is consistent with the analytical calculation result.
← Vue3 BuildScipy Module β†’