Chapter 11. Calculus: Continuous and Discrete

Time may change me But I can’t trace time I said that time may change me But I can’t trace time

This chapter primarily focuses on the types of problems students and teachers will cover in college-level mathematics courses and how Mathematica can be used as a calculator (tool for getting an answer) and a teacher (tool for gaining insight into a mathematical problem). However, this focus was largely pragmatic and does not imply that Mathematica is limited to introductory calculus. Quite the contrary. Mathematica has been leading the charge among computer algebra systems since its inception, and with each new release the depth and breadth of its abilities in symbolic calculus improve. My goal in most of these recipes is to provide a starting point for the inexperienced user. Experts will probably find little that is new or highly original. This was a conscious choice based on space limitations. I am quite certain one could write a small cookbook by turning each recipe here into an entire chapter! Such is the depth of Mathematica’s abilities.

Most of the recipes in this chapter address what is commonly known as infinitesimal or continuous calculus. These problems deal with limits (11.1 Computing Limits), series (11.3 Using Power Series Representations), derivatives (11.4 Differentiating Functions), integrals (11.5 Integration), and differential equations (11.6 Solving Differential Equations). A common application of calculus is finding minimums and maximums. Mathematica packages these techniques into Minimize, Maximize, and related functions (11.7 Solving Minima and Maxima Problems). When you use your calculus skills to solve real engineering and physics problems, you are bound to run smack into applications that involve vector calculus. Mathematica has a package of functions specifically dedicated to vector calculus, and we touch on some of this functionality in 11.8 Solving Vector Calculus Problems.

Although the calculus of continuous functions still plays a dominant role, discrete calculus is extremely important and has been garnering increasing attention lately due to research in such varied domains as string theory, probability theory, theory of algorithms, and combinatorics, to name a few. Mathematica 7 has enhanced its discrete calculus abilities. 11.9 Solving Problems Involving Sums and Products through 11.11 Generating Functions and Sequence Recognition help you start using these capabilities.

Mathematica also recognizes prime notation, but this notation is more commonly used in Mathematica when entering a differential equation. See the sidebar Mathematica’s Representation of Differentiation for some important subtleties.

In[26]:= {Sin'[x], Sin''[x]}
Out[26]= {Cos[x], -Sin[x]}

You can use D along with Solve to differentiate implicit functions. Simply use D as usual and use Solve to find the solution in terms of y'[x].

Discussion

There are cases where you may want to use the D to synthesize a function on the fly. In this case, use Set (=) to perform the differentiation operation immediately or use Evaluate with SetDelayed (:=).

In[37]:= f1[x_] = D[Sin[Pi x Cos[x ^ 2]], x];

In[38]:= f2[x_] := Evaluate[D[Sin[Pi x Cos[x ^ 2]], x]]

In[39]:= {f1[2.], f2[2.]}

Out[39]= {-9.65614, -9.65614}

If you forget to do so, you will get an error when you call the function with a literal value.

Discussion

Many students will use Mathematica to check the answers to their calculus homework, but Mathematica is also useful for generating demonstrations of the fundamental concepts underlying differentiation. For example, the derivative of a function at a point is the slope of the tangent to the function at that point. Further, given two points, the slope of the secant drawn between these points approaches the derivative as the points approach each other along the curve. The following function uses Mathematica’s dynamic features to generate presentations of this fact using any function and starting points as input.

Mathematica’s Representation of Differentiation
Mathematica’s Representation of Differentiation

Integrate will easily handle most integration problems you are likely to encounter in school, engineering, and science.

Discussion

Double and higher-order integrals are computed with a single Integrate function by adding multiple integration variables. However, if you use the traditional integration notation, you will use multiple integral symbols.

Discussion

Some integrations may return with conditionals and assumptions due to convergence issues. You can eliminate these by providing your own assumptions.

Discussion

You also do this using GenerateConditionsFalse.

Discussion

You can also get piecewise functions as a result of Integrate.

Discussion

When Integrate is unable to solve the integration, it will return the unevaluated integral in symbolic form.

Discussion

Applications of integration are numerous, and it would be impossible to provide even a small representative set of examples here. Rather, I will provide examples that emphasize how Integrate can be combined with other Mathematica functions in non-obvious ways.

A simple application is a function to compute the area between two arbitrary curves given two points. When you create functions that embed Integrate, you often want to allow options to pass through to increase generality.

Discussion

This would generate a huge messy conditional if not for the ability to pass assumptions about the arbitrary bounds a and b.

Discussion

Create a table of volumes of hyperspheres. Here Boole maps True to 1 and False to 0. Note that the list of integration limits must be converted to a sequence using Apply (@@). By the way, this is a very expensive way to calculate volume of a hypersphere, but it does illustrates how to parameterize the order of integration. Search for hyperspheres on Wikipedia or Wolfram’s MathWorld to find a more practical formula.

Discussion

You can combine Integrate with differentiation to create a general function to compute the length of a curve between two points.

In[61]:= Clear[lengthOfCurve]

In[62]:= lengthOfCurve[expr_, var_, a_, b_, opts : OptionsPattern[]] :=
          Integrate[Sqrt[1 + D[expr, var] ^ 2], {var, a, b},
           Sequence @@ FilterRules[{opts}, Options[Integrate]]]

Or, you can compute the length of the hypotenuse of a right triangle.

Discussion

Verify the formula for the circumference of a circle given its radius by taking twice the arc length of a semicircle.

Discussion

Here is a purely symbolic solution with assumptions to simplify results.

Discussion

The solutions provided by DSolve are not automatically simplified, and you often will want to use Simplify or FullSimplify to postprocess them into a more mathematically friendly form. This is especially relevant when comparing the answer DSolve finds with answers provided in a typical textbook. Consider this problem adapted from Advanced Engineering Mathematics by Erwin Kreyszig (John Wiley). Here you want to find the solution to a differential equation describing the speed of a fluid flowing out of an opening in a container.

Discussion

Given the physics of the problem, it should be clear we want the first solution (the second solution has the height increasing with time).

Discussion

Although this has simplified the result somewhat, it is a much more complicated solution than the one provided by Kreyszig, which is

Discussion

Did DSolve give the wrong result? A common mistake when using Mathematica is to prematurely substitute specific constants as I did above. It is often advisable to solve equations entirely in symbolic form and substitute constants later.

Discussion

Although this did not get us all the way to the form of the book’s solution, you are more likely to see the final transformation that will demonstrate that DSolve was correct. It hinges on noticing that 1/4 is the same as (-1/2)*(-1/2).

Discussion

Substituting h0 and kl with the constants shows that Mathematica did get the correct solution. Alternatively, you can ask Mathematica to prove its solution is equal to the book’s solution by using Resolve and ForAll. The only problem here is that Mathematica does not show its work!

Discussion

For many applications of minimization or maximization, you are interested in the extreme value within a specific interval.

Discussion

I restrict this discussion to Maximize for simplicity, but everything here applies to Minimize as well. If you are interested in displaying the result of Maximize, you will want to force the result to numerical form, as we did in the solution. Maximize will keep the result in exact form if it is given input in exact form. For polynomials, this typically means the result will be expressed in terms of radicals or Root objects. A Root[f,k] object represents the kth solution to a polynomial equation f[x] == 0.

Discussion

Sometimes you want to find solutions for integer values only. You can constrain Maximize to the integers in one of two ways. You might recognize this problem as an instance of a knapsack problem where you are optimizing the value of the knapsack (item1 has value 8, item2 11, and so on) subject to size constraint of 14 where item1 has size 5 and so on.

Discussion

A more convenient notation when all variables are integer is to specify the domain as the third argument to Maximize.

Discussion

Maximize seeks a global maximum, whereas an alternative function, FindMaximum, seeks a local maximum (there is also FindMinimum for local minimums). FindMaximum allows you to specify a starting point for the search, but otherwise has a very similar form to Maximize. The following program demonstrates the difference between Maximize and FindMaximum. The advantage of FindMaximum is that it does not require the objective function to be differentiable.

Discussion

Simple vector calculus problems can be solved in terms of the calculus primitives discussed in this chapter’s recipes along with vector functions like Dot and Cross. For example, line integrals are commonly used to calculate work performed when moving a particle along a path in a vector field. Here F is the vector equation of the field, f is the equation of the path through the field, var is the parameter of f, and a and b are the start and end points of the path.

Solution

Another common operation in vector calculus is the surface integral over scalar functions and vector fields. Surface integrals are the 2D analog of line integrals. One way to think of the scalar surface integral is to imagine a surface f made of a material whose density varies as described by a second function g. The surface integral of f over g is then the mass per unit thickness.

In[93]:= surfacelntegralScalar[g_, f_, {v1_, v1a_, v1b_}, {v2_, v2a_, v2b_}] :=
          Integrate[g[f[v1, v2]] Norm[Cross[D[f[v1, v2], v1], D[f[v1, v2], v2]]],
           {v1, v1a, v1b}, {v2, v2a, v2b}]

For example, consider the surface fl, which is a half sphere over the interval { ϕ , 0, Pi/2} and { θ , 0, 2 Pi}, and compute the surface integral given a density function given by (x^2 + y^2) z.

Solution

If we use a constant function (uniform density), we get the surface area of the half sphere as expected (surface area of an entire sphere is 4 πr2).

In[97]:= g2[{x_, y_, z_}] := 1
         surfaceIntegralScalar[g2, f1, {ϕ, 0, Pi/2}, {θ, 0, 2 Pi}]
Out[98]= 2 π

For a vector field, there is a similar equation using Dot in place of scalar multiplication by the norm. The traditional way to visualize the vector surface interval is to consider a fluid flowing through a surface where there is a vector function F describing the velocity of the fluid at various points on the surface. The surface integral is then the flux, or the quantity of fluid flowing through the surface in unit time.

In[99]:= surfaceIntegralVector[F_, f_, {v1_, v1a_, v1b_}, {v2_, v2a_, v2b_}] :=
          Integrate[Dot[F[f[v1, v2]], Cross[D[f[v1, v2], v1], D[f[v1, v2], v2]]],
           {v1, v1a, v1b}, {v2, v2a, v2b}]

Here is the solution to the flux described by {3 y, -z, x^2} through a surface described parametrically as {s t, s + t, (s^2 - t^2)/2}.

In[100]:= f[s_, t_] := {s t, s + t, (s^2 - t^2) /2}
          F[{x_, y_, z_}] := {3 y, -z, x^2}
          surfaceIntegralVector[F, f, {s, 0, 1}, {t, 0, 3}]
Out[102]= -15

A standard result from electrostatics is that the net flux out of a unit sphere, for a field that is everywhere normal, is zero. We can verify this as follows:

In[103]:= F2[{x_, y_, z_}] := {1, 1, 1}/(x^2 + y^2 + z^2)

In[104]:= f2[θ_, ϕ_] := {Sin[ϕ] Cos[θ], Sin[ϕ] Sin[θ], Cos[ϕ]}

In[105]:= surfaceIntegralVector[F2, f2, {θ, 0, 2 Pi}, {ϕ, 0, Pi}]

Out[105]= 0

The solution shows how the calculus primitives and other Mathematica functions can be used to build up higher-order vector calculus solutions. However, if you are interested in solving problems in vector calculus, the package VectorAnalysis` is definitely worth a look. Be forewarned that you might be in for a bit of a learning curve with this particular package, but it offers a lot of functionality. An important feature of the package is that it simplifies working in different coordinate systems. Before you can make effective use of VectorAnalysis`, you need to understand how coordinate systems are used and which coordinate system is appropriate to your problem.

In[106]:= Needs["VectorAnalysis`"]

In[107]:= CoordinateSystem
Out[107]= Cartesian

In[108]:= SetCoordinates[Spherical]
Out[108]= Spherical[Rr, Ttheta, Pphi]

In[109]:= CoordinateSystem
Out[109]= Spherical

When you use VectorAnalysis`, you will typically want to use functions in that package in place of some standard Mathematica functions such as Dot and Cross. This is because the alternatives DotProduct and CrossProduct respect the current coordinate system. For example, if the current coordinate system is Spherical, you expect the following DotProduct to be zero because the vectors are orthogonal in spherical coordinates.

In[110]:= DotProduct[{1, Pi/2, 0}, {1, Pi/2, Pi/2}]
Out[110]= 0

In contrast, Dot and Cross always assume Cartesian coordinates.

Discussion

Some of the most important vector calculus operations are Div (divergence), Grad (gradient), Curl, and the Laplacian. Although it would make a nice exercise to implement these from the calculus primitives, as I did for line and surface integrals, there is no need if you use the VectorAnalysis` package. These operations use the default coordinate system, or you can specify a specific coordinate system as a separate argument.

The divergence represents the instantaneous outflow of a vector field at each point.

Discussion

The curl of a vector field represents the amount of rotation.

Discussion

By definition, the divergence of the curl must be zero since the curl has no net outflow.

In[114]:= SetCoordinates[Cartesian[x, y, z]];
          Div[Curl[{1, 1, 1} / (x^2 + y^2 + z^2)]]
Out[115]= 0

The gradient of a function f is a vector-valued function that indicates the direction in which f is increasing most rapidly. If you were climbing a hill, you would move in the direction of the gradient at each point to reach the top (strictly speaking the gradient would only be guaranteed to be directing you to a local peak). You can visualize the meaning of the gradient by using VectorPlot. I restrict the result to 2D for easier visualization.

Discussion

If sums or products don’t converge, Mathematica will let you know by emitting an error. You can test for convergence without evaluating the sum using SumConvergence.

Discussion
Discussion

As with Integrate, Sum can specify multiple summation variables. In traditional form these sums are rendered as a multiple summation, but keep in mind that these are entered as Sum[expr,{n,nmin,nmax},{m,mmin,mmaz}] rather than Sum[Sum[expr, {n,nmin,nmax}],{m,mmin,mmaz}].

This double summation has a surprisingly simply solution.

Discussion

This is a very famous sum attributed to Srinivasa Ramanujan, one of India’s greatest mathematical geniuses. You might think that Mathematica is just doing some simple pattern matching to recognize this result; however, substitute for any of the magic constants in this formula, and Mathematica will handle it just as well (but don’t expect the answer to be as pretty).

Discussion

Here is a very pretty formula for π that combines an infinite sum and an infinite product.

Discussion

As of version 7, Mathematica can handle indefinite sums and products. Mathematica will seek to eliminate the sum if possible. For example, the sum over k of a polynomial is another polynomial that can be expressed in terms of k, and products over polynomials will invariably reduce to some expression involving Gamma.

Discussion

The Z-transform is an important infinite sum used in signal processing. It is defined as Sum[f[n] z^-n,{n, 0, Infinity}], but is directly supported using ZTransform.

Discussion

Here is an unconventional application for Sum, but one that is sometimes used in discrete math to introduce the idea of a generating function. You can use Sum to construct a generating function for solutions to problems like n1+n2+n3 == 12 subject to nl >= 4, n2 >= 2, and 5 >= n3 >= 2. Each Sum is constructed from the smallest number the associated variable can take to the largest, by considering the smallest the other variables can take. For example, xl must be at least 4 but can’t be greater than 12-2-2 = 8, since n2 and n3 must each be at least 2. Here we use Expand to generate the polynomial and Cases to find the exponents that sum to 12, thus giving all solutions.

Discussion

If you only care about the number of solutions, it would fall out of the coefficient of x12 in the expansion of this polynomial.

Discussion

See 11.11 Generating Functions and Sequence Recognition for more information on generating functions in Mathematica.

Readers who are interested in gaining insight into the algorithms that underlie Mathematica’s amazing feats with infinite sums should read A=B by Marko Petkovsek, Herbert S. Wilf, and Doron Zeilberger (A K Peters), which is available online at http://bit.ly/1LJiwe .