Derivatives and integrals in Mathematica

Mathematica uses the command D[...] to take derivatives of variables and functions. As an example, type

   D[x^3, x]
   3 x^2
The general format of the D[...] command is
   D[ <function>, <variable> ]
which tells Mathematica to take the derivative of <function> with respect to <variable>. Mathematica treats all derivatives as partial derivatives, so we have
   D[x y^2, x]
   y^2
   D[x y^2, y]
   2 x y

To take the second derivative, we can just use the D[...] command twice in a row:

   D[ D[x^3, x], x ]
   6 x

The command to compute integrals is Integrate. It works much like D[...] in that we specify an integrand and a variable of integration:

   Integrate[x^2, x]
   x^3 / 3
   Integrate[x y, y]
   x y^2 / 2
Note that Mathematica omits the constant of integration that is so common in calculus textbooks.

We can also use Integrate to compute definite integrals by specifying a lower and upper limit of integration:

   Integrate[x^2, {x, 1, 2}]
   7 / 3
computes the integral of x^2 from x = 1 to x = 2. The special keyword Infinity can be used to specify a lower or upper limit of integration:
   Integrate[(1/x)^2, {x, 1/2, Infinity}]
   2

Some definite integrals are too hard for Mathematica to compute analytically. In this case, we can estimate the integral numerically using NIntegrate. (Note the double capital letter at the beginning of this command.) As an example, try

   Integrate[Sin[x^4], {x, 0, 1}]
   Integrate[Sin[x^4], {x, 0, 1}]
You see that when an integral is too hard for Mathematica, it simply "spits it out". But we can compute the integral numerically:
   NIntegrate[Sin[x^4], {x, 0, 1}]
   0.18757

Another way to integrate numerically is to nest the Integrate command within the N[...] command:

   N[ Integrate[Sin[x^4], {x, 0, 1}] ]
   0.18757
This has the advantage that we can ask for more significant figures:
   N[ Integrate[Sin[x^4], {x, 0, 1}], 20]
   0.187569544685
Note that we don't get 20 significant figures because numerical integration is inherently approximate. In this case we only got 18 significant figures. When integrating numerically, you usually need to ask for a few more significant figures than you really want.


Other parts of the Mathematica tutorial: