import os
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import brentq
os.chdir(r'D:\projects\wordpress\ex52')
os.getcwd()
f=lambda x: 0.2+x * np.cos(3/x)
x=np.linspace(-1,1,1000)
plt.plot(x,f(x))
plt.axhline(0,color='k')
xstar=brentq(f,-0.7,-0.5)
print(xstar,f(xstar))
plt.plot(xstar,f(xstar), 'ro')<br />
plt.text(-1.0, -0.5, 'x=%6.4f and f(x)=%6.4f' % (xstar, f(xstar)))
plt.tight_layout()
plt.savefig("example52.png", dpi=300)
plt.show()
plt.close()
Like this:
Like Loading...
Related
Recent Comments