 CoCalc Public Files05 - Differentiation Assignment / Differentiation Notes.sagews
Author: Aaron Tresham
Views : 756
Compute Environment: Ubuntu 20.04 (Default)
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is ### Prerequisites:

• Intro to Sage
• Graphing and Solving Equations
• Tangent Lines

# Differentiation

Last time we saw that the slope of the line tangent to a function $f(x)$ at the point $(x_0,f(x_0))$ is equal to $\displaystyle\lim_{h\to 0}\frac{f(x_0+h)-f(x_0)}{h}$.

Since every point on the graph of $f$ has a different tangent line, the slope of the tangent line may change. To keep track of these slopes, we define a new function, called the "derivative" of $f$.

In other words, the derivative of the function $f(x)$, denoted $f'(x)$ or $\frac{d}{dx}f(x)$, gives the slope of the line tangent to $f$ at $x$. Replacing $x_0$ with $x$ in the above limit results in the following formula for the derivative: $f'(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$

Note: The slope of the tangent line is also the (instantaneous) rate of change of the function. So you can think of the derivative $f'$ as giving the rate of change of $f$.

In this lab, we will learn how to use Sage to calculate derivatives (called "differentiation"), and we will look at graphs of the derivative function.

## Calculating the Derivative Function

To calculate the derivative of a function in Sage, we use the derivative command. This command requires two arguments: (1) a function name or an expression and (2) the variable with respect to which the derivative is to be calculated.

## Example 1

Find the derivative of $f(x)=x^2$.

We could do this using limits:

f(x)=x^2
%var h
limit((f(x+h)-f(x))/h,h=0)

2*x

However, instead of using limits it will be easier to use the derivative command:

derivative(x^2,x)

2*x

You can also define a function first, and use the function name in the derivative command.

f(x)=x^2
derivative(f(x),x)

2*x

Whichever way we do it, we see that $f'(x)=2x$.

## Example 2

Find the derivative of $g(x)=x^3e^{-x^2}$.

g(x)=x^3*e^(-x^2)
derivative(g(x),x)
show(_) #The show command will make the output look nicer.

-2*x^4*e^(-x^2) + 3*x^2*e^(-x^2)
$\displaystyle -2 \, x^{4} e^{\left(-x^{2}\right)} + 3 \, x^{2} e^{\left(-x^{2}\right)}$

We see $g'(x)=-2x^4e^{-x^2}+3x^2e^{-x^2}$.

## Calculating a Particular Value of the Derivative

There are two ways to calculate the value of the derivative at a particular point.

1. Give the derivative function a name, and then use it to calculate the particular value.

2. Add (x=particular value) in parentheses after the derivative command.

## Example 3

Suppose we want to calculate $f'(3)$ when $f(x)=x^2$.

Method 1: Name the derivative function. Sage does not allow the name $f'(x)$, so I like to use df(x) [d for derivative].

f(x)=x^2
df(x)=derivative(f,x)


Now that I have defined my derivative function df(x), I can use it to evaluate $f'(3)$.

df(3)

6

Sage tells me that $f'(3)=6$.

Method 2: Add (x=3) after the derivative command:

f(x)=x^2
derivative(f,x)(x=3)

6

Notice that we get the same answer: $f'(3)=6$.

Caution: The (x=3) must come after the closing parenthesis of the derivative command. There is no place for the 3 within the derivative command. You must substitute 3 after finding the derivative, either by naming the derivative function or by putting (x=3) afterwards.

## Example 4

Find $g'(5)$ (using the same $g(x)$ from above).

Method 1: Give the derivative a name, such as dg. Then we simply compute dg(5).

g(x)=x^3*e^(-x^2)
dg(x)=derivative(g(x),x)
dg(5)

-1175*e^(-25)

Use the N() command if you want a numerical approximation:

N(dg(5))

-1.63183340413327e-8

So $g'(5)=-1175e^{-25}\approx-1.63\times10^{-8}=-0.0000000163$ (notice the scientific notation).

Method 2: Add the particular value after the derivative command:

g(x)=x^3*e^(-x^2)
derivative(g(x),x)(x=5)

-1175*e^(-25)

Of course, we get the same answer as we got from method 1.

## Example 5

Find $g'(-3)$.

If you already have a dg function (method 1), then just compute dg(-3).

dg(-3)
N(_)

-135*e^(-9) -0.0166603235517017

Thus, $g'(-3)\approx -0.016660$.

If you are using method 2, then compute the derivative again:

derivative(g(x),x)(x=-3)

-135*e^(-9)

If you have many particular values to calculate, method 1 is more efficient. If you need only one particular value, then method 2 involves less typing.

Personally, I prefer method 1. I think method 1 more clearly shows what you are doing.

## Derivatives with Other Variables

Sage is able to handle derivative when other variables are involved. All variables other than the variable of differentiation (the variable after the comma in the derivative command) are treated as constants (unspecified numbers).

## Example 6

Find the derivative of $F(x)=ax^2+bx+c$. [It's implied that the derivative is with respect to $x$, since the function is defined as a function of $x$.]

%var a,b,c
F(x)=a*x^2+b*x+c
show(derivative(F(x),x))

$\displaystyle 2 \, a x + b$

So $F'(x)=2ax+b$.

Of course, if we take the derivative with respect to $a$, $b$, or $c$ we will get a different answer. [This is awkward, since F is written as a function of $x$. This is not a normal Calculus 1 question, but Sage is able to deal with it.]

show(derivative(F(x),a))

$\displaystyle x^{2}$
show(derivative(F(x),b))

$\displaystyle x$
show(derivative(F(x),c))

$\displaystyle 1$

## Applied Example - Velocity

The rate of change of position with respect to time (called velocity) is the derivative of the position function.

## Example 7

The position (in feet) of an object moving in a straight line after $t$ minutes is given by the function $p(t)=-3t^2+40t+5$.

How fast is the object travelling at $t=10$ minutes?

p(t)=-3*t^2+40*t+5
v(t)=derivative(p(t),t)  #I will call this function v for velocity
v(10)

-20

The objects's velocity is $-20$ ft/min.

You could also use method 2 (note that we have t=10, not x=10):

p(t)=-3*t^2+40*t+5
derivative(p(t),t)(t=10)

-20

## Higher-Order Derivatives

The second derivative of $f(x)$, denoted $f''(x)$ or $\frac{d^2}{dx^2}f(x)$, is the derivative of $f'(x)$. In other words, $f''(x)=(f')'(x)$ or $\frac{d^2}{dx^2}f(x)=\frac{d}{dx}\left(\frac{d}{dx}f(x)\right)$.

Similarly, the third derivative of $f(x)$, denoted $f'''(x)$ or$\frac{d^3}{dx^3}f(x)$, is the derivative of $f''(x)$.

After the third derivative, we stop using primes. For example, the fourth derivative is denoted $f^{(4)}(x)=\frac{d^4}{dx^4}f(x)$ and the tenth derivative is denoted $f^{(10)}(x)=\frac{d^{10}}{dx^{10}}f(x)$.

If you want the second, third, etc. derivative in Sage, you need to add one more argument to the derivative command.

## Example 8

Here are some second derivatives (using the same $f$, $g$, and $F$ from above):

derivative(f(x),x,2)

2

$f''(x)=2$

show(derivative(g(x),x,2))

$\displaystyle 4 \, x^{5} e^{\left(-x^{2}\right)} - 14 \, x^{3} e^{\left(-x^{2}\right)} + 6 \, x e^{\left(-x^{2}\right)}$

$g''(x)=4 \, x^{5} e^{\left(-x^{2}\right)} - 14 \, x^{3} e^{\left(-x^{2}\right)} + 6 \, x e^{\left(-x^{2}\right)}$

show(derivative(F(x),x,2))

$\displaystyle 2 \, a$

$F''(x)=2a$

## Example 9

Here are some higher-order derivatives (same $f$, $g$, and $F$):

derivative(f(x),x,3) #third derivative

0

$f'''(x)=0$

show(derivative(g(x),x,5)) #fifth derivative

$\displaystyle -32 \, x^{8} e^{\left(-x^{2}\right)} + 400 \, x^{6} e^{\left(-x^{2}\right)} - 1320 \, x^{4} e^{\left(-x^{2}\right)} + 1140 \, x^{2} e^{\left(-x^{2}\right)} - 120 \, e^{\left(-x^{2}\right)}$

$g^{(5)}(x)=-32 \, x^{8} e^{\left(-x^{2}\right)} + 400 \, x^{6} e^{\left(-x^{2}\right)} - 1320 \, x^{4} e^{\left(-x^{2}\right)} + 1140 \, x^{2} e^{\left(-x^{2}\right)} - 120 \, e^{\left(-x^{2}\right)}$

derivative(F(x),x,4) #fourth derivative

0

$F^{(4)}(x)=0$

## Example 10

Find $g^{(10)}(7)$.

Method 1:

d10g(x)=derivative(g(x),x,10) #define the derivative function first
d10g(7)
N(d10g(7))

41459781162688*e^(-49) 2.17368892264044e-8

Notice the scientific notation again: $g^{(10)}(7)\approx 2.17\times 10^{-8}=0.0000000217.$

Method 2:

derivative(g(x),x,10)(x=7)

41459781162688*e^(-49)

## Applied Example - Acceleration

Acceleration is the rate of change of velocity. In other words, acceleration is the second derivative of the position function.

## Example 11

The position (in feet) of an object moving in a straight line after $t$ minutes is given by the function $p(t)=-3t^2+40t+5$.

What is the object's acceleration at $t=10$ minutes?

p(t)=-3*t^2+40*t+5
a(t)=derivative(p(t),t,2) #I will call this function a for acceleration.
a(10)

-6

The acceleration is $-6$ ft/min/min = $-6$ ft/min$^2$.

## Graphs of Derivatives

We are going to graph a function and its derivative and compare.

## Example 12

Let's start with a simple example and look at the graphs of $f(x)=x^2$ and its derivative.

f(x)=x^2
df(x)=derivative(f(x),x)
plot(f,xmin=-4,xmax=4,legend_label='f')+plot(df,xmin=-4,xmax=4,color='red',legend_label='f\'') Notice:

• When the original function $f$ is increasing, the derivative $f'$ is positive.
• When $f$ is decreasing, $f'$ is negative.
• $f$ has a local minimum at $x=0$, and $f'(0)=0$.

Caution: There is no relationship between the direction (increasing/decreasing) of $f$ and the direction of $f'$. The relationship is between the direction of $f$ and the sign (positive/negative) of $f'$.

## Example 13

Now let's look at $g(x)=x^3e^{-x^2}$ and its derivative.

g(x)=x^3*e^(-x^2)
dg(x)=derivative(g(x),x)
plot(g,xmin=-4,xmax=4,legend_label='g')+plot(dg,xmin=-4,xmax=4, color='red',legend_label='g\'') Again we observe the same relationship:

• When the original function $g$ is increasing, the derivative $g'$ is positive.
• When $g$ is decreasing, $g'$ is negative.
• When $g$ has a local minimum or maximum, $g'$ is $0$.

Also observe the following:

• When $g$ is concave up, $g'$ is increasing.
• When $g$ is concave down, $g'$ is decreasing.
• When $g$ has an inflection point, $g'$ has a local minimum or maximum.

## Tangent Lines

Last time we used limits to find the slopes of tangent lines. Now we can use the derivative to find the slope.

In general, the tangent line to $f$ at $x = x_0$ has equation $y=f(x_0)+f'(x_0)(x-x_0)$.

## Example 14

Suppose we want the tangent line to the function $g(x)=x^3e^{-x^2}$ when $x=2$.

An equation for the line is $y=g(2)+g'(2)(x-2)$.

g(x)=x^3*e^(-x^2)         #First define g
dg(x)=derivative(g(x),x)  #Then define the derivative function
TL(x)=g(2)+dg(2)*(x-2)    #Now define the tangent line
show('$y=$',TL(x))
plot(g,xmin=0,xmax=4)+plot(TL,xmin=0,xmax=4,color='red')+point((2,g(2)),color='black',size=25)

$y=$ $\displaystyle -20 \, {\left(x - 2\right)} e^{\left(-4\right)} + 8 \, e^{\left(-4\right)}$ ## Applied Example - Population Growth

Given a function $P(t)$ that gives the population at time $t$, the derivative $P'(t)$ gives you the rate of change of the population - how quickly the population is increasing (or decreasing).

## Example 15

The population $P$ of Sageville $t$ years after its founding is modelled by the equation $\displaystyle P(t)=\frac{4000}{1+399e^{-0.1t}}.$

At what rate was the population increasing 100 years after its founding?

P(t)=4000/(1+399*e^(-0.1*t))
dP(t)=derivative(P,t)
dP(100)

6.99028304391498

The population is growing at a rate of approximately 7 persons per year 100 years after the founding of Sageville.

Note: A common mistake here is to claim that the population is growing at a rate of "7%," but this growth rate is not a percentage. The correct units are "persons/year." In fact, 7 persons is less than 0.2% of the population at this time.

## Example 16

Find a formula for the tangent line of $P(t)$ above at $t=100$. Graph $P$ and its tangent line.

TL(t)=P(100)+dP(100)*(x-100)
plot(P(t),xmin=0,xmax=150)+plot(TL(t),xmin=0,xmax=150,color='red')+point((100,P(100)),color='black',size=20) ## Example 17

Calculate $P''(100)$ for the population model above. What does this quantity represent in this context?

d2P(t)=derivative(P(t),t,2)
d2P(100)

-0.674153700135403

$P''(100)\approx -0.67$.

This means that the rate of increase of the population is declining at a rate of about $0.67$ persons/year per year 100 years after the founding of Sageville.

Although the population is still increasing $(P'(100)>0)$, its not increasing as rapidly as before $(P''(100)<0)$.