Mastering Differentiation and Tangent Lines with SageMath

School
University of South Carolina**We aren't endorsed by this school
Course
MATH 141
Subject
Mathematics
Date
Dec 12, 2024
Pages
8
Uploaded by AmbassadorThunder15627
10/24/24, 10:35 AM Lab 05 - Differentiation and Tangent Lines Lab 05 - Differentiation and Tangent Lines Overview In this lab, we will learn how to use SageMath to find derivatives and the equation of the tangent line to a curve at a given point. Important SageMath Commands Introduced in this Lab Command Description Example diff(f(z), x) Finds the derivative of f(x) diff(z?, ) diff(f(z),z,n) | Find the n*® derivative of f(z) | diff(sin(z), z, 100) Related Course Material Sections 3.1 and 3.2. Recall the point-slope form of the equation of the line: y—y1 = m(z 1), where (21, ¥1) is a point on the line and m is the slope of the line. Next, since (x1, f(x1)) is on the tangent line, we can substitute y; = f(1) and move it to the other side. Therefore, we get: y=m(z —z1) + f(z1). Finally, we know that the derivative evaluated at x; is the same as the slpoe of the tangent line to the graph of y = f(x) at 1. Thus, we get the following formula for the equation of the tangent line to the graph of y = f(«) at x1: y = f'(z1)(x 1) + f(21). Example 1 In the previous lab, we learned how to use limits and the difference quotient to calculate the derivative of f(x). In SageMath, a more direct way is to use the diff( f(x), ) command to calculate f'(x). This command can also be used to find higher order derivatives. The command diff( f(z), z,n) will calculate £ (z), the n'® derivative of f(z). Let f(z) = =Y. Use SageMath to find the following: 1. f'(z) https://hub.ovh2.mybinder.org/user/calculuslab-calculus_lab-gvh8nxx0/nbconvert/html/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb?dow... 1/8
Background image
10/24/24, 10:35 AM In [12]: out[12]: In [13]: Out[13]: In [14]: Out[14]: In [15]: Out[15]: In [16]: Out[l6]: In [18]: Out[18]: In [19]: Out[19]: 2. f"(z) 3. fl//(w) 4. f(lo)(zc) def f(x): return x*10 £(x) X"10 diff(f(x),x) 10*x"9 diff(f(x),x,x) 90*x"8 diff(f(x),x,x,x) 720%x"7 diff(f(x),10) 3628800 Suppose we want to calculate f'(3). One thought might be to try diff( f(3), x). diff(f(3),x) 0 Note that this output is wrong. If we use this command, SageMath first calculates f(3), and then takes the derivative of this constant which resulted in 0. Instead, we could do one of the following: Lab 05 - Differentiation and Tangent Lines 1. We can let df(z) = f'(z) and then evaluate df(3). 2. We can have SageMath calculate f'(x), and then use our command (expression)(x=3) to evaluate the function f'(z) at z = 3. df(x) = diff(f(x),x) df(3) 196830 (diff(f(x),x))(x=3) 196830 https://hub.ovh2.mybinder.org/user/calculuslab-calculus_lab-gvh8nxx0/nbconvert/html/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb?dow... 2/8
Background image
10/24/24, 10:35 AM In [22]: In [23]: Out[23]: In [24]: Out[24]: In [25]: Out[25]: In [26]: Out[26]: In [27]: Out[27]: In [28]: Out[28]: In [29]: Out[29]: In [30]: Out[30]: Lab 05 - Differentiation and Tangent Lines Example 2 Find £'(x), f"(10), and £ (7) for the following functions: 1.flz)=2°4+2x—5 def f(x): return x*3+2%*x-5 f(x) diff(f(x),x) 3*x"2 + 2 df (x)=diff(f(x),x,x) df(10) 60 diff(f(x),x,x)(x=10) 60 diff(f(x),10)(x=pi) 0 2. f(x) = x cos(z)* def £(x): return Xx*cos(x)A2 £(x) X*C0S (X)A2 diff(f(x),x) 2%x*Cos(x)*sin(x) + Cos(x)A2 diff(F(x),x,x)(x=18) -20%c0s(10)"2 - 4*cos(10)*sin(10) + 20*sin(10)72 diff(F(x),10)(x=pi) -512*pi 3. f(x) = sin(cos(tan(x))) https://hub.ovh2.mybinder.org/user/calculuslab-calculus_lab-gvh8nxx0/nbconvert/html/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb?dow... 3/8
Background image
10/24/24, 10:35 AM In [31]: Out[31]: In [32]: Out[32]: In [33]: Out[33]: In [34]: Out[34]: In [35]: Out[35]: In [36]: Out[36]: In [37]: Out[37]: In [38]: Lab 05 - Differentiation and Tangent Lines def f(x): return sin(cos(tan(x))) f(x) sin(cos(tan(x))) diff(f(x),x) -(tan(x)"2 + 1)*cos(cos(tan(x)))*sin(tan(x)) diff(f(x),x,x)(x=10) -(tan(10)~2 + 1)~2*sin(cos(tan(10)))*sin(tan(10))”2 - (tan(10)”2 + 1)"2*cos(cos(ta n(10)))*cos(tan(10)) - 2*(tan(10)"2 + 1l)*cos(cos(tan(10)))*sin(tan(10))*tan(10) diff(f(x),10)(x=pi) 83931*cos(1) - 213375*sin(1) Example 3 Consider the function f(z) = x%. We will use SageMath to find the equation of the tangent line of f(x) at x = 1 and to plot both the function and the tangent line. In order to find the equation of the tangent line, we need to find both f(1) and f'(1). def f(x): return x”2 f(1) 1 df(x) = diff(f(x),x) df(1) 2 Recall from the notes at the beginning of this lab that the equation of the tangent line of flz)atz =1isy = f'(1)(x 1) + f(1). We use SageMath to find this line. y = df(1)*(x-1) + f(1) y 2*¥x - 1 Therefore, y = 2z 1 is the equation of the tangent line of f(z) at x = 1. We now plot both f(x) and the tangent line on the same graph. Choose a domain which has the z-value x = 1 in the center. plot([f(x),y], xmin = @, xmax = 2, color=['blue', 'red'], linestyle=['-', '--"]) https://hub.ovh2.mybinder.org/user/calculuslab-calculus_lab-gvh8nxx0/nbconvert/html/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb?dow... 4/8
Background image
10/24/24, 10:35 AM Lab 05 - Differentiation and Tangent Lines Out[38]: Example 4 Repeat Example 3 with the following functions: 1.fz) =2 +22°+1latz = —1 In [39]: def f(x): return x*3+2*x”2+1 f(-1) Out[39]: 2 Tn [40]: df(x)=diff(f(x),x) df(-1) Outf[40]: -1 In [43]: y=df(-1)*(x+1)+f(-1) y Out[43]: -x + 1 In [44]: plot([f(x),y],xmin=-2,xmax=2,color=[ ‘blue', 'red’'],linestyle=["-","--"]) https://hub.ovh2.mybinder.org/user/calculuslab-calculus_lab-gvh8nxx0/nbconvert/html/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb?dow... 5/8
Background image
10/24/24, 10:35 AM Lab 05 - Differentiation and Tangent Lines Out[44]: 15 - 10 - - - ---- - - - - - --, -~ e - -- -—- 2. f(x) =2"atx = 2 In [45]: def f(x): return 27x f(2) Out[45]: 4 In [46]: df(x)=diff(f(x),x) df(2) Out[4671: 4*log(2) In [47]: y=df(2)*(x-2)+f(2) y Outl[47]: 4*(x - 2)*log(2) + 4 In [48]: plot([f(x),y],xmin=-2,xmax=4,color=[ ‘blue', 'red’'],linestyle=["-","--"]) https://hub.ovh2.mybinder.org/user/calculuslab-calculus_lab-gvh8nxx0/nbconvert/html/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb?dow... 6/8
Background image
10/24/24, 10:35 AM Lab 05 - Differentiation and Tangent Lines Out[48]: 3. f(x) = cos(z) atz = & In [49]: def f(x): return cos(x) f(pi/4) Out[49]: 1/2*sqgrt(2) In [52]: df(x)=diff(Ff(x),x) df(pi/4) Out[52]: -1/2*sqrt(2) In [53]: y=df(pi/4)*(x-(pi/4))+f(pi/4) y Out[53]: 1/8*sgrt(2)*(pi - 4*x) - 1/2*sqrt(2) In [54]: plot([f(x),y],xmin=-pi/2,xmax=pi/2,color=["'blue’, 'red'],linestyle=["-","--"]) https://hub.ovh2.mybinder.org/user/calculuslab-calculus_lab-gvh8nxx0/nbconvert/html/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb?dow... 7/8
Background image
10/24/24, 10:35 AM Lab 05 - Differentiation and Tangent Lines Out[54]: 1.0 1 g 0.5 - —1.5 -1.0 ~0.5 b+ 0.5 1.0 15 https://hub.ovh2.mybinder.org/user/calculuslab-calculus_lab-gvh8nxx0/nbconvert/html/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb?dow... 8/8
Background image