Calculus 2, Lab 1: Partial Fractions#
The method of partial fraction decomposition is a useful tool for evaluating integrals of rational functions, and it comes up in other contexts as well, such as the method of Laplace transforms for differential equations. Partial fraction decomposition will also come in handy when you get to sequences and series: a partial fraction decomposition can reveal that a particular series is “telescoping”: parts of each term cancel with other terms.
A problem (which one quickly discovers) is that determining the coefficients for a partial fraction decomposition can be an exercise that is both tedious and error-prone. The brute force method (equating coefficients and solving a system of equations) is not particularly efficient.
If it turns out that partial fractions are going to be a part of your life, then you’d like to be able to handle them quickly and accurately. You have two options:
Let the computer do it for you
Develop systematic methods (formulas, even!) to get the job done
In this tutorial, we’ll see how the computer implements these methods for us, and you’ll have a chance to explore where they come from.
import sympy as sy
sy.init_printing()
x=sy.symbols('x')
Manipulating rational functions using Sympy.#
First, take a minute to search online (try search terms like “sympy partial fraction”).
You’ll probably discover some documentation, including details on the apart()
command. Other commands that you might find useful include factor()
and simplify()
.
(Note that “simplify” can take on many meanings, depending on the context. It does not always give you what you want.)
The factor()
command does exactly what you think: it attempts to factor any polynomial or rational expression you provide. The apart()
command performs partial fraction decomposition.
By default, these commands assume rational roots. You will want make sure you know how to use the extension
option in cases where you do not have rational roots. For example, to factor \(f(x)=x^2-2\), we would need to run a command like f.factor(extension=sy.sqrt(2))
.
Exercise 1. Demonstrate how things work with the following examples.#
For each example, first use the factor
command to determine the factors of the denominator (just for information purposes), and then use the apart
command to perform the decomposition.
We’re working with SymPy, so you can enter a function using the syntax f=expr(x)
, where expr(x)
is whatever expression in x defines the function. Remember that you need to use **
for exponents, and *
for multiplication, even by a coefficient.
(a) For the function \(f(x)=\dfrac{3x^2-2x+1}{x^4+2x^3+2x^2+2x+1}\)
# first, define your function
# now, factor using the factor() command
# finally, use the apart() command to get the partial fraction decomposition.
(b) For the function \(g(x)=\dfrac{2x+1}{x^3+2x^2-3x-6}\). (You may need to add an extension
.)
# define the function
# factor
# get partial fractions
(c) For the function \(h(x)=\dfrac{1}{(x^2-a^2)(x-b)^3}\)
(Don’t forget to define \(a\) and \(b\) as symbols! You will also need to specify that \(x\) is the variable in the apart
command.)
# define the function
# first you will need to define symbols a and b
# factor
# get partial fractions
(d) For the function \(r(x) = \dfrac{1}{ax^2+bx+1}\). (Example from Stack Overflow.)
(You might need to invoke full=True
here, and append with .doit()
to get a human-readable result. The only reason this example is here is because the result is hilariously awful.)
# define the function
# jump straight to the partial fractions, just to see what you get!
Exercise 2. Irreducible factors and complex roots#
The formulas we derived on the assignment deal with factors of the form \((x-a)^k\). What if there is an irreducible factor? It turns out these are only irreducible if we work over the real numbers. With complex numbers, we can factor any polynomial. For example, the factor \(x^2+1\) in question 2(a) above can be factored as \((x-i)(x+i)\), where \(i=\sqrt{-1}\). In SymPy, we can call the complex number \(i\) using sy.I
.
Try running the factor
and apart
commands for the function \(f(x)=\dfrac{3x^2-2x+1}{x^4+2x^3+2x^2+2x+1}\) from question 2(a) again, but this time, include the option extension=sy.I
.
# define the function
# factor, including the complex extention
# get the partial fraction decomposition
You may need to apply the simplify
command to get reaonable-looking complex expressions.
Finally, you might want to check that the two complex terms combine to give you the same thing as before. The output of the apart
command above should contain two terms with complex numbers in the denominator. In the code cell below, enter these two terms, naming them u
and v
, respectively.
# enter the first term with complex numbers
# enter the second term with complex numbers
Now, we want to combine these two terms into a single term with a common denominator. If you have two rational terms u
and v
that you would like to combine in this way, you can use the command together(u+v)
. Try this, and confirm that the result is the same as the term from 2(a) with the irreducible quadratic in the denominator.
Exercise 3. Finding formulas for the coefficients#
This problem involves some pencil-and-paper work. You can type up your results in this notebook (learning to do so can be very valuable!), or submit this work separately.
(a) To warm up, consider \(f(x) = \dfrac{2x}{(x-1)(x-3)}\). We seek a decomposition of the form \(f(x)=\dfrac{A}{x-1}+\dfrac{B}{x-3}\).
Show the following: for \(x\neq 1\), \((x-1)f(x) = A+B\dfrac{x-1}{x-3}\). It follows that \(A = \lim\limits_{x\to 1}(x-1)f(x)\).
Use this to compute the value of \(A\), and then derive a similar result to find the value of \(B\).
(b) Suppose that \(f(x) = \dfrac{p(x)}{(x-a)q(x)}\), where the degree of \(q(x)\) is less than or equal to the degree of \(p(x)\), and \(q(a)\neq 0\).
If \(A\) is the coefficient of \(\dfrac{1}{x-a}\) in the partial fraction decomposition of \(f(x)\), show that $\(A = \lim_{x\to a}(x-a)f(x) = \dfrac{p(a)}{q(a)}.\)$
(c) Now suppose that \(f(x) = \dfrac{p(x)}{(x-a)^kq(x)}\), where \(\deg(q(x))+k>\deg(p(x))\), and \(q(a)\neq 0\).
Suppose \(f(x)\) has a partial fraction decomposition of the form $\(f(x) = \frac{A_0}{(x-a)^k}+\frac{A_1}{(x-a)^{k-1}}+\cdots+\frac{A_{k-2}}{(x-a)^2}+\frac{A_{k-1}}{(x-a)}+\frac{r(x)}{q(x)},\)\( where \)r(x)$ is a remainder function.
Let \(g(x) = (x-a)^kf(x) = \dfrac{p(x)}{q(x)}\), and show that \(A_j = \dfrac{g^{(j)}(a)}{j!}\) for \(j=0,1,,\ldots, k-1\).
(d) Let \(f(x) = \dfrac{x^2+3x+2}{(x-2)^4(x+1)}\). First, determine the partial fraction decomposition of \(f(x)\) using the apart()
command.
# define the function
# get the decomposition
Second, use SymPy, and the formulas from parts (b) and (c), to compute the coefficients of the partial fraction decomposition, and confirm that your results agree with the ones obtained from the apart()
command.
# several code cells are provided; add more if you need them!
# remember that f.diff() computes a derivative, and f.subs(x,a) will substitute x with the number a.