\( \DeclareMathOperator{\abs}{abs} \newcommand{\ensuremath}[1]{\mbox{$#1$}} \)

Integration

 1 Basic Integration

 1.1 Indefinite Integrals

The command to preform both an indefinite and definite integral are the same in Maxima.
To preform an indefinite integral we use the integrate(f(x),x) command.  Note that the
first input is the function that we wish to integrate, while the second is the variable of
integration.

--> integrate(x^2+2*x,x);
\[\tag{%o1} \frac{{{x}^{3}}}{3}+{{x}^{2}}\]

In the following example, we define a function f(t), bind I to its antiderivative family,
and then have Maxima show us the results of the indefinite integral:

--> f(t):=sqrt(1+exp(t));
I:'integrate(f(t),t);
I=ev(I,nouns);
\[\tag{%o1} \operatorname{f}(t):=\sqrt{1+\operatorname{exp}(t)}\] \[\tag{I}\int {\left. \sqrt{{{\% e}^{t}}+1}dt\right.}\] \[\tag{%o3} \int {\left. \sqrt{{{\% e}^{t}}+1}dt\right.}=-\log{\left( \sqrt{{{\% e}^{t}}+1}+1\right) }+\log{\left( \sqrt{{{\% e}^{t}}+1}-1\right) }+2 \sqrt{{{\% e}^{t}}+1}\]
--> kill(f);
\[\tag{%o5} \mathit{done}\]

 1.2 "Catching" the Antiderivative Family

Let's say that we want to integrate 2x+cos(x) and assign its antiderivatives to a new function f(x).
If we try:

--> f(x):=integrate(2*x+cos(x),x);
\[\tag{%o4} \operatorname{f}(x):=\int {\left. 2 x+\cos{(x)}dx\right.}\]

Then there are two problems:

1. We don't have the integration constant

2. Watch what happens when we try to evaluate f(3):

--> f(3);
\[\mbox{integrate: variable must not be a number; found: 3}\mbox{\ensuremath{\neq}0: f(x=3)}\mbox{ -- an error. To debug this try: debugmode(true);}\]

The issue here is the same that we faced with the derivative.  We have to catch the antiderivative.
We will also add in an integration constant while we are at it:

--> kill(f);
\[\tag{%o8} \mathit{done}\]
--> define(f(x),integrate(2*x+cos(x),x)+C);
\[\tag{%o6} \operatorname{f}(x):=C+\sin{(x)}+{{x}^{2}}\]
--> f(x);
f(%pi/2);
\[\tag{%o7} C+\sin{(x)}+{{x}^{2}}\] \[\tag{%o8} C+\frac{{{\ensuremath{\pi} }^{2}}}{4}+1\]
--> kill(all);
\[\tag{%o0} \mathit{done}\]

Much better!  Now we can move on to finding specific antiderivatives using Maxima's solving capabilities.

 1.3 Finding Antiderivatives that Satisfy a Specific Condition

Let's say that we want to find the antiderivative f(x) of 2x + cos(x) that satisfies f(pi/2) = 3.
Here's how we would do that:

--> define(f(x), integrate(2*x+cos(x),x)+C);
\[\tag{%o1} \operatorname{f}(x):=C+\sin{(x)}+{{x}^{2}}\]

So the above f(x) has the integration constant included.  Now we want to apply the condition f(pi/2)=3:

First, we set-up the equation for the initial condition:

--> E:f(%pi/2)=3;
\[\tag{E}C+\frac{{{\ensuremath{\pi} }^{2}}}{4}+1=3\]

Then we solve it and catch the solution:

--> S:solve(E,C);
\[\tag{S}[C=-\frac{{{\ensuremath{\pi} }^{2}}-8}{4}]\]

Finally, we bind the solution to the constant C:

--> C:rhs(S[1]);
\[\tag{C}-\frac{{{\ensuremath{\pi} }^{2}}-8}{4}\]

So that now f(x) is given by:

--> f(x);
\[\tag{%o5} \sin{(x)}+{{x}^{2}}-\frac{{{\ensuremath{\pi} }^{2}}-8}{4}\]
--> kill(f,C);
\[\tag{%o9} \mathit{done}\]

It is important that you kill C if you plan on using it for your integration constant again, otherwise
it will keep its constant value that you assigned above!

 1.4 Streamlining the Process

Note that we do not have to to all of these steps separately.  Infact, let's streamline
this process in the following problem:  Let's find the antiderivative F(x) of f(x)=x^3 - 2x+7
that satisfies F(0)=3:

--> f(x):=x^3-2*x+7; define(F(x),C+integrate(f(x),x));
\[\tag{%o10} \operatorname{f}(x):={{x}^{3}}-2 x+7\] \[\tag{%o11} \operatorname{F}(x):=C+\frac{{{x}^{4}}}{4}-{{x}^{2}}+7 x\]
--> C:rhs(solve(F(0)=3,C)[1]); F(x);
\[\tag{C}3\] \[\tag{%o13} \frac{{{x}^{4}}}{4}-{{x}^{2}}+7 x+3\]
--> kill(C,f,F);
\[\tag{%o14} \mathit{done}\]

Let's pull apart the statement:

C:rhs(solve(F(0)=3,C)[1]);

Recall that solve(EQN,var) solves the equation EQN for the variable var and reports the result as a
list of equations like so:

--> solve(x^2 = x, x);
\[\tag{%o8} [x=0,x=1]\]

So, we type: solve(F(0)=3,C) to solve for the value of C that we want -- there is only one
solution in the list, so we take: solve(f(%pi/2)=3,C)[1], and we complete the process by re-binding
the value of C to the right-hand side of the solution equation.  Let's do this a few more times.

 1.5 Examples of Particular Solutions to Antiderivatives

Find the function f(x) with:

f'(x) = cos^2(x) ; f(%pi/3) = 2/3

--> define(f(x),integrate(cos(x)^2,x)+C);
\[\tag{%o15} \operatorname{f}(x):=C+\frac{\frac{\sin{\left( 2 x\right) }}{2}+x}{2}\]
--> C:rhs(solve(f(%pi/3)=2/3,C)[1]);
\[\tag{C}-\frac{4 \ensuremath{\pi} +{{3}^{\frac{3}{2}}}-16}{24}\]
--> f(x);
\[\tag{%o17} \frac{\frac{\sin{\left( 2 x\right) }}{2}+x}{2}-\frac{4 \ensuremath{\pi} +{{3}^{\frac{3}{2}}}-16}{24}\]

This is a little goofy-looking.  Let's try to simplify it:

--> ratsimp(%);
\[\tag{%o18} \frac{6 \sin{\left( 2 x\right) }+12 x-4 \ensuremath{\pi} -{{3}^{\frac{3}{2}}}+16}{24}\]

Not Bad!

Now let's try to find the function f(x) with f'(x)=sin(sqrt(x)) with f(1) = 0.
We begin by killing f and C because we want to use them again:

--> kill(f,C);
\[\tag{%o19} \mathit{done}\]
--> define(f(x),integrate(sin(sqrt(x)),x)+C);
\[\tag{%o20} \operatorname{f}(x):=C+2 \left( \sin{\left( \sqrt{x}\right) }-\cos{\left( \sqrt{x}\right) } \sqrt{x}\right) \]
--> C:rhs(solve(f(1)=0,C)[1]);
\[\tag{C}2 \cos{(1)}-2 \sin{(1)}\]
--> f(x);
\[\tag{%o22} 2 \left( \sin{\left( \sqrt{x}\right) }-\cos{\left( \sqrt{x}\right) } \sqrt{x}\right) -2 \sin{(1)}+2 \cos{(1)}\]
--> kill(f,C);
\[\tag{%o23} \mathit{done}\]

Nice!  This problem would have been pretty tough without the help of Maxima!
Let's find f(x) with: f'(x) = sqrt(1+%e^x) and f(0) = sqrt(2)

--> define(f(x), integrate(sqrt(1+exp(x)),x)+C);
\[\tag{%o24} \operatorname{f}(x):=C-\log{\left( \sqrt{{{\% e}^{x}}+1}+1\right) }+\log{\left( \sqrt{{{\% e}^{x}}+1}-1\right) }+2 \sqrt{{{\% e}^{x}}+1}\]
--> C:rhs(solve(f(0)=sqrt(2),C)[1]);
\[\tag{C}\log{\left( \sqrt{2}+1\right) }-\log{\left( \sqrt{2}-1\right) }-\sqrt{2}\]
--> f(x);
\[\tag{%o26} -\log{\left( \sqrt{{{\% e}^{x}}+1}+1\right) }+\log{\left( \sqrt{{{\% e}^{x}}+1}-1\right) }+2 \sqrt{{{\% e}^{x}}+1}+\log{\left( \sqrt{2}+1\right) }-\log{\left( \sqrt{2}-1\right) }-\sqrt{2}\]

The beauty of doing things this way is that now we can now go on to use f(x)
to find values, graph, etc:

--> f(0);f(log(2));
\[\tag{%o27} \sqrt{2}\] \[\tag{%o28} -\log{\left( \sqrt{3}+1\right) }+\log{\left( \sqrt{3}-1\right) }+\log{\left( \sqrt{2}+1\right) }-\log{\left( \sqrt{2}-1\right) }+2 \sqrt{3}-\sqrt{2}\]
--> wxplot2d(f(x),[x,-1,5]);
\[\tag{%t29} \]  (Graphics)
\[\tag{%o29} \]

 2 More than One Antiderivative

Let's say that I want the second antiderivative of x^2, call it f(x) that satisfies f(0)=1 and f'(0)=2.
Let's see what we can do here:

--> define(f(x),c[0]+integrate(c[1]+integrate(x^2,x),x));
\[\tag{%o1} \operatorname{f}(x):=\frac{{{x}^{4}}}{12}+{c_1} x+{c_0}\]

Two things to note in the above:

1. To get two antiderivatives, we simply antiderived twice in succession, and

2. We used subscripts c[0], and c[1] to identify the integration constants.

Now let's solve for c[0] and c[1]:

--> C:solve([f(0)=1,subst(x=0,diff(f(x),x)=2)])[1];
\[\tag{C}[{c_1}=2,{c_0}=1]\]

Note that solve() always returns a list, and so to avoid a list-in-a-list situation, we simply ask for the list of solutions
with the index 1 above.  Now we bind the solutions that we found to the constants:

--> c[1]:rhs(C[1]); c[0]:rhs(C[2]);
\[\tag{%o3} 2\] \[\tag{%o4} 1\]
--> f(x);
\[\tag{%o5} \frac{{{x}^{4}}}{12}+2 x+1\]

Let's clean up!

--> kill(all);
\[\tag{%o0} \mathit{done}\]

Let's repeat the exercise from above, but let's be a little more organized about it.
This time we will catch each antiderivative as it appears in its own function.  Let's find the third antiderivative F(x) of
x*cos(x) that satisfies F(0)=1, F'(%pi)=2, and F''(%pi/2)=1.

--> f[0](x):=x*cos(x);
define(f[1](x),c[0]+integrate(f[0](x),x));
define(f[2](x),c[1]+integrate(f[1](x),x));
define(f[3](x),c[2]+integrate(f[2](x),x));
\[\tag{%o1} {f_0}(x):=x \cos{(x)}\] \[\tag{%o2} {f_1}(x):=x \sin{(x)}+\cos{(x)}+{c_0}\] \[\tag{%o3} {f_2}(x):=2 \sin{(x)}-x \cos{(x)}+{c_0} x+{c_1}\] \[\tag{%o4} {f_3}(x):=-x \sin{(x)}-3 \cos{(x)}+\frac{{c_0}\, {{x}^{2}}}{2}+{c_1} x+{c_2}\]
--> C:solve([f[3](0)=1,f[2](%pi)=2,f[1](%pi/2)=1])[1];
\[\tag{C}[{c_1}=\frac{{{\ensuremath{\pi} }^{2}}-4 \ensuremath{\pi} +4}{2},{c_0}=-\frac{\ensuremath{\pi} -2}{2},{c_2}=4]\]
--> c[1]:rhs(C[1]);c[0]:rhs(C[2]);c[2]:rhs(C[3]);
\[\tag{%o7} \frac{{{\ensuremath{\pi} }^{2}}-4 \ensuremath{\pi} +4}{2}\] \[\tag{%o8} -\frac{\ensuremath{\pi} -2}{2}\] \[\tag{%o9} 4\]
--> F(x):=f[3](x);
\[\tag{%o10} \operatorname{F}(x):={f_3}(x)\]
--> F(x);
\[\tag{%o11} -x \sin{(x)}-3 \cos{(x)}-\frac{\left( \ensuremath{\pi} -2\right) \, {{x}^{2}}}{4}+\frac{\left( {{\ensuremath{\pi} }^{2}}-4 \ensuremath{\pi} +4\right) x}{2}+4\]

The beauty of this is that now all three derivatives of F(x) are available in their entirety:

--> f[2](x);f[1](x);f[0](x);
\[\tag{%o12} 2 \sin{(x)}-x \cos{(x)}-\frac{\left( \ensuremath{\pi} -2\right) x}{2}+\frac{{{\ensuremath{\pi} }^{2}}-4 \ensuremath{\pi} +4}{2}\] \[\tag{%o13} x \sin{(x)}+\cos{(x)}-\frac{\ensuremath{\pi} -2}{2}\] \[\tag{%o14} x \cos{(x)}\]
--> kill(all);
\[\tag{%o0} \mathit{done}\]

 3 Definite Integrals

The syntax for a definite integral is:

integrate(function,variable of integration, lower bound, upper bound)

To see this check out:

--> 'integrate(x^2,x,0,1);
\[\tag{%o30} \int_{0}^{1}{\left. {{x}^{2}}dx\right.}\]

And as an example:

--> I:'integrate(x^2,x,0,1);
I=ev(I,nouns);
\[\tag{I}\int_{0}^{1}{\left. {{x}^{2}}dx\right.}\] \[\tag{%o32} \int_{0}^{1}{\left. {{x}^{2}}dx\right.}=\frac{1}{3}\]

Here are several more examples of definite integrals:

--> I:'integrate(2^(2*x+1),x,-2,4);
I=ev(I,nouns);
\[\tag{I}\int_{-2}^{4}{\left. {{2}^{2 x+1}}dx\right.}\] \[\tag{%o34} \int_{-2}^{4}{\left. {{2}^{2 x+1}}dx\right.}=\frac{4095}{16 \log{(2)}}\]
--> I:'integrate(cos(x)^4,x,0,%pi/4)$
I=ev(I,nouns);
\[\tag{%o36} \int_{0}^{\frac{\ensuremath{\pi} }{4}}{\left. {{\cos{(x)}}^{4}}dx\right.}=\frac{3 \ensuremath{\pi} +8}{32}\]

 4 Improper Integrals at Infinity

When we want to compute improper integrals at ± infinity we use the bounds inf and minf respectively.  So:

(%i63) I:'integrate(1/(x^2+1),x,minf,inf)$ I=ev(I,nouns);
\[\tag{%o63} \int_{-\infty }^{\infty }{\left. \frac{1}{{{x}^{2}}+1}dx\right.}=\ensuremath{\pi} \]

Or:

(%i65) I:'integrate(exp(-x^2),x,minf,0)$ I=ev(I,nouns);
\[\tag{%o65} \int_{-\infty }^{0}{\left. {{\% e}^{-{{x}^{2}}}}dx\right.}=\frac{\sqrt{\ensuremath{\pi} }}{2}\]

Or even still:

(%i83) I:'integrate(exp(-x),x,log(4),inf)$ I=ev(I,nouns);
\[\tag{%o83} \int_{\log{(4)}}^{\infty }{\left. {{\% e}^{-x}}dx\right.}=\operatorname{gamma\_ incomplete}\left( 1,\log{(4)}\right) \]

Wait... what?!  Working with a CAS can sometimes be really frustrating.  The above is correct, but what does it mean?
To get a better feeling, we try:

(%i85) float(rhs(%o83));
\[\tag{%o85} 0.25\]

Oh... so it seems that the value is 1/4.  This behavior is not uncommon for Maxima, Maple, or Mathematica and is just
one of the frustrations that we must deal with when allowing technology to do the heavy lifting for us.

 5 Numerical Integration

Let's say we are not that interested in the symbolic value of a definite integral, but rather
the numberical value.  Maxima has several methods of numerical integration.  The most
useful and efficient is quad_qag().  quad_qag()??!!  Let's give it a try to see how it works:

(%i87) quad_qags(cos(x)^4,x,0,%pi/4);
\[\tag{%o87} [0.54452431127404,6.045434278445985 {{10}^{-15}},21,0]\]

quad_qags is a numerical method that uses approximating functions based on the technique of QUADrature to adaptively approximate the function so as to give a highly accurate approximation to the integral.  Notice that the
result comes as a list with 4 elements.  The first is the actual approximate value of the integral.
The second is the maximum error estimate.  The third is information on the number of quadratic functions
used to approximate the curve, and the last element gives an ERROR type code.

We will be most interested in the first two elements in this list.  If you just want the approximate value
of the integral, just ask for it:

--> quad_qags(cos(x)^4,x,0,%pi/4)[1];
\[\tag{%o57} 0.5445243112740431\]

If you want the error, ask for it as the second element in the list:

--> quad_qags(cos(x)^4,x,0,%pi/4)[2];
\[\tag{%o58} 6.045434278445985 {{10}^{-15}}\]

Of course, you could use float to approximate integrals:

--> float(integrate(cos(x)^4,x,0,1));
\[\tag{%o59} 0.5786742787280478\]

 5.1 Numerical Integration and Infinite Bounds

If you want to find a numerical estimate of an integral with infinite boundaries, then you must use
a variant of quad_qags() called quad_qagi().  Here are some examples:

(%i91) 'integrate(exp(-x^2),x,2,inf)=quad_qagi(exp(-x^2),x,2,inf)[1];
\[\tag{%o91} \int_{2}^{\infty }{\left. {{\% e}^{-{{x}^{2}}}}dx\right.}=0.0041455346903363\]
(%i93) 'integrate(1/(x^2+sin(x)),x,minf,-2)=quad_qagi(1/(x^2+sin(x)),x,minf,-2)[1];
\[\tag{%o93} \int_{-\infty }^{-2}{\left. \frac{1}{\sin{(x)}+{{x}^{2}}}dx\right.}=0.51906751828381\]

Note that quad_quagi() DOES NOT WORK ON FINITE INTERVALS!

(%i96) 'integrate(1/(x^2+sin(x)),x,-3,-2)=quad_qagi(1/(x^2+sin(x)),x,-3,-2)[1];
\[\mathit{\neq \{Lisp function\}}\mbox{: Unexpected limits of integration: }-3, -2\mbox{ -- an error. To debug this try: debugmode(true);}\]

In which case we must use quad_qags():

(%i97) 'integrate(1/(x^2+sin(x)),x,-3,-2)=quad_qags(1/(x^2+sin(x)),x,-3,-2)[1];
\[\tag{%o97} \int_{-3}^{-2}{\left. \frac{1}{\sin{(x)}+{{x}^{2}}}dx\right.}=0.19002652460848\]

Just for fun, let's try to compute the value of the above integral from -inf (minf) to inf (inf):

(%i99) 'integrate(1/(x^2+sin(x)),x,minf,inf)=quad_qagi(1/(x^2+sin(x)),x,minf,inf)[1];
\[\mbox{}\\\mbox{ ***MESSAGE FROM ROUTINE DQAGI IN LIBRARY SLATEC.}\mbox{}\\\mbox{ \cdot \cdot \cdot INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED}\mbox{}\\\mbox{ \cdot ABNORMAL RETURN}\mbox{}\\\mbox{ \cdot ERROR NUMBER = 5}\mbox{}\\\mbox{ \cdot }\mbox{}\\\mbox{ \cdot \cdot \cdot END OF MESSAGE}\] \[\tag{%o99} \int_{-\infty }^{\infty }{\left. \frac{1}{\sin{(x)}+{{x}^{2}}}dx\right.}=-0.62574579784912\]

Maxima sure did complain alot, but ultimately it gave us an answer.  Unfortunately , the answer that it
gave is garbage!  Error number 5 (when you read the manual for Maxima) is actually "integral is probably divergent..."
an indeed, this integral is divergent!  Just because technology furnishes us with an answer does not mean we
should inherently trust it!  To be fair, Maxima tried to let us know that this answer was extremely suspect!


Created with wxMaxima.