Shared05 - Differentiation Assignment / Differentiation Notes.sagewsOpen in CoCalc
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


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


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:

%var h

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


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


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

Example 2

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

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 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 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].


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


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

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


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

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 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).


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


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:


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).

-135*e^(-9) -0.0166603235517017

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

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


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
\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.]

\displaystyle x^{2}
\displaystyle x
\displaystyle 1

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 t minutes is given by the function p(t)=-3t^2+40t+5 .

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

%var t
v(t)=derivative(p(t),t)  #I will call this function v for velocity

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

You could also use method 2:


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):



\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)}

\displaystyle 2 \, a


Example 9

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

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


show(derivative(g(x),x,5)) #fifth derivative
-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


Example 10

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

Method 1:

d10g(x)=derivative(g(x),x,10) #define the derivative function first
41459781162688*e^(-49) 2.17368892264044e-8

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

Method 2:


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 t minutes is given by the function p(t)=-3t^2+40t+5 .

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

%var t
a(t)=derivative(p(t),t,2) #I will call this function a for acceleration.

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.



  • 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.

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
y= \displaystyle -20 \, {\left(x - 2\right)} e^{\left(-4\right)} + 8 \, e^{\left(-4\right)}