Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Lecture 4 : Calculus and power series
References:
Summary:
In this lecture, we start by recalling how new functions can be defined in Python. Then we show how to plot functions in SageMath and discuss concepts from calculus (or analysis) like limits, derivatives and integrals. We finish by discussing power series, which are a generalization of the Taylor series from analysis.
Python warmup: functions
We have already seen many functions implemented in SageMath (such as log
, span
or solve
). But it is also possible (and often very useful) to write functions ourselves.
This creates a new function parabola
, which we can immediately call on some value:
The general code pattern for defining a function foo
with inputs (or arguments) looks as follows:
As was the case with if
and for
, we have a block of indented code (the body of the function) in which we can do what we want (e.g. create new variables, call other functions, use if
and for
). Most functions foo
should in the end give back some result, which we do using the return
keyword.
Let's see a function with more than one argument:
Exercise
Define the following functions and test them in some examples.
the function
sign
returning the sign of a real numberthe function
conjugate
, taking -matrices and returning
Solution (uncomment to see)
We can also have functions with optional arguments. These are arguments which can be given, but don't have to be given when calling the function. However, in exchange you need to provide a default value for each of them. Let's generalize the function parabola
from above to implement the function :
As we see above, if a function gets a number of arguments which is smaller than the total number of fixed and optional arguments, it simply fills the optional arguments in the order that was specified in the function definition.
If we want to explicitly specify the argument c
above, but leave a
to take the default value, we can do so by explicitly specifying the name of the argument that we want:
The general pattern looks like this:
Here, it is important that the non-optional arguments come before the optional ones.
Exercise
Implement a function fibonacci
computing the Fibonacci-Numbers defined by Add the option to specify by hand.
Solution (uncomment to see)
Calculus
SageMath can do many computations related to the infinitesimal behaviour of functions, such as limits, derivatives and integrals. In each case, the function should be represented by a symbolic expression, as seen in the last lecture. Since it is useful for illustrating the later computations (and in general), we'll start however with some information how to plot functions (a.k.a. producing pretty pictures).
Plotting
The most basic task here is to plot a function in one variable on an interval. For instance, we can give the function as a symbolic expression in x
:
Alternatively, we can program a function as seen in the first part of the lecture:
Most of the time it's more useful to use the symbolic expressions, though.
A curiosity: if you type plot(g(x),x,-5,5)
it does not do what you want. If you understand what goes wrong there, you already have a pretty good grasp on symbolic expressions!
Exercise
Plot your favorite function on an interval of your choice.
Plot your second-favorite function on the interval , but in green.
Hint: Since unfortunately I forgot to say how to change the color, you will have to find it out yourself!
Solution (uncomment to see)
As you can guess, there are lots more optional arguments for the plot
function, which you can again find in the documentation.
The function plot
actually returns a Graphics-object and you can literally add multiple such objects to plot multiple functions:
Three more variants of plots:
Exercise
Consider the following system of equations:
Plot the two solution sets to the two equations in separate colors (in the same coordinate system).
Solve this system of equations (using the methods from last lecture) and verify graphically that you found all solutions in the first part of the exercise.
To spare you the typing, here are the left-hand sides of the equations:
Solution (uncomment to see)
Limits
For a function given by a symbolic expression, we can compute its limit at a given point as follows:
Exercise
Compute the limit and check with a plot that this is plausible.
Solution (uncomment to see)
We can also deal with divergent functions, one-sided limits and limits as goes to :
Derivatives and integrals
Of course SageMath can differentiate (symbolic) functions, and for many functions it can also integrate them. Here are three ways to obtain the derivative:
To get the derivative at a specific point, we can evaluate the symbolic expression there, as seen in the last lecture:
Exercise
Write a function plot_tangent
which takes as input
a symbolic expression
f
of a function in the variablex
a point
x0
and outputs a plot of the function f
on the interval together with its tangent line at x0
.
Bonus: If you are done early, feel free to include optional parameters to change the interval of plotting, the colour of the tangent line etc.
Solution (uncomment to see)
Of course we can also compute partial derivatives for functions depending on multiple parameters:
Here is an example of how to do indefinite integration:
The syntax for a definite integral like is as follows:
To apply the things we learned, here is a task from an actual Analysis II-exam at ETH Zurich (see Exercise 3).
Exercise
Let . Let the function be defined by Find the global maxima and minima of for
.
.
Hint: The function slopes down to zero towards infinity.
In case you have forgotten how these extreme value problems work, you can of course look it up on wikipedia.
Solution (uncomment to see)
Finally, we can also do some calculus involving vectors. Here is the parametrization of a spiral:
The arc length of the path given by
There are many more interesting functions, and we don't have time to go through all of them. In particular, see this tutorial on vector calculus in SageMath. There, you learn how to define a vector field, compute its divergence and curl, etc.
Power series
Power series are a very useful part of mathematics. Typically one first encounters them in the form of the Taylor series of a smooth function around the point . However, it is also possible to do calculations with more general series The easiest way to compute with them in SageMath is to create a PowerSeriesRing
, with some formal variable t
. Here you have to specify a base ring, which is the ring containing the possible coefficients above.
Then, to compute the Taylor series of the function at , we can just plug the variable t
of our power series ring into the function f
.
By default, this computes it to order , but this can be changed as follows:
Note that default_prec=n
means the last coefficient that is computed is the one for , which can be somewhat confusing ...
The nice thing about power series is that we can add and multiply them, and the Taylor series of is equal to the product of the series for and :
To get the coefficient of of such a power series , we can type P[k]
:
A cool application of power series are so-called generating functions. Given a sequence of real numbers, we say that a function is a generating function for this sequence, if is the Taylor series of at .
Exercise
The Wikipedia page of the Fibonacci numbers claims that the function is a generating function for these numbers. Check below if the first coefficients of the Taylor series of are correct, and use this method to compute the nd Fibonacci number.
Solution (uncomment to see)
Assignments
Exercise (short)
Solve Exercise 2 b) from the Analysis II-exam above.
Remark: I couldn't get SageMath to solve Exercise 2 a) since it involves some third roots, which are kind of tricky.
Solution (uncomment to see)
Exercise
A partition of a natural number is one representation of as an ordered sum of positive integers. For instance, the number has different partitions: If denotes the number of partitions of , then the infinite product is a generating function of .
(theoretical) Use the formula for the geometric series to show that the finite product is a generating series for the number of partitions of with maximal part at most .
In particular, since for we have , this explains why the infinite product above is a generating series for .Use this knowledge to write a function
partition_number(n, m=None)
to compute the numbers .
Remark: In the above case, it is useful to set the optional parameterm
to the Python valueNone
as default. Then, at the beginning of the function, you should check whetherm
isNone
and if yes give it a reasonable value.Check your function in a few cases (e.g. using the enumeration of partitions of above).
Solution (uncomment to see)