Shared05 - Differentiation Assignment / Differentiation Notes.sagewsOpen in CoCalc
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 at the point is equal to .

Since every point on the graph of 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 .

In other words, the derivative of the function , denoted or , gives the slope of the line tangent to at . Replacing with in the above limit results in the following formula for the derivative:

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

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 .

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 .

## Example 2

Find the derivative of .

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)

We see .

## 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 the particular value in parentheses after the derivative command.

Note: Sage may give you a warning message if you use the second method (this method may be disallowed in a future version of Sage).

## Example 3

Suppose we want to calculate when .

Method 1: Name the derivative function. Sage does not allow the name , 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 .

df(3)
6

Sage tells me that .

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

derivative(f,x)(3)
6

Notice that we get the same answer: .

Caution: The (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 (3) afterwards.

## Example 4

Find (using the same 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 (notice the scientific notation).

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

derivative(g(x),x)(5)
-1175*e^(-25)

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

## Example 5

Find .

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

dg(-3)
N(_)
-135*e^(-9) -0.0166603235517017

Thus, .

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

derivative(g(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 . [It's implied that the derivative is with respect to , since the function is defined as a function of .]

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

So .

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

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

## Applied Example

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 minutes is given by the function .

How fast is the object travelling at minutes?

%var t
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 ft/min.

You could also use method 2:

derivative(p(t),t)(10)
-20

## Higher-Order Derivatives

The second derivative of , denoted or , is the derivative of . In other words, or .

Similarly, the third derivative of , denoted or, is the derivative of .

After the third derivative, we stop using primes. For example, the fourth derivative is denoted and the tenth derivative is denoted .

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 , , and from above):

derivative(f(x),x,2)
2

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

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

## Example 9

Here are some higher-order derivatives (same , , and ):

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

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

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

## Example 10

Find .

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:

Method 2:

derivative(g(x),x,10)(7)
41459781162688*e^(-49)

## Applied Example

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 minutes is given by the function .

What is the object's acceleration at minutes?

%var t
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 ft/min/min = ft/min.

## 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 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 is increasing, the derivative is positive.
• When is decreasing, is negative.
• has a local minimum at , and .

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

## Example 13

Now let's look at 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 is increasing, the derivative is positive.
• When is decreasing, is negative.
• When has a local minimum or maximum, is .

Also observe the following:

• When is concave up, is increasing.
• When is concave down, is decreasing.
• When has an inflection point, 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 at has equation .

## Example 14

Suppose we want the tangent line to the function when .

An equation for the line is .

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)