import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
x = np.linspace(-5,3,100)
def f(x):
return 4 * x ** 3 + (x - 2) ** 2 + x ** 4
x_min_local=optimize.fmin_bfgs(f,5)
print('f(x)极小值点:',x_min_local)
Optimization terminated successfully.
Current function value: 2.804988
Iterations: 11
Function evaluations: 24
Gradient evaluations: 12
f(x)极小值点: [0.46961744]
x_max_global=optimize.fminbound(f,-10,10)
print('取得极小值时的x值:',x_max_global)
取得极小值时的x值: -2.6729805844842622
plt.plot(x,f(x))
plt.show()