CoCalc Public Files05 - Differentiation Assignment / Differentiation Notes.sagewsOpen with one click!
Author: Aaron Tresham
Views : 756
License: Other -- explicitly state in your code
Compute Environment: Ubuntu 20.04 (Default)
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)f(x) at the point (x0,f(x0))(x_0,f(x_0)) is equal to limh0f(x0+h)f(x0)h\displaystyle\lim_{h\to 0}\frac{f(x_0+h)-f(x_0)}{h}.

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

In other words, the derivative of the function f(x)f(x), denoted f(x)f'(x) or ddxf(x)\frac{d}{dx}f(x), gives the slope of the line tangent to ff at xx. Replacing x0x_0 with xx in the above limit results in the following formula for the derivative: f(x)=limh0f(x+h)f(x)hf'(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 ff' as giving the rate of change of ff.

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)=x2f(x)=x^2.

We could do this using limits:

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

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.

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

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

Example 2

Find the derivative of g(x)=x3ex2g(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)
2x4e(x2)+3x2e(x2)\displaystyle -2 \, x^{4} e^{\left(-x^{2}\right)} + 3 \, x^{2} e^{\left(-x^{2}\right)}

We see g(x)=2x4ex2+3x2ex2g'(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)f'(3) when f(x)=x2f(x)=x^2.

Method 1: Name the derivative function. Sage does not allow the name f(x)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)f'(3).


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

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

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

Notice that we get the same answer: f(3)=6f'(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)g'(5) (using the same g(x)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)

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


So g(5)=1175e251.63×108=0.0000000163g'(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)

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

Example 5

Find g(3)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)0.016660g'(-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)=ax2+bx+cF(x)=ax^2+bx+c. [It's implied that the derivative is with respect to xx, since the function is defined as a function of xx.]

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

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

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

x2\displaystyle x^{2}
x\displaystyle x
1\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 tt minutes is given by the function p(t)=3t2+40t+5p(t)=-3t^2+40t+5.

How fast is the object travelling at t=10t=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)

The objects's velocity is 20-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)

Higher-Order Derivatives

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

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

After the third derivative, we stop using primes. For example, the fourth derivative is denoted f(4)(x)=d4dx4f(x)f^{(4)}(x)=\frac{d^4}{dx^4}f(x) and the tenth derivative is denoted f(10)(x)=d10dx10f(x)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 ff, gg, and FF from above):



4x5e(x2)14x3e(x2)+6xe(x2)\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)=4x5e(x2)14x3e(x2)+6xe(x2)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)}

2a\displaystyle 2 \, a


Example 9

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

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


show(derivative(g(x),x,5)) #fifth derivative
32x8e(x2)+400x6e(x2)1320x4e(x2)+1140x2e(x2)120e(x2)\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)=32x8e(x2)+400x6e(x2)1320x4e(x2)+1140x2e(x2)120e(x2)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)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)2.17×108=0.0000000217.g^{(10)}(7)\approx 2.17\times 10^{-8}=0.0000000217.

Method 2:


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

What is the object's acceleration at t=10t=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)

The acceleration is 6-6 ft/min/min = 6-6 ft/min2^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)=x2f(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\'')


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

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

Example 13

Now let's look at g(x)=x3ex2g(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 gg is increasing, the derivative gg' is positive.
  • When gg is decreasing, gg' is negative.
  • When gg has a local minimum or maximum, gg' is 00.

Also observe the following:

  • When gg is concave up, gg' is increasing.
  • When gg is concave down, gg' is decreasing.
  • When gg has an inflection point, gg' 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 ff at x=x0x = x_0 has equation y=f(x0)+f(x0)(xx0)y=f(x_0)+f'(x_0)(x-x_0).

Example 14

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

An equation for the line is y=g(2)+g(2)(x2)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=y= 20(x2)e(4)+8e(4)\displaystyle -20 \, {\left(x - 2\right)} e^{\left(-4\right)} + 8 \, e^{\left(-4\right)}

Applied Example - Population Growth

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

Example 15

The population PP of Sageville tt years after its founding is modelled by the equation P(t)=40001+399e0.1t.\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)

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)P(t) above at t=100t=100. Graph PP 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)P''(100) for the population model above. What does this quantity represent in this context?

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

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

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

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