Sage Notebook "derivatives of functions"

lecture Theoretical Mechanics IPSP

Universität Leipzig, winter term 2020

Author: Jürgen Vollmer (2020)

lizence: Creative Commons Attribution-ShareAlike 4.0 International CC BY-SA 4.0

Sage is an OpenSource Project that implements a broach range of computer-based mathematics and computer algebra in a Jupyter notebook.

Documentation and information on the installation of Sage can be found at https://sagemath.org

A very nice introduction to working with Sage is given in the book Paul Zimmermann, u.a.: "Computational Mathematics with SageMath"

In [1]:
# declare variable that will be used 
x = var("x")

determine and show the 1st derivative of $\sin x$

In [2]:
derivative( sin(x), x )
Out[2]:
cos(x)

determine and show the 3rd derivative of $\exp(2x)$

remark: type and execute 'derivative( exp(2*x), x, 3 )' into the empty field below in order to see why I added the show statement

In [3]:
D3 = derivative( exp(2*x), x, 3 )
show(D3)
In [ ]:
 

show the sine function and its first 3 derivatives

remark: type and execute 'range(4)' into the empty field below if you do not know what is doing

In [4]:
[ derivative( sin(x), x, k) for k in range(4) ]
Out[4]:
[sin(x), cos(x), -sin(x), -cos(x)]
In [ ]:
 

define your own function $f(x)$, and determine its first 5 derivatives

the text in between the triple quotes """ ... """ is a comment that describes what the function is doing

  • in the present case this is so obvious that you won't need it, but for more complex functions it is extremely important, because it helps you recall what happened here when you need the function again after a few weeks

  • for more complex functions you should therefore also choose a name that is clearly describing the purpose of the function

In [6]:
def f (x) :
    """
    argument:   x
    result:     the function returns the value  x^4 - x^2
    """
    return x^4 - x^2
In [7]:
show([ derivative( f(x), x, k) for k in range(6) ])
In [8]:
## or: store the derivaties in the array 'diffs_of_f'
diffs_of_f = [ derivative( f(x), x, k) for k in range(6) ]

## and request the third derivative by stating
diffs_of_f[3]
Out[8]:
24*x
In [ ]: