Could someone take me through the steps of decomposing 
$$\frac{2x^2+11x}{x^2+11x+30}$$
into partial fractions? 

More generally, how does one use partial fractions to compute integrals
$$\int\frac{P(x)}{Q(x)}\,dx$$
of rational functions ($P(x)$ and $Q(x)$ are polynomials) ?



This question is asked in an effort to cut down on duplicates. See Coping with *abstract* duplicate questions.

Also see List of Generalizations of Common Questions.

What you are trying to do is a partial fraction decomposition.
Idea. Imagine your calculator is broken and a bunch of hoodlums stop you on the street, and demand at knifepoint that you compute $\frac{191}{105}$ as a decimal (part of their initiation into the Mathies Gang, you see), or else they will slit your throat. Unfortunately, since your calculator is broken, you can really only do divisions if the divisor is a single digit number (so you can use your fingers to do the operations; you're very good with those, because you know the  multiplication tables of single digit numbers...). Luckily, you do notice that $105 = 3\times 5 \times 7$. Is there some way you can save your neck using this observation?
Well, you do know that $191 = 105 + 86$, so at least you know that $\frac{191}{105} = 1 + \frac{86}{105}$, so that takes care of the integer part of the fraction. What about $\frac{86}{105}$? Aha! Here's a clever idea: maybe $\frac{86}{105}$ is really the result of a sum of fractions! If you had a sum of fractions of the form
$$\frac{A}{3} + \frac{B}{5} + \frac{C}{7}$$
then to write it as a single fraction you would find the common denominator, $105$, and then do a bunch of operations, and end up with a fraction $\frac{\mathrm{something}}{105}$. If you can find an $A$, $B$, and $C$ so that the something is $86$, then instead of computing $\frac{86}{105}$ you can do $\frac{A}{3}$, $\frac{B}{5}$, and $\frac{C}{7}$ (which you can do, since the denominators are single digit numbers), and then add those decimals to get the answer. Can we? We do a bit of algebra:
$$\frac{A}{3} + \frac{B}{5} + \frac{C}{7} = \frac{35A + 21B + 15C}{105}$$
so you want $35A + 21B + 15C = 86$. As luck would have it, $A=B=1$ and $C=2$ works, so
$$\frac{86}{105} = \frac{1}{3} + \frac{1}{5} + \frac{2}{7}.$$
And now, all is well:
\begin{align*}
\frac{191}{105} &= 1 + \frac{86}{105}\\\
&= 1 + \frac{1}{3} + \frac{1}{5} + \frac{2}{7}\\\
&= 1 + (0.3333\ldots) + (0.2) + (0.285714285714\overline{285714}\ldots)
\end{align*}
and you can give those dangerous hoodlums their answer, and live to derive another day.

**Your problem.** You want to do something similar with the polynomial quotient, with denominators that "easy"; in this case, degree $1$. The first task is to make the fraction "less than $1$", by making sure the denominator has degree greater than the numerator. You do this with polynomial long division (see also this recent answer). Doing the long division mentally, we have: to get $2x^2$ from $x^2+11x+30$ we multiply by $2$:
$$2x^2 + 11x = (x^2+11x+30)(2+\cdots)$$
that produces unwanted $11x + 60$, (well, you get $22x + 60$, but you *do* want $11x$ of those, so you only have a leftover of $11x+60$); nothing to do about them, except cancel them out after the product is done. So you have
$$2x^2 + 11x = (x^2+11x+30)(2) - (11x+60).$$
So you can write
$$\frac{2x^2+11x}{x^2+11x+30} = 2 + \frac{-(11x+60)}{x^2+11x+30}.$$
Now we got the "integer part", and we work on the "fraction part". The denominator factors as $(x+5)(x+6)$, so we want to think of that fraction as the end result of doing a sum of the form
$$\frac{A}{x+5} + \frac{B}{x+6}.$$
Because the sum is "smaller than $1$" (numerator of degree smaller than the denominator) each of these fractions should also be "smaller than one". So both the $A$ and $B$ will be constants.
So we have:
\begin{align*}
\frac{-11x - 60}{x^2+11x+30} &= \frac{A}{x+5} + \frac{B}{x+6} \\\
&= \frac{A(x+6)}{(x+5)(x+6)} + \frac{B(x+5)}{(x+6)(x+5)}\\\
&= \frac{A(x+6) + B(x+5)}{(x+5)(x+6)}\\\
&= \frac{Ax + 6A + Bx + 5B}{x^2+11x+30} = \frac{(A+B)x + (6A+5B)}{x^2+11x+30}.
\end{align*}
For this to work out, you need $(A+B)x + (6A+5B) = -11x-60$. That means we need $A+B=-11$ (so the $x$s agree) and $6A+5B = -60$ (so the constant terms agree).
That means $A=-11-B$ (from the first equation). Plugging into the second equation, we get
$$-60 = 6A+5B = 6(-11-B)+5B = -66 -6B + 5B = -66-B.$$
So that means that $B=60-66 = -6$. And since $A+B=-11$, then $A=-5$.
(An alternative method for finding the values of $A$ and $B$ is the Heaviside cover-up method; from the fact that
$$-11x - 60 = A(x+6)+B(x+5)$$
we know that the two sides must take the same value for every value of $x$; if we plug in $x=-6$, this will "cover up" the $A$ on the right and we simply get $B(-6+5) = -B$; this must equal $-11(-6)-60 = 6$; so $6 = -B$, hence $B=-6$. Then plugging in $x=-5$ "covers up" the $B$ to give us $-11(-5)-60 = A(-5+6)$, or $-5=A$. So we obtain $A=-5$, $B=-6$, same as before.)
That is,
$$\frac{-11x-60}{x^2+11x+30} = \frac{-5}{x+5} + \frac{-6}{x+6}.$$
Putting it all together, we have:
$$\frac{2x^2+11x}{x^2+11x+30} = 2 + \frac{-11x-60}{x^2+11x+30} = 2 - \frac{5}{x+5} - \frac{6}{x+6}.$$
And <triumphant trumpet cue>ta da!</triumphant trumpet cue> Your task is done. You've decomposed the quotient into partial fractions.
Caveat. You need to be careful if the denominator has repeated factors, or has factors that are of degree $2$ and cannot be further factored (e.g., $x^2+1$); I talk about that below in the general discussion.
Now, let's hope the Mathies Gang doesn't ask for a proof of Goldbach's Conjecture from its next batch of pledges...

General Idea.
So let's discuss this idea for the general problem of integrating a rational function; that is, a function of the form
$$\frac{P(x)}{Q(x)}$$
where $P(x)=a_nx^n+\cdots +a_1x+a_0$ and $Q(x)=b_mx^m+\cdots + b_1x+b_0$ are polynomials.
Integrating polynomials is easy, so the first task is to do long division in order to write the fraction as a polynomial plus a rational function in which the numerator has degree smaller than the denominator. So we will consider only the case where $n\lt m$ going forward.
First, let us make sure we can actually do those fractions with "easy denominators." To me, "easy denominator" means (i) linear, $ax+b$; (ii) power of a linear polynomial, $(ax+b)^n$; (iii) irreducible quadratic; (iv) power of an irreducible quadratic. So let's talk about how to integrate those:

When $Q(x)$ has degree $1$ and $P(x)$ is constant. For example, something like
$$\int \frac{3}{2x-5}\,dx.$$
These integrals are very easy to do: we do a change of variable $u=2x-5$, so $du=2dx$. We simply get
$$\int\frac{3}{2x-5}\,dx = 3\int\frac{dx}{2x-5} = 3\int\frac{\frac{1}{2}du}{u} = \frac{3}{2}\ln|u|+C = \frac{3}{2}\ln|2x-5|+C.$$

In fact, the idea above works whenever the denominator is a power of a degree $1$ polynomial and the numerator is a constant. If we had something like
$$\int\frac{3}{(2x-5)^6}\,dx$$
then we can let $u=2x-5$, $du=2dx$ and we get
$$\begin{align*}
\int\frac{3}{(2x-5)^6}\,dx &= 3\int\frac{\frac{1}{2}du}{u^6} = \frac{3}{2}\int u^{-6}\,dx \\&= \frac{3}{2}\left(\frac{1}{-5}u^{-5}\right)+C\\ &= -\frac{3}{10}(2x-5)^{-5} + C.\end{align*}$$

What if the denominator is an irreducible quadratic? Things get a little more complicated. The simplest example of an irreducible quadratic is $x^2+1$, and the easiest numerator is $1$. That integral can be done directly:
$$\int\frac{1}{x^2+1}\,dx = \arctan(x)+C.$$
If we have an irreducible quadratic of the form $x^2+a$, with $a\gt 0$, then we can always write it as $x^2+b^2$ with $b=\sqrt{a}$; then we can do the following: factor out $b^2$ from the denominator,
$$\int\frac{dx}{x^2+b^2} = \int\frac{dx}{b^2((\frac{x}{b})^2 + 1)};$$
and now setting $u=\frac{x}{b}$, so $du = \frac{1}{b}\,dx$, we get:
$$\int\frac{dx}{x^2+b^2} = \frac{1}{b}\int\frac{1}{(\frac{x}{b})^2+1}\left(\frac{1}{b}\right)\,dx = \frac{1}{b}\int\frac{1}{u^2+1}\,du,$$
and now we can do the integral easily as before.
What if we have a more general irreducible quadratic denominator? Something like $x^2+x+1$, or something else with an $x$ term?


The magic phrase here is *Completing the square". We can write $x^2+x+1$ as
$$x^2 + x + 1 = \left(x^2 + x + \frac{1}{4}\right) + \frac{3}{4} = \left(x+\frac{1}{2}\right)^2 + \frac{3}{4}.$$
Then setting $w=x+\frac{1}{2}$, we end up with an integral that looks just like the previous case! For instance,
$$\int\frac{dx}{x^2+x+1} = \int\frac{dx}{(x+\frac{1}{2})^2+\frac{3}{4}} = \int\frac{dw}{w^2+\frac{3}{4}},$$
and we know how to deal with these.
So: if the denominator is an irreducible quadratic, and the numerator is a constant, we can do the integral.

What if the denominator is an irreducible quadratic, but the numerator is not constant? Since we can always do the long division, then we can take the numerator to be of degree $1$. If we are lucky, it's possible we can do it with a simple substitution; for example, to do
$$\int\frac{2x+3}{x^2+3x+4}\,dx$$
(note the denominator is irreducible quadratic), we can just let $u=x^2+3x+4$, since $du = (2x+3)\,dx$, exactly what we have in the numerator, so
$$\int\frac{2x+3}{x^2+3x+4}\,dx = \int\frac{du}{u} = \ln|u|+C = \ln|x^2+3x+4|+C = \ln(x^2+3x+4) + C.$$
If we are not lucky? Well, we can always make our own luck. For instace, if we had
$$\int\frac{3x}{x^2+3x+4}\,dx$$
then we can't just make the substitution $u=x^2+3x+4$; but if we wanted to do that anyway, we would need $2x+3$ in the numerator; so we play a little algebra game:
$$\begin{align*}
\frac{3x}{x^2+3x+4} &= 3\left(\frac{x}{x^2+3x+4}\right)\\
&= 3\left(\frac{\frac{1}{2}(2x)}{x^2+3x+4}\right)\\
&=\frac{3}{2}\left(\frac{2x}{x^2+3x+4}\right) &\text{(still not quite right)}\\
&= \frac{3}{2}\left(\frac{2x+3-3}{x^2+3x+4}\right)\\
&= \frac{3}{2}\left(\frac{2x+3}{x^2+3x+4} - \frac{3}{x^2+3x+4}\right).
\end{align*}$$
What have we accomplished? The first summand, $\frac{2x+3}{x^2+3x+4}$, is an integral we can do with a simple substitution; and the second summand, $\frac{3}{x^2+3x+4}$, is an integral that we just saw how to do! So in this way, we can solve this kind of integral. We can always rewrite the integral as a sum of an integral that we can do with a substitution, and an integral that is as in case 3 above.

What if the denominator is a power of an irreducible quadratic? Something like $(x^2+3x+5)^4$? If the numerator is of degree at most $1$, then we can play the same game as we just did to end up with a sum of two fractions; the first one will have numerator which is exactly the derivative of $x^2+3x+5$, and the second will have a numerator that is constant. So we just need to figure out how to do an integral like
$$\int\frac{2dx}{(x^2+3x+5)^5}$$
with constant numerator and a power of an irreducible quadratic in the denominator.


By completing the square as we did in 3, we can rewrite it so that it looks like
$$\int\frac{dw}{(w^2+b^2)^5}.$$
Turns out that if you do integration by parts, then you get a Reduction formula that says:
$$\int\frac{dw}{(w^2+b^2)^n} = \frac{1}{2b^2(n-1)}\left(\frac{w}{(w^2+b^2)^{n-1}} + (2n-3)\int\frac{dw}{(w^2+b^2)^{n-1}}\right)$$
By using this formula repeatedly, we will eventually end up in an integral where the denominator is just $w^2+b^2$... and we already know how to do those. So all is well with the world (with a lot of work, at least).
Okay. Do we now need to go and discuss what to do when the denominator is a cubic polynomial, then a fourth degree polynomial, then a fifth degree polynomial, etc.?
No! We can play the same game we did with fractions above, and take an arbitrary rational function and rewrite it as a sum of fractions, with each fraction a power of a degree 1 or an irreducible quadratic polynomial. The key to this is the Fundamental Theorem of Algebra, which says that every polynomial with real coefficients can be written as a product of linear and irreducible quadratic polynomials. In fact, one of the major reasons why people wanted to prove the Fundamental Theorem of Algebra was to make sure that we could do integrals of rational functions in the manner we are discussing.
So here is a method for integrating a rational function:

To compute
$$\int\frac{P(x)}{Q(x)}\,dx$$
where $P(x)$ and $Q(x)$ are polynomials:

If $\deg(P)$ is equal to or greater than $\deg(Q)$, perform long division and rewrite the fraction as a polynomial plus a proper fraction (with numerator of degree strictly smaller than the denominator). Integrate the polynomial (easy).

Completely factor the denominator $Q(x)$ into linear terms and irreducible quadratics. This can be very hard to do in practice. In fact, this step is the only obstacle to really being able to do these integrals always, easily. Factoring a polynomial completely and exactly can be very hard. The Fundamental Theorem of Algebra says that there is a way of writing $Q(x)$ that way, but it doesn't tell us how to find it.

Rewrite the fraction as a sum of fractions, each of which has a denominator which is either a power of linear polynomial or of an irreducible quadratic. (More about this below.)

Do the integral of each of the fractions as discussed above.



How do we rewrite as a sum?
Write $Q(x)$ as a product of powers of distinct polynomials, each linear or irreducible quadratic; e.g., $Q(x) = x(2x-1)(x+2)^3(x^2+1)(x^2+2)^2$.
For each power $(ax+b)^n$, use $n$ fractions of the form:
$$\frac{A_1}{ax+b} + \frac{A_2}{(ax+b)^2} + \cdots+\frac{A_n}{(ax+b)^n},$$
where $A_1,\ldots,A_n$ are constants-to-be-determined-later.
For each power of an irreducible quadratic, $(ax^2+bx+c)^m$, use $m$ fractions of the form:
$$\frac{C_1x+D_1}{ax^2+bx+c} + \frac{C_2x+D_2}{(ax^2+bx+c)^2} + \cdots + \frac{C_mx+D_m}{(ax^2+bx+c)^m},$$
where $C_1,D_1,\ldots,C_m,D_m$ are constants-to-be-determined-later.
So in the example above, we $Q(x) = x(2x-1)(x+2)^3(x^2+1)(x^2+2)^2$, we would get:
$$\begin{align*}
&\quad+\frac{A}{x} &\text{(corresponding to the factor }x\text{)}\\
&\quad+\frac{B}{2x+1} &\text{(corresponding to the factor }2x-1\text{)}\\
&\quad+\frac{C}{x+2} + \frac{D}{(x+2)^2}+\frac{E}{(x+2)^3} &\text{(corresponding to the factor }(x+2)^3\text{)}\\
&\quad+\frac{Gx+H}{x^2+1} &\text{(corresponding to the factor }x^2+1\text{)}\\
&\quad+\frac{Jx+K}{x^2+2} + \frac{Lx+M}{(x^2+2)^2}&\text{(corresponding to the factor }(x^2+2)^2\text{)}
\end{align*}$$
And now, the final step: how do we figure out what all those constants-to-be-determined-later are? We do the operation and compare it to the original! Let's say we are trying to calculate
$$\int\frac{3x-2}{x(x+2)^2(x^2+1)}\,dx$$
(not the same as above, but I want to do something small enough that we can do it).
We set it up as above; then we do the algebra. We have:
$$\begin{align*}
\frac{3x-2}{x(x+2)^2(x^2+1)} &= \frac{A}{x} + \frac{B}{x+2} + \frac{C}{(x+2)^2} + \frac{Dx+E}{x^2+1}\\
&= \frac{\small A(x+2)^2(x^2+1) + Bx(x+2)(x^2+1) + Cx(x^2+1) + (Dx+E)x(x+2)^2}{x(x+2)^2(x^2+1)}.
\end{align*}$$
Now we have two options: we can do the algebra in the numerator and write it as a polynomial. Then it has to be identical to $3x-2$. For example, the coefficient of $x^4$ in the numerator would be $A+B+D$, so we would need $A+B+D=0$; the constant term would be $4A$, so $4A=-2$; and so on.
The other method is the Heaviside cover-up method. Since the two expressions have to be the same, the numerator has to be the same as $3x-2$ when evaluated at every value of $x$. If we pick $x=0$ and plug it into the left numerator, we get $-2$; if we plug it into the right hand side, we get $A(2)^2(1) = 4A$, so $4A=-2$, which tells us what $A$ is. If we plug in $x=-2$, th left h and side is $3(-2)-2 = -8$, the right hand side is $C(-2)((-2)^2+1) = -10C$, so $-10C = -8$, which tells us what $C$ is; we can then simplify and continue doing this (you'll note I selected points where a lot of the summands on the right simply evaluate to $0$) until we get the value of all the coefficients.
And once we've found all the coefficients, we just break up the integral into a sum of integrals, each of which we already know how to do. And so, after a fair amount of work (but mainly just busy-work), we are done.
