a,x = sym.symbols('a,x')
baseexpr = x**2 * sym.exp(-a*x**2)
arange = np.arange(0,2.25,.25)
print(arange)
xrange = np.linspace(-3,3,100)
# setup the figure
fig,ax = plt.subplots(1,2) # (row, col)
print(fig)
print(ax)
for ai in arange:
fx = baseexpr.subs(a,ai)
dfx = sym.diff(fx)
# print(dfx)
critpnts = sym.solve(dfx)
# print(critpnts)
ax[0].plot(xrange,sym.lambdify(x,fx)(xrange))
ax[1].plot(xrange,sym.lambdify(x,dfx)(xrange))
# print(critpnts)
if 1 in critpnts:
display(Math('\\Rightarrow %s \\text{ has a critical point at } x=1' %sym.latex(fx)))
elif 2 in critpnts:
display(Math('\\Rightarrow %s \\text{ has a critical point at } x=2' %sym.latex(fx)))
else:
display(Math('\\quad %s \\text{ has NO critical point at x=1 or x=2}' %sym.latex(fx)))
ax[0].set_ylim([0,2])
ax[0].set_title('Function')
ax[0].plot([1,1],[0,2],'--',color='gray')
ax[0].plot([2,2],[0,2],'--',color='gray')
ax[1].set_ylim([-2,2])
ax[1].set_title('Derivative')
ax[1].plot([1,1],[-2,2],'--',color='gray')
ax[1].plot([2,2],[-2,2],'--',color='gray')
plt.show()