+{"cells":[{"cell_type":"markdown","metadata":{"id":"Brtl6ZgJqgu2"},"source":["# COURSE: Master math by coding in Python\n","# SECTION: Calculus\n","# VIDEO: Calculus BUG HUNT! (This is the file with bugs to find/fix.)\n","\n","\n","### https://www.udemy.com/course/math-with-python/?couponCode=202312\n","#### INSTRUCTOR: Mike X Cohen (http://sincxpress.com)\n","\n","This code roughly matches the code shown in the live recording: variable names, order of lines, and parameter settings may be slightly different."]},{"cell_type":"markdown","source":["<a target=\"_blank\" href=\"https://colab.research.google.com/github/mikexcohen/MathWithPython/blob/main/calculus/mathWithPython_calc_BUGHUNT.ipynb\">\n"," <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n","</a>"],"metadata":{"id":"JZhUk_36xwBy"}},{"cell_type":"code","execution_count":1,"metadata":{"id":"KFfqVGpviYuH","executionInfo":{"status":"ok","timestamp":1698836437423,"user_tz":-480,"elapsed":1089,"user":{"displayName":"Mike X Cohen","userId":"13901636194183843661"}}},"outputs":[],"source":["import sympy as sym\n","import numpy as np\n","import matplotlib.pyplot as plt\n","from IPython.display import display,Math"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7eN_TTBviYuN"},"outputs":[],"source":["# evaluate a function in a range\n","from sympy.abc import x\n","\n","fx = (4*x**3 + 2*x**2 - x) / (-4*x**4 + 2*x**2)\n","\n","xrange = np.linspace(-2,2,200)\n","fxx = sym.lambdify(x,fx)\n","\n","plt.plot(xrange,fxx)\n","plt.ylim([-20,20])\n","plt.xlim(xrange[[0,-1]])\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Ol1zNug9iYuN"},"outputs":[],"source":["# compute the limit\n","x = sym.symbols('x')\n","fx = 1/(x+3)\n","\n","lim_pnt = -3\n","lim = sym.limit(x,fx,lim_pnt,dir='+')\n","\n","display(Math('\\\\lim_{x\\\\to %g^+} %s = %g' %(lim_pnt,sym.latex(fx),sym.latex(lim))))"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"P7o21pxuiYuO"},"outputs":[],"source":["# piece-wise function\n","from sympy.abc import x\n","\n","piece1 = x**2\n","piece2 = 4*sym.exp(-x**2)\n","\n","# put them together with conditions\n","fx = sym.Piecewise( piece1,x<0 , [piece2,x>=0) )\n","\n","# evaluate the function in a range\n","xx = np.linspace(-2,2,1000)\n","fxx = sym.lambdify(x,fx)\n","\n","# show it in a plot\n","plt.plot(xx,fxx(x),'k')\n","\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_WyVkwakiYuP"},"outputs":[],"source":["# show the first and second derivatives of sin(x)\n","x = np.linspace(-2*np.pi,2*np.pi,200)\n","dt = np.diff(x[0:2])\n","\n","y = np.sin(x)\n","dy = np.diff(y)\n","ddy = np.diff(y,2)\n","\n","plt.plot(x,y,label='y')\n","plt.plot(x,dy,'--',label='dy',alpha=.6)\n","plt.plot(x,ddy,':',label='d$^2$y',alpha=.3)\n","\n","plt.legend(framealpha=1)\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0KAOneROiYuP"},"outputs":[],"source":["# Compute critical points using sympy\n","\n","x = sym.symbols('x')\n","fx = x**2 * sym.exp(-x**2)\n","\n","# derivative in sympy, solve\n","dfx = sym.diff(fx,x)\n","critpoints = sym.solve(fx)\n","print('The critical points are: ' + str(critpoints))\n","\n","\n","# plot the function derivative and its critical points\n","y = sym.lambdify(x,dfx)\n","xx = np.linspace(-3,3,200)\n","\n","plt.plot(xx,y(xx))\n","plt.plot([-3,3],[0,0],'k--')\n","plt.xlim([-3,3])\n","\n","for i in critpoints:\n"," plt.plot(i,0,'ro')\n","\n","plt.title('Function derivative')\n","plt.show()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JGSMTOe4iYuQ"},"outputs":[],"source":["# Compute the area between two curves (not the same thing as Between Two Ferns)\n","from matplotlib.patches import Polygon\n","\n","\n","x = sym.symbols('x')\n","f1sym = sym.cos(x)\n","f2sym = x\n","\n","xx = np.linspace(0,np.pi/3,100)\n","f1 = np.cos(xx)\n","f2 = xx\n","\n","fintersect = np.argmin(abs(f1-f2))\n","\n","\n","traceX = np.concatenate((xx[0:fintersect],xx[fintersect:0:-1]))\n","traceY = np.concatenate((f1[0:fintersect],f2[fintersect:0:-1]))\n","\n","points = np.vstack((traceX,traceY)).T\n","p = Polygon(points,facecolor='k',alpha=.3)\n","\n","fig, ax = plt.subplots()\n","ax.add_patch(p)\n","\n","plt.plot(xx,f1, xx,f2)\n","plt.show()\n"]},{"cell_type":"code","source":[],"metadata":{"id":"pHw-iRMYildX"},"execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.4"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":0}
0 commit comments