-{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[{"file_id":"1oUxmJATa0Uq1li9Se_97HTTiGwW55Qcu","timestamp":1662057266795}],"collapsed_sections":[],"authorship_tag":"ABX9TyMmra+2RsSvCPgH/8UW4HUL"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","metadata":{"id":"bhWV8oes-wKR"},"source":["# COURSE: Master calculus 1 using Python: derivatives and applications\n","## SECTION: Applications\n","### LECTURE: CodeChallenge: 2nd derivative test\n","#### TEACHER: Mike X Cohen, sincxpress.com\n","##### COURSE URL: udemy.com/course/pycalc_x/?couponCode=202108"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"D52Je4VqT0TK"},"outputs":[],"source":[]},{"cell_type":"code","source":["import numpy as np\n","import sympy as sym\n","import matplotlib.pyplot as plt\n","\n","from IPython.display import display,Math\n","\n","# better image resolution\n","from IPython import display as disPlay\n","disPlay.set_matplotlib_formats('svg')"],"metadata":{"id":"cnKqevEET1J1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":[],"metadata":{"id":"Bf67Cy3LUaNc"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Exercise 1: Compute and plot the derivatives "],"metadata":{"id":"AzF0PRxEUasq"}},{"cell_type":"code","source":["x = sym.symbols('x')\n","\n","# the function and its derivatives\n","f = x**4 - 8*x**2\n","df = sym.diff(f,x)\n","ddf = sym.diff(f,x,2)\n","\n","# find f'=0\n","critPoints = np.array( sym.solve(df) )\n","critPoints"],"metadata":{"id":"9Y1ypw7iRAI3"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# lambdify and plot\n","f_l = sym.lambdify(x,f)\n","df_l = sym.lambdify(x,df)\n","ddf_l = sym.lambdify(x,ddf)\n","\n","xx = np.linspace(-3,3,101)\n","\n","plt.plot(xx,f_l(xx),label=\"f\")\n","plt.plot(xx,df_l(xx),label=\"f'\")\n","plt.plot(xx,ddf_l(xx),label=\"f''\")\n","plt.plot(critPoints,df_l(critPoints),'o',label=\"f'=0\")\n","plt.plot(critPoints,ddf_l(critPoints),'d',label=\"f''(f'=0)\")\n","plt.plot(xx[[0,-1]],[0,0],'k--',linewidth=.2)\n","\n","plt.ylim([-50,50])\n","plt.xlim(xx[[0,-1]])\n","plt.legend()\n","plt.show()"],"metadata":{"id":"8tcoLaElRkRl"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":[],"metadata":{"id":"tXCNFt5wRkU0"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Exercise 2: Implement the 2nd derivative test"],"metadata":{"id":"8zKsMZwPRkXz"}},{"cell_type":"code","source":["for cp in critPoints:\n","\n"," # find the sign of the second derivative\n"," cpSign = np.sign( ddf_l(cp) )\n","\n"," # print out the result\n"," if cpSign==-1:\n"," display(Math('d^2f(%s) < 0, \\\\text{ so it is a local maximum.}' %cp))\n"," elif cpSign==1:\n"," display(Math('d^2f(%s) > 0, \\\\text{ so it is a local minimum.}' %cp))\n"," elif cpSign==0:\n"," display(Math('d^2f(%s) = 0, \\\\text{ so the test is inconclusive.}' %cp))"],"metadata":{"id":"Y6hhr2uZYksc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":[],"metadata":{"id":"oSdDN561UpQV"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Exercise 3: Put it in a function"],"metadata":{"id":"1HUXzKF-UpTC"}},{"cell_type":"code","source":["def secondDerivTest(f):\n","\n"," # compute derivatives\n"," df = sym.diff(f,x)\n"," ddf = sym.diff(f,x,2)\n","\n"," # find f'=0\n"," critPoints = np.array( sym.solve(df) )\n","\n"," # abort if no critical points!\n"," if len(critPoints)==0:\n"," print('No critical points! Aborting function!')\n"," return\n","\n","\n"," # lambdify and plot\n"," f_l = sym.lambdify(x,f)\n"," df_l = sym.lambdify(x,df)\n"," ddf_l = sym.lambdify(x,ddf)\n","\n"," xx = np.linspace(-3,3,101)\n","\n"," plt.plot(xx,f_l(xx),label=f\"$f = {sym.latex(f)}$\")\n"," plt.plot(xx,df_l(xx),label=f\"$f' = {sym.latex(df)}$\")\n"," plt.plot(xx,ddf_l(xx),label=f\"$f'' = {sym.latex(ddf)}$\")\n"," plt.plot(critPoints,df_l(critPoints),'o',label=\"f'=0\")\n"," plt.plot(critPoints,ddf_l(critPoints),'d',label=\"f''(f'=0)\")\n"," plt.plot(xx[[0,-1]],[0,0],'k--',linewidth=.2)\n","\n"," plt.xlim(xx[[0,-1]])\n"," plt.legend()\n"," plt.show()\n","\n"," # report the results of the 2nd derivative test\n"," for cp in critPoints:\n","\n"," # find the sign of the second derivative\n"," cpSign = np.sign( ddf_l(cp) )\n","\n"," # print out the result\n"," if cpSign==-1:\n"," display(Math('d^2f(%s) < 0, \\\\text{ so it is a local maximum.}' %cp))\n"," elif cpSign==1:\n"," display(Math('d^2f(%s) > 0, \\\\text{ so it is a local minimum.}' %cp))\n"," elif cpSign==0:\n"," display(Math('d^2f(%s) = 0, \\\\text{ so the test is inconclusive.}' %cp))"],"metadata":{"id":"7yLNNYV4UrDs"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# test\n","f = x**4 - 8*x**2\n","secondDerivTest(f)"],"metadata":{"id":"JvTl_gfNUrGp"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":[],"metadata":{"id":"BOK90tuha1_n"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Exercise 4: Try with a few different functions"],"metadata":{"id":"8BEY3fKOUrJi"}},{"cell_type":"code","source":["secondDerivTest(x**4)"],"metadata":{"id":"h9Wb5rsERkar"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["secondDerivTest(-x**4)"],"metadata":{"id":"sdQu3j1gRkdi"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["secondDerivTest(x**3)"],"metadata":{"id":"u-H_A8ohpmH0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["secondDerivTest( 2*sym.pi*x + sym.sin(sym.pi*x) )\n","# problem is due to complex numbers; redefine x to have real=True"],"metadata":{"id":"_zMs3yXYdrGp"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":[],"metadata":{"id":"f07EItITdrVw"},"execution_count":null,"outputs":[]}]}
0 commit comments