9. Scipy Tutorial-方程(组)求解(根)
在scipy里利用root函数可以比较容易的获得多项式方程的根,即求解方程式。
import numpy as np
from scipy import optimize
x = np.arange(-10, 10, 0.1)
def f(x):
return x ** 2 + 30 * np.sin(x)
grid = (-10, 10, 0.1)
xmin_global = optimize.brute(f, (grid, ))
print "Global minima found ", xmin_global
xmin_local = optimize.fminbound(f, 0, 10)
print "Local minimum found ", xmin_local
root = optimize.root(f, [-3.5, 0, 3.5, 6])
print "root is:",
for x1 in root.x:
print x1,
print ""
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)
ax.plot(x, f(x), 'b-', label="f(x)")
xmins = np.array([xmin_global[0], xmin_local])
ax.plot(xmins, f(xmins