Why do I get a math error for this when trying to compute $\int_{-1}^1(x^2-\frac{1}{x^2}+3) \, dx$?

Well, what happends to:

$$f(x)=\frac{1}{x^2}$$

When $x\in\left[-1,1\right]$?


Say $F$ is an antiderivative of $f$—that is, if you differentiate $F$, then you get $f$. What you're doing to compute the integral is the following: $$ \int_{a}^{b} f(x) \, dx = [F(x)]_a^b=F(b)-F(a) \, . $$ This method of evaluating integrals is an application of the fundamental theorem of calculus—which connects antiderivatives with integrals. To be clear, $$ \int_{a}^{b} f(x) \, dx $$ represents the area under the graph of $f(x)$ between $x=a$ and $x=b$. Meanwhile, $$ \int f(x) \, dx $$ represents the family of functions which, when differentiated, gives you $f(x)$. It should surprise you that these two ideas should should be so intimately connected. (This also explains why the integral and antiderivative notations are so similar.) However, the fundamental theorem of calculus only applies in certain cases. The example you gave, $$ \int_{-1}^1 x^2-\frac{1}{x^2}+3 \, dx \, , $$ is not one of those cases, since the function is undefined at $x=0$. This is a problem many students have: they are so used to applying the fundamental theorem of calculus that they don't realise when it doesn't apply. In fact, $$ \int_{-1}^1 x^2-\frac{1}{x^2}+3 \, dx $$ is not even an integral at all in the strict sense of the word. Just take a look at the graph of $y=x^2-\frac{1}{x^2}+3$:

Graph of y=x^2-1/x^2+3

What would 'the area under the graph' mean to you in this case? What we are dealing with is called an 'improper integral'. The example you gave $$ \int_{-1}^1 x^2-\frac{1}{x^2}+3 \, dx \, , $$ is merely a shorthand for $$ \lim_{a\to0^-}\int_{-1}^{a} x^2-\frac{1}{x^2}+3 \, dx + \lim_{b \to 0^+}\int_{b}^{1} x^2-\frac{1}{x^2}+3 \, dx \, . $$ Since neither of these limits exist, the 'integral' is said to be divergent. Here are some useful links:

  • The Wikipedia articles on the fundamental theorem of calculus and improper integrals.
  • I also gave an introduction to improper integrals here.