import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-3.14, 3.14, 0.02)
y = np.sin(x) + x * x / 2 - 1
plt.plot(x, y)
plt.show()
for i in range(0, len(y) - 1):
if y[i] >-0.01 and y[i] <0.01:
print(x[i])
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-np.pi,np.pi,1000)
y=np.sin(x)+x**2/2.0-1
e=0.0001 #set error range
roots=[]
for i in range(len(x)-1):
if np.sign(y[i]) != np.sign(y[i+1]):
roots.append(find_root(x[i],x[i+1],e))
plt.plot(x,y)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
def function(x):
return np.sin(x)+x**2/2.0-1
def find_root(a,b,e):
y1=function(a)
y2=function(b)
if np.abs(y1)<=e or np.abs( y2)<=e:
return a if np.abs(y1)<np.abs(y2) else b
c=(a+b)/2.0
y3=function(c)
if np.sign(y1)==np.sign(y3):
return find_root(c,b,e)
else:
return find_root(a,c,e)
x=np.linspace(-np.pi,np.pi,1000)
y=function(x)
e=0.0001 #set error range
roots=[]
for i in range(len(x)-1):
if np.sign(y[i]) != np.sign(y[i+1]):
roots.append(find_root(x[i],x[i+1],e))
plt.plot(x,y)
plt.title("root:"+str(roots))
print("root: ",roots)
plt.show()
总是被恶意举报,牛逼你提供更好的答案出来,别整这些没用的!!!