Python fsolve查找函数的零,但也查找局部最小值/最大值。我只想画域[-3,5]上的零
下面是通过imgur显示的输出图 我想在域[-3,5]上绘制函数的所有零。但是,除了零之外,fsolve还为我提供了本地分钟和本地最大值:Python fsolve查找函数的零,但也查找局部最小值/最大值。我只想画域[-3,5]上的零,python,Python,下面是通过imgur显示的输出图 我想在域[-3,5]上绘制函数的所有零。但是,除了零之外,fsolve还为我提供了本地分钟和本地最大值: import warnings warnings.filterwarnings('ignore', 'The iteration is not making good progress') import matplotlib.pyplot as plt import numpy as np import math import time from scipy
import warnings
warnings.filterwarnings('ignore', 'The iteration is not making good progress')
import matplotlib.pyplot as plt
import numpy as np
import math
import time
from scipy.optimize import fsolve
#
#
# if using a jupyter notebook
%matplotlib inline
def f(x):
return np.sin(3*math.pi*np.cos(2*math.pi*x) * np.sin(math.pi*x))
#
x0 = np.linspace(-3, 5, 40000, endpoint=True)
t = time.time()
q = np.zeros(np.shape(x0))
# print(len(x0))
for i in range(40000):
q[i] = fsolve(f, x0[i], full_output=False)
ts = time.time() -t
print(ts)
qq = np.unique(q)
print(q)
AA = np.shape(qq)
print(AA)
plt.rcParams['figure.figsize'] = [15, 7]
xx = np.arange(-3,5,0.008) # start,stop,step
def f(x):
return np.sin(3*math.pi*np.cos(2*math.pi*x) * np.sin(math.pi*x))
plt.plot(xx,f(xx))
plt.plot(qq,f(qq),'ro')
plt.yticks(np.arange(min(f(xx)), max(f(xx))+1, 1.0))
plt.xlim(-3, 5)
plt.show()