Chapter 4 - Calculus
Chapter 4 - Calculus (An intro to the zoo of functions via SageMath)
4.1. Introduction to Polynomials (Evaluation and Division)
4.2. Interpolation and applications
4.3. Limits of sequences and functions
4.4. Continuous functions and applications
4.5. Derivatives
4.6. Elementary theorems on derivatives
4.7. Series
4.8. Taylor series
4.9. Integration
This chapter is devoted to introductory notions from calculus and analysis. In calculus the fundamental objects that we deal with are functions.
Below we begin with material on polynomial functions. Recall that a function is called polynomial, if it has the form where is a nonnegative integer, and the numbers , , , are constants, called the coefficients of the polynomial. If the leading coefficient is non-zero, , then is called the degree of . For example, a line with slope is a polynomial of degree 1, hence such a polynomial is a linear function. On the other hand, a quadratic function (parabola) , with is a polynomial of degree 2.
SageMath provides an enjoyable framework to study functions and their applications, and the same applies for polynomials. We begin with an important notion in the theory of polynomials, the so-called , having a variety of interesting applications in numerical analysis (numerical methods, and hence also to other sciences).
4.1. Introduction to Polynomials (Evaluation and Division)
As we proncounced in this section we will focus on interpolation of polynomials. As we will verify below, for most of the cases that we are interested in, Sage succesfully applies and simplifies the computations in such procedures (as most of the mathematical packages, e.g., Mathematica, Matlab or Maple). Nowadays it is well know that such packages become essentiall for realizing the beauty of numerical methods. Therefore, we should expect that most of them contain well-fixed routines to treat interpolation methods, and our goal below is to describe the situation for SageMath (at least, and this has many similarities with the implementation in Mathematica of polynomial interpolation).
The first series of exercises is about a classical topic, namely the classical Horner scheme, which is about the division of polynomials with applications in their evaluation.
Example
Given the polynomial
define coefficients as follows:
Then and it follows that Let us now use a bit of programming in Sage, in order to implement the situation.
In other words,
Exercise
Use the routine constructed above, to determine all roots of the polynomial .
Solution:
Remark
We can perform algebraic operations on polynomials in python using the 'numpy' library.
4.2. Interpolation and applications
Often, we may have some data but the function that generates the data is unknown. In such problems we try to fit a certain class of functions to the data. The process of fitting a function to some given data is called interpolation. The most usual class of functions for such a procedure are the polynomials (this is because polynomials have the nice property of approximating any continuous function -- Weierstrass Approximation Theorem). The process of fitting a polynomial through given data is called . The next series of exercises is about the so-called . The Lagrange interpolation method provides a direct approach for determining interpolated values regardless of the data points spacing, that is, it can be fitted to unequally spaced or equally spaced data.
1) Lagrange interpolation
Consider some distinct points (for simplicity you may think these points as real numbers). Next we want to find a polynomial of degree not greater than , which takes a prescribed value at , for all . Such a polynomial is given by , with for any . Since we have and for any , it follows that
The polynomial is known as the Lagrange interpolation polynomial, while the polynomials are called elementary Lagrange polynomials.
For instance:
If two points , are given then one has and with for and some given points the polynomials are given respectively by
In an analogous way are treated the cases with , etc. If for some function , then is referred to as the Lagrange interpolation polynomial for .
Exercise (Lagrange interpolation polynomial)
Using polynomial interpolation, present an approximate formula of the sine function, using the known values of at the points , , , , . Make a plot comparing the interpolation polynomial with .
Solution
We have the table Thus by applying the method based on elementary Lagrange polynomials we can compute the interpolation polunomial for the sin function. In SageMath this goes as follows:
We see from the given plots that for the given values and from to , the interpolation polynomial is accurate enough.
Exercise (Elementary Lagrange polynomials)
Consider the nodes , , and the values , , . Write down the corresponding Lagrange interpolation polynomial , and provide its plot, together with the plots of the elementary Lagrange polynomials , along with the given points.
Solution:
Three points are given, hence the Lagrange interpolation polynomial is at most of degree two. The elementary Lagrange polynomials are given by
Thus we compute , and and now we can write down the interpolation polynomial , as follows: To verify this in Sage and plot the same time the given data, we have used the cell:
Exercise
Prove that the Lagrange interpolation polynomial for the data given below, is of degree two:
Solution:
There are given 4 points , so the Lagrange interpolation polynomial should be of degree . We will use Sage to show that in fact it has degree :
Thus the interpolation polynomial under question is given by .
Exercise
Consider the function and the points , , . Construct the Lagrange interpolation polynomial , to approximate . Next find the difference between the approximation and the real value.
Solution:
We use SageMath and the command to obtain the Lagrange interpolation polynomial. We have
Thus and is of degree two. Let us now compare the approximation with the real value:
The difference between the approximation and the real value is given by the absolute value . We compute
** Remark (on the use of scipy)**
We can apply the Lagrange intepolation method in SageMath also by using and importing the as follows:
Above we are looking for the Lagrange interpolation polynomials for the data , , , and , which we introduced in Sage in terms of - and -coordinates. The answer returned by Sage should be read as . Finally, note that we may also type
Exercise for practice
Find via the method of the Lagrange interpolation polynomial for the data , , , .
2) Hermite interpolation
Rougly speaking Hermite interpolation polynomials are polynomials that interpolate data that may includes their first derivatives. More particular assume that are given:
pairwise distinctint nodes, say ,
The values ,
The first derivatives , where and in general by we denote the (first) derivative of a function .
Then we may express by the sum
where and are the fundamental Hermite interpolation polynomials of first and second type, given by
respectively. Here we have and are the fundamental Lagrange polynomials introduced above (for ).
Again, as in the case of Lagrange interpolation we can use the above method to interpolate some given function at some given points.
Exercise
Consider the function . Given 5 distinct points in the real line, write a code in Sage which will return the Hermite interpolation polynomial corresponding to these nodes and the values and , for (for simplicity you may fix some 5-tuple ).
Solution:
Since the Hermite interpolation method includes derivatives, next we are goint to use commands as , which gives the derivative of a function and , returning the th derivative of (see also below for more details on how one can treat derivatives in SageMath). So, let us fix the nodes
Below we write the code in such a way that changing only the very first line, i.e., giving different values to the 5-tuple , will immediately produce the corresponding Hermite interpolation polynomial , together with the plots of the function and that of .
In fact one case see the explicit form of Hermite interpolation polynomials of 1st and 2nd type, after typing (for instance, for the 1st type)
Let us focus on these polynomials for a few, for instance on . Let us find its coefficients:
This means that is of degree and has no term of first order and no constant coefficient, as we can see also based on the command :
Let us now check
Lets us now plot the Hermite polynomials of first and of second type seperately:
We proceed with another example:
As we can see has much more complicated form than the previous case:
3) Splines
Let us finally present a series of problems related to splines and in particular (naturall) cubic splines.
Our guide here is Section 5.1.9 by Brisk Guide to Mathematics.
Exercise
Find and plot the spline for the points for .
Solution:
We present the solutin directly via Sage Math
Exercise
Plot the spline corresponding to the data , , , , , and . Next compute its value at
the points , , . Can we compute also its value at the point ? Then compute the area underneath the spline.
Solution:
Hence we cannot compute the value of the spline at points that are not included in between the given points.
Now, to compute the area underneath the spline we should apply an integration (look in Section 3.9 for more details and further applications of integration).
Here is another example:
4.3. Limits of sequences and functions
Next we are interested in studying the convergence of given sequences or computing limits of functions. We begin with sequences.
Recall that a sequence is an enumerated collection of objects in which repetitions are allowed and order matters. Sequences can be viewed as functions , where as usual denotes the set of natural numbers (we often include as a natural), and is a subset of real numbers. We often denote a sequence by , where is its th-term. In the previous Chapter we met sequences defining by recursion, as the Fibonacci sequence,
However below we will treat more general sequences. An important property of a sequence is convergence, and this is what we are interested in below. If a sequence converges, it converges to a particular value known as the limit. If a sequence , converges to some limit, then it is called convergent, and moreover this limit is unique, denoted by . A sequence that does not converge is called divergent. An example of a divergent sequence is given by .
Example (Limits of sequences)
Try to verify via SageMath that the sequence converges to zero (for example, plot the first 100 terms of the sequece at hand).
Solution:
We have already seen in Chapter 1 how to plot sequences, via a method combining the with the command. For our case one can give the cell
Hence should converge to . To comfirm this fact via Sage use the command as follows (see also below)
Exercise
Prove that the seqence tends to as .
Solution:
We need to show that
We may multiple both the numerators and denominator of by . This gives the claim since as . To verify this via Sage we may use the command or its alias , as follows (see also below)
Exercise
Prove that , where . Next plots its first 100 terms.
Solution:
Let us now plot the first 100 temrs of and illustrate the convergence to zero: Notice, on could type
Recall however that a sequence is a function which has the natural numbers as domain, thus to get the correct graph of you should use the method presented above.
Exercise
Compute the following limits in SageMath:
, where .
, where .
, wherer .
Solution:
The solutions can be obtained sing the command , or its alias .
Hence , where is the Euler number (the base of the natural logarithm) (a classical result in calculus).
Limits of 1-variable functions.
The sequences are special examples of functions defined on the subset of natural numbers . Next we treat limits of more general real-valued functions (with one variable).
In SageMath an appropriate command for computing limits of a given function is again based on the command and has the form where . The option ``direction'' is to study the limits to the left or to the right. This option can be omitted, but in case we need to use it can be introduced as or , or and , respectively.
Remark.
By default, Sage calculates the limit to the left.
Example
Evaluate the limit .
Solution
It is well-know that , so we expect that the given limit equals to 4. Indeed, in Sage we verify this as follows:
Or just type
Let us also verify the claim by computing the left and right limits.
Since the left and right limit of are both equal to 4, the claim follows.
Some remarks on the computation of limits
If are functions on with and , for all , we can write Thus, if , for all , and , for all , then supposing the limit on the right-hand side exists. This is an approach that we often apply to compute limits of functions given as fractions.
Squeeze Theorem
Another very useful result for the computation of limits is the so called Squeeze Theorem, which states the following:
Let be an interval containing the point . Let be functions defined on , except possibly at itslef. Suppose that for any we have that Then .
Exercise
Compute the following limits (note that some of them may not exist - specify them)
when , , and .
.
.
.
.
(and also the two-side limits, as above).
(and also the two-side limits).
.
(and also the two-side limits).
.
.
.
.
.
Solution:
For the first case, one may apply the cell
In case you like a decimal presentation, proceed as usual, i.e.,
For the case similarly we compute
and for via the same method we obtain
In the second case we get an indefinite of type . Hence we can apply l'Hopital's rule (see below) to compute this limit by hand. By SageMath we get directly the result as follows:
Let us present the rest limits in question.
This answer means that the limit is undetermined, i.e., it doesn't exist. It may be useful to plot this function, to understand this result in a visual way.
Hence although the left and right limits exist, they are not equal and hece this gives another explanation
why the limit in case 3. does not exist.
This is a very classical limit that everyone should know to prove also in a formal way (such one is based on the squeeze theorem).
7,
We can illustrate this result by plotting the function , in the usual way
We leave the rest few cases for practice.
4.4. Continuous functions
We now proceed with tasks on the continuity of functions. We recall first some basic definitions.
Definition. Let be a function, . is continuous at if and is continuous on if for all , is continuous at .
Example
Consider the function defined as: Show that is continuous.
Solution:
If then which coincides with . Same argument applies when . It remains to show that .
One-sided limits at coincide: they are both equal to . Combining this with the relation ,we deduce that is also continuous at .
Exercise
Consider the function defined as: Specify the values of the parameters and such that is continuous at .
Solution:
Exercise for practice
Consider the piecewise function defined below directly in Sage. Examine its continuity at .
Exercise
Consider the function defined as: Specify the values of the parameter such that is continuous at .
Solution:
We present the solution directly in Sage, without any comments (try to confirm the mathematical computation behind the code below)
Intermediate Value Theorem
Let be a continuous function. Then takes any given value between and at some point within .
Corollary
If then there is such that .
Bisection method
There is a simple algorithm to find an approximation of this that is called bisection method.
Example
Find by approximating the root of on the interval via the method described above.
4.5. Derivatives
We proceed with tasks on derivatives. We first recall some theoretical details on derivatives.
Theoretical notes on derivatives
Let be a real or complex function defined on an interval and . If the limit exists, the function is said to be differentiable at , provided is finite. The value of the derivative at , namely , is denoted by or or If is finite, the derivative is also sometimes called proper. If is infinite, it is improper. If is one of the boundary points of , we arrive at one-sided derivatives (i.e., left-sided derivative and right-sided derivative). If a function has a derivative at , the function is said to be differentiable at . A function which is differentiable at every point of a given interval is said to be differentiable on the interval.
Rules of differentiation:
, for every real or complex number ;
We can solve derivatives in SageMath in at least three different ways:
by using limits via the command ;
by using or functions;
by using SAGE function method .
We will explore the options that we can apply for any case via examples.
Exercise
Find the derivative of in SageMath by apply the difinition of , and next use the function to verify the result of the first method.
Solution:
Let us first determine the derivative in question by using the definition and hence limits. Recall that is defined as the limit and we say that exists when this limit exists. In Sage this goes as follows:
Great! We received our result, that is, . Let us now shorten the code with the built-in function , as suggested in the statement.
Note that the same can be done with the function, as follows:
Finally, let us also use a third variant that Sage offers for the computation of the derivative of a function, based on the method . This is essentially the same as above, but is written in the following form:
This second version of the syntax is more like Python, and makes it clear that the derivative is an attribute, or method associated with the object f(x). In particular, one could also type , as it is shown below:
Exercise (evaluation of derivatives at certain points)
Find the particular value of derivative of for Here we can also use two methods
Create a function and just input a value
We can specify the variable after , see below.
Solution:
Let us apply first the first method:
Let us also specify the details of another method, among those mentioned above, that one can apply to get the same answer:
Or we can type
In general, we prefer more the second method.
Example
Find the derivative of the function for all real numbers and next find . Moreover, present a numerical approxiamtion of .
For the numerical approximation we can use the function:
Similarly for another value:
With Sage we can also handle derivatives when other variables are involved. In this case, all variables other than the variable of differentiation (the variable after the comma in the derivative command) are treated as constants (unspecified numbers). However, one should still introduce these varianbles as symbolic variables in Sage.
Exercise
Find the derivative of the function , where are positive constants and . Next evaluate the derivative at
Solution:
To compute the value we can add the cell:
Application of first order derivatives (velocity of an object)
Recall that the velocity of a moving object is the derivative of its position function, and its acceleration is the derivative of its velocity function. Next we will use the units for velocity and for acceleration (though one could instead use for velocity and respectively for acceleration).
Exercise
If the position of a moving object in time t is given by the function determine the velocity and the acceleration of the object for general time and next for time .
Solution
Hence the object is moving with constant acceleratiob 8m/sec^2 (assuming that we compute the velocity in m/sec).
Higher order derivatives
The previous problem it could solved by a different approach, relying on second order derivatives (as the acceleration is the second derivatibe of the position function). Usually, given a function we denote the second derivative by . In general for higher order derivatives we use the notation We call the th derivative of at . See the 6th Chapter of the BG book for the formal definition of higher-order derivatives as limits.
Exercise
Find the 2nd order, 3rd order and 5th order derivatives of the function with . Next evaluate the second-order derivative at , at and at .
Solution:
One can use our previous functions and methods , or, and add a third argument (or second if the variable is not specified), which will show the order of the derivative that need to compute.
To evaluate a higher-order derivative at a certain point we can use the same approach as for the evaluation of the derivative of a function .
Or we can type:
In case we need a numerical approximation of we can add:
Similarly we can compute :
Exercise
Find the acceleration of an object at time general time and at time sec, when its position function is given by , with .
Solution:
Or we could directly type:
A few details on partial derivatives
Partial derivatives are derivatives that can be used when you have a function of multiple variables. Unlike ordinary derivatives, which describe how a function changes with respect to one variable, partial derivatives measure how a function changes with respect to one variable while keeping all other variables constant. For instance, consider the function . Then we have the following two first-order partial derivatives:
For more details see Chapter 8 in the BG book.
Exercise
Consider the function . Find the 1st and 3rd order partial derivatives with respect to .
Solution:
Exercise
Find the first order and second order partial derivatives of .
Geometrical meaning of the derivative
Derivatives allow us to get instantaneous rate of change or slope of a function at a given point. More specifically, the derivative at a point on a curve represents the slope of the tangent line to the curve at that point. Geometrically, the tangent line touches the curve at only one point and is the best linear approximation of the curve near that point. The derivative gives us information about the direction and steepness of the curve at any point, allowing us to analyze the behavior of the curve and make predictions about its future behavior. In essence, the derivative provides us with a powerful tool for understanding and analyzing the geometry of functions.
The geometric meaning of the derivative allows to approximate
A real function is called increasing at of its domain, if for all points x of some neighbourhood of a point if and if . A real function is increasing on an interval if for all . Similarly, a function is said to be decreasing at a point if and and only if there is a neighbourhood of the point such that for all , while for all from this neighbourhood. A function is decreasing on an interval A if for all . Thus a function having a non-zero finite derivative at a point is either increasing or decreasing at that point, according to the sign of the derivative.
Exercise
Find the derivative of , and plot the tangent lines for and .
Solution:
Exercise
Find the local extrema for . Find out whether there any maxima or minima.
Solution:
Exercise (tangent lines)
Find the tangent line to the function for . Next create an illustration including the graph of and the tangent line at question.
Solution:
Exercise
Find the second derivative of
Solution:
Let us use Sage's symbolic calculus capabilities to find the first and second derivatives of the given function:
Derivatives of polynomial functions
Polynomials are some of the easiest and most commonly used class of functions that we encounter in Mathematics. They can easily and efficiently be evaluated at every specific value and have very nice properties: they are continuous everywhere, differentiable everywhere, and integrable on any bounded interval. Finding the derivatives or antiderivatives of a polynomial are very easy tasks too.
Exercise
Find the derivative of the polynomial function given below at :
Solution:
We can use SAGE's symbolic calculus capabilities to find the derivative of the function and then evaluate it at x = 2.
Exercise
Find the derivative of
Solution:
We can use the quotient rule to find the derivative of g(x) and simplify the expression.
4.6. Elementary theorems on derivatives
We now proceed with some elementary properties of derivatives and in particular some basic theorems related to them.
Derivative of the inverse function
If is a real-valued function differentiable at , such that the inverse exists on a neighbourhood of the value and , then
Rolle’s theorem
Assume that the function is continuous on a closed bounded interval [a, b] and differentiable inside this interval. If , then there is a number such that
Lagrange’s mean value theorem
Assume the function is continuous on an interval [a, b] and differentiable at all points inside this interval. Then there is a number such that
Cauchy’s mean value theorem
Let functions and be continuous on an interval [a, b] and differentiable inside this interval. Further, let for all and . Then there is a point such that
The l'Hopital's rule
Finally let us recall the l'Hopital's rule, which is related to differentiation.
Assume that and are differentiable functions on an open interval , except possibly at a point . Suppose that , or , and for all with . Moreover, assume that the limit exists. Then the l'Hopital's rule states that
Exercise (critical points)
Find the critical points of . Next plot for .
Solution:
Exercise (critical points)
Find the critical points of the function . Next plot in one figure the given function and its (first) derivative and mark the c Use green color for the graph of the derivative.
Solution:
Note that as the 2 critical points of are solutions of the equation , we can attain them as follows:
This gives us an alternative to rewrite the program for marking these critical points as follows
Exercise (Rolle's theorem)
Verify Rolle's theorem for the function in the interval .
Solution
4.7. Series
Recall that a series is the sum of the terms of a sequence, usually denoted by
where is the th term of the sequence. If the sequence converges to a limit we write . If the sequence does not converge to a limit, then the series is said to diverge.
A geometric series is a series of the form , where and are constants. The sum of a geometric series is given by:
A telescoping series is a series in which most of the terms cancel out:
The sum of a series can be computed in SageMath by using the function. The syntax is , where f is the function to be summed, x is the variable of summation, a is the starting index of summation, and b is the ending index of summation.
Example
Consider a series that converges if and is equal to
Let us now compute a partial sum
Exercise
Use SageMath to compute the sum of the series .
Solution:
Exercise
Compute the sum of the series .
Solution:
Exercise
Find the value of using partial fraction decomposition. Verify in this way that the sum converges and compare the result to the value of the telescoping sum .
Solution:
On the other hand we see that:
Exercise
Compute the sum of the series .
Solution:
We proceed by analyzing the use of power series in Sage. Below is an example of computing the square root of 2 using power series.
Example
Compute the square root of 2 using power series.
Solution:
This example shows how to approximate ising power series.
An illustration of a telescoping series
Series of the form with are referred to as telescoping series. For example, a telescoping series is given below:
To illustrate this series we can proceed as follows:
4.8. Taylor polynomials (Taylor expansions)
We now want to approximate functions, by Taylor polynomials.
Taylor polynomials are mathematical approximations of a function using a series of polynomials. Specifically, they are a way of representing a function as an infinite sum of terms, where each term is a polynomial multiplied by a power of the independent variable.
The Taylor polynomial of a function centered at is given by:
where
denotes the derivative of evaluated at ;
is the remainder term of the -degree Taylor polynomial.
The first few terms of the Taylor polynomial are given by:
First-degree Taylor polynomial (also known as the tangent line approximation):
Second-degree Taylor polynomial:
Third-degree Taylor polynomial:
Fourth-degree Taylor polynomial:
and so on. As the degree of the polynomial increases, the approximation gets closer to the actual value of the function.
In SageMath we can find the Taylor polynomial of a given function in two ways:
apply the function;
apply the SAGE function method.
Let us present some examples.
Exercise
For the function find the Taylor polynomial of degree at most 5, around .
Solution:
Or we could directly type:
Remark
To get a better view of how the approximation works, one can plot both the given functions as follows (below we will described another procedure for doing this)
As you can see our Tailor polynomial of degree 5 approximates the function pretty well to some extent
Exercise
For the exponential function find the Taylor polynomial of degree at most 10, around 0.
Solution:
Exercise
For the function find the Taylor polynomial of degree 2 and 8, for x=3. Moreover, plot the approximations.
Solution:
We can check the accuracy of approximation near the point
As we can see, the higher the degree of approximation, the more precise are the values near the center . Let us now plot the approximations:
Exercise (optional) - Construction of a Taylor series function in Python
The following three exercises are optional, as we dont use built-in functions from Sage to study Taylor series.
In these examples we are totally based on Python so one may skip them.
Construct your own Taylor series Sage function for . Note that this function should satisfy the following:
It takes as an input the point x_0 around which we want to consider the Taylor expansion.
It takes as an input the function f for which the Taylor series is computed.
It takes as an input the order at which we want to stop the Taylor expansion.
The resulting Taylor series is a function of one free variable, i.e., it can be evaluated at some point x_value
Solution:
A possible solution goes as follows:
We can pick some concrete inputs to see that our Sage function "" for Taylor polynomial works as it should. For example, Taylor polynomial of degree 5 of sin(x) at 0 is
Notice that Taylor polynomial of degree 6 of sin(x) at 0 is the same as the degree 5 Taylor polynomial
This is becaue sin(x) is an odd function, and thus the Taylor series contains only summands with odd exponents. Indeed, observe the change if we shift to degree 7
Exercise (optional)
Compare the graphs of the Taylor polynomials of the sin function, centered at the same point .
Solution:
(Note that the Sage cell given below will work only if the previous cell, where the function "" is defined, has already been compiled).
Exercise (optional)
Repeat the above procedure and display a complete code for the computation and visualization of Taylor polynomials of the exponential function around the origin. In particular, chose to display the graphs of Taylor polynomials of orders . (Note that is neither odd, nor an even function, so there is no reason to expect some vanishing of terms in the Taylor expansion, as we saw above for the case of ).
Solution:
Let us now display the code for the computation and visualization of Taylor polynomials of order of around .
4.9. Integration
We now proceed with learning how to compute simple integrals via Sage. First we recall some theoretical background.
Theoretical remarks on integration
An integral is a mathematical concept that expresses the area under a curve. It is a way to calculate the length, area, or volume of an irregular object or shape. There are two types of integrals, definite and indefinite.
A definite integral is used to find the area under a curve between two specific points. It is written as:
where is the function being integrated, is the lower limit and is the upper limit of integration.
An indefinite integral, also called an antiderivative, is the set of all functions that would have as their derivative. It is written as:
Here are some common integrals:
, where is a coefficient, and is a constant of integration.
, where .
.
.
.
.
.
Now, in order to compute/find the definite integral we can use:
integral(f(x), x, a, b)function;f.integral(x,a,b) or f.integrate(x,a,b)methods.
To find the indefinite integral we can use the same:
integral(f(x), x)function;f.integral(x) or f.integrate(x)methods.
but we need to omit the and parameters, lets have a look
Remark
Note that Sage does not add the constant of integration (the constant posed above)
Example
Conside the function .
Use Sage to plot the graph of for and the area bounded by the graph of an the -axis (andf the vertical lines ).
Compute the integral and next provide a geometric interpretation.
Solution:
To mark the region bounded by the graph of , the -axis and the lines , we can instead type:
General geometric interpretation:
The integral , when it exists, represents the net area between the curve of the function , and the -axis, over the interval
For our case the integral represents the signed area between the curve of , the -axis and the vertical lines . Here, by the term signed area we mean that if the curve is above the -axis, then the area contributes positively to the integral. If the curve is below the -axis, then the area contributes negatively.
The total area is "net" in the sense that the areas above the -axis are positive, and those below the -axis are negative. Thus, if part of the curve lies above the -axis and part lies below, the positive and negative areas are subtracted from each other.
Example (Riemann sums)
For the theory on Riemann sums we refer to Chapter 6 in the BG book. Below we will use the function to illustrate them and also explain how we can use them to approximate an integral.
Observe for all the three case (left, right and middle Rieman sums) we can get better approximations by increasing .
We can use this method to estimate the integral (or area) as follows:
By increasing we can get a much better approximation: (similarly are treated the right or middle Riemann sums).
Example (computation of different types of integrals via SageMath)
Another example:
If we want to have a Latex output style, we can add:
Exercise
Find the antiderivative of the function
Solution
Exercise
Evaluate the definite integral of the function from to
Solution:
Exercise
Find the integral of the function
Solution:
Exercise
Calculate the definite integral of the function from to
Solution:
Exercise
Check the integration by parts formula for and .
Solution:
It is important to understand that Sage can also handle definite integrals involving variables, as in the case of derivatives. Let us treat such an example.
Exercise
Compute the integral
Solution:
This is an application of symbolic variables as first we should introduce the variables .
**Fundamental Theorem of Integral Calculus - applications **
Let us now discuss a few tasks related to the fundamental theorem of calculus, see Chapter 6 on the BG book for details.
Here we only recall that given some continuous function , defined on a finite interval , we can introduce the function
Its derivative at is given by .
Exercise
Introduce in SageMath the fucntion
Next derive the derivative of and compute .
Solution:
Be aware that in SageMathCell in order to obtain the result posed above we shoud include in our program the command .
In fact below we describe a bit quicker way to introduce .
Or finally one could type
However, in this case we cannot type as we will get an error (try it!).
Using this approach, to evaluate we should type the following:
Exercise for practice
Use Sage to evaluate the derivatives and , where
Next confirm Sage's result by hand.
Let with , and . Find the positive real where starts decreasing.
Remark
Some functions do not have elementary antiderivatives. For example, consider the integral
Let us ask Sage for the error function
Type: LazyImport
String form: erf
File: /ext/sage/9.7/src/sage/misc/lazy_import.pyx
Docstring:
The error function.
The error function is defined for real values as
\operatorname{erf}(x) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-t^2}
dt.
This function is also defined for complex values, via analytic
continuation.
EXAMPLES:
We can evaluate numerically:
sage: erf(2)
erf(2)
sage: erf(2).n()
0.995322265018953
sage: erf(2).n(100)
0.99532226501895273416206925637
sage: erf(ComplexField(100)(2+3j))
-20.829461427614568389103088452 + 8.6873182714701631444280787545*I
Basic symbolic properties are handled by Sage and Maxima:
sage: x = var("x")
sage: diff(erf(x),x)
2*e^(-x^2)/sqrt(pi)
sage: integrate(erf(x),x)
x*erf(x) + e^(-x^2)/sqrt(pi)
ALGORITHM:
Sage implements numerical evaluation of the error function via the
"erf()" function from mpmath. Symbolics are handled by Sage and
Maxima.
REFERENCES:
* https://en.wikipedia.org/wiki/Error_function
* http://mpmath.googlecode.com/svn/trunk/doc/build/functions/expin
tegrals.html#error-functions
Class docstring:
EXAMPLES:
sage: from sage.misc.lazy_import import LazyImport
sage: my_integer = LazyImport('sage.rings.all', 'Integer')
sage: my_integer(4)
4
sage: my_integer('101', base=2)
5
sage: my_integer(3/2)
Traceback (most recent call last):
...
TypeError: no conversion of this rational to integer
Init docstring:
EXAMPLES:
sage: from sage.misc.lazy_import import LazyImport
sage: lazy_ZZ = LazyImport('sage.rings.all', 'ZZ')
sage: type(lazy_ZZ)
<class 'sage.misc.lazy_import.LazyImport'>
sage: lazy_ZZ._get_object() is ZZ
True
sage: type(lazy_ZZ)
<class 'sage.misc.lazy_import.LazyImport'>
Call docstring:
Calling self calls the wrapped object.
EXAMPLES:
sage: from sage.misc.lazy_import import LazyImport
sage: my_isprime = LazyImport('sage.all', 'is_prime')
sage: is_prime(12) == my_isprime(12)
True
sage: is_prime(13) == my_isprime(13)
True
Hence, according to Sage the error function is defined as
Let us evaluate this fucntion at some points:
Finally, we could directly aapproximate the initial intregral based on the function.
In the final chapter we will see that Sage provides another built-in method to approximate integrals numerically.
Another example where we get an expression in terms of the error function is for example the following integral:
An example of numerical integration in SageMath
Compute the integral
Let us introduce and have a look at the so called Gamma function.
Hence our Gamma function at least at gives the same result with the built-in function in Sage that represents the function.
Let us try to plot our function for certain :
We can plot in a better way the Gamma function by importing in it!
Type to learn more information for the Gamma function in SageMath and see also Chapter 6 on the BG book.
Let us now return to our initial problem. One way to obtain a meaninfull result is to apply the following method:
Let us now use Sage via the built-in function . This approximates the value of an integral on an interval.
This provides a good approximation, as the error (the second component in Sage's output above is very small). However, observe that:
But:
So in this case the method provides a very big error and the estimation is wrong, and not acceptable. We will discuss mathematical techniques of numerical integration and how we can implement them using Sage in our final chapter, the final week of the course.
Improper integrals
Compute the integral
Let us compare this result with the previous result:
Thus, the method is not very accurate in this case, and has a very high error, close to 1.7.