CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
calculuslab

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: calculuslab/Calculus_Lab
Path: blob/main/141-Labs/Lab 05 - Differentiation and Tangent Lines.ipynb
Views: 491
Kernel: SageMath 9.2

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

ParseError: KaTeX parse error: Undefined control sequence: \hfill at position 32: …|l|l|} \hline \̲h̲f̲i̲l̲l̲ ̲\textbf{Command…
Sections 3.1 and 3.2.

Recall the point-slope form of the equation of the line: yy1=m(xx1),y - y_1 = m(x-x_1), where (x1,y1)(x_1, y_1) is a point on the line and mm is the slope of the line. Next, since (x1,f(x1))(x_1, f(x_1)) is on the tangent line, we can substitute y1=f(x1)y_1 = f(x_1) and move it to the other side. Therefore, we get: y=m(xx1)+f(x1).y = m(x-x_1) + f(x_1). Finally, we know that the derivative evaluated at x1x_1 is the same as the slpoe of the tangent line to the graph of y=f(x)y = f(x) at x1x_1. Thus, we get the following formula for the equation of the tangent line to the graph of y=f(x)y = f(x) at x1x_1: y=f(x1)(xx1)+f(x1).y = f'(x_1)(x-x_1) + f(x_1).

Example 1

In the previous lab, we learned how to use limits and the difference quotient to calculate the derivative of f(x)f(x). In SageMath, a more direct way is to use the diff(f(x),x)\textbf{diff}(f(x),x) command to calculate f(x)f'(x). This command can also be used to find higher order derivatives. The command diff(f(x),x,n)\textbf{diff}(f(x), x, n) will calculate f(n)(x),f^{(n)}(x), the nthn^\text{th} derivative of f(x)f(x).

Let f(x)=x10f(x) = x^{10}. Use SageMath to find the following:

  1. f(x)f'(x)

  2. f(x)f''(x)

  3. f(x)f'''(x)

  4. f(10)(x)f^{(10)}(x)

Suppose we want to calculate f(3)f'(3). One thought might be to try diff(f(3),x)\textbf{diff}(f(3),x).

diff(f(3),x)

Note that this output is wrong. If we use this command, SageMath first calculates f(3)f(3), and then takes the derivative of this constant which resulted in 0. Instead, we could do one of the following:

  1. We can let df(x)=f(x)df(x) = f'(x) and then evaluate df(3)df(3).

  2. We can have SageMath calculate f(x)f'(x), and then use our command (expression)(x=3)(\textit{expression})\textbf{(x=3)} to evaluate the function f(x)f'(x) at x=3.x=3.

df(x) = diff(f(x),x) df(3)
(diff(f(x),x))(x=3)

Example 2

Find f(x)f'(x), f(10)f''(10), and f(10)(π)f^{(10)}(\pi) for the following functions:

  1. f(x)=x3+2x5f(x) = x^3 + 2x - 5

  1. f(x)=xcos(x)2f(x) = x\cos(x)^2

  1. f(x)=sin(cos(tan(x)))f(x) = \sin(\cos(\tan(x)))

Example 3

Consider the function f(x)=x2f(x) = x^2. We will use SageMath to find the equation of the tangent line of f(x)f(x) at x=1x = 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)f(1) and f(1)f'(1).

def f(x): return x^2 f(1)
df(x) = diff(f(x),x) df(1)

Recall from the notes at the beginning of this lab that the equation of the tangent line of f(x)f(x) at x=1x = 1 is y=f(1)(x1)+f(1).y = f'(1)(x-1) + f(1). We use SageMath to find this line.

y = df(1)*(x-1) + f(1) y

Therefore, y=2x1y = 2x - 1 is the equation of the tangent line of f(x)f(x) at x=1x = 1. We now plot both f(x)f(x) and the tangent line on the same graph. Choose a domain which has the xx-value x=1x = 1 in the center.

plot([f(x),y], xmin = 0, xmax = 2, color=['blue', 'red'], linestyle=['-', '--'])

Example 4

Repeat Example 3 with the following functions:

  1. f(x)=x3+2x2+1f(x) = x^3 + 2x^2 + 1 at x=1x = -1

  1. f(x)=2xf(x) = 2^x at x=2x = 2

  1. f(x)=cos(x)f(x) = \cos(x) at x=π4x = \frac{\pi}{4}