9951 explained code solutions for 126 technologies


python-scipyHow do I use the Newton optimization algorithm in Python with SciPy?


The Newton optimization algorithm is a numerical method used to find the minimum of a function. It is available in Python through the SciPy library. To use it, the following code can be used:

from scipy.optimize import minimize

def func(x):
   return x**2 + 10*np.sin(x)

res = minimize(func, x0=0, method='Newton-CG')
print(res.x)

This code will produce the following output:

[-1.30644012]

The code consists of the following parts:

  1. An import of the minimize function from the scipy.optimize module.
  2. The definition of a function func that takes a single argument x and returns the value of the function to be minimized.
  3. The call to the minimize function with the function to be minimized, the initial guess for the minimum (x0) and the optimization method (Newton-CG).
  4. The printing of the result, which is the value of x that minimizes the function.

For more information on the Newton optimization algorithm, see the SciPy documentation.

Edit this code on GitHub