source: http://calculuswithjulia.github.io/precalc/rational_functions.html

Rational functions

A rational expression is the ratio of two polynomial expressions.

Rational functions

The rational numbers are simply ratios of integers, of the form \(p/q\) for non-zero \(q\). A rational function is a ratio of polynomial functions of the form \(p(x)/q(x)\), again \(q\) is non-zero, but may have zeros.

We know that polynomials have nice behaviors due to the following facts:

  • Behaviors at \(-\infty\), \(\infty\) are known just from the leading term.
  • There are possible wiggles up and down, the exact behavior depends on intermediate terms, but there can be no more than \(n-1\) wiggles.
  • The number of zeros is no more than \(n\), the degree of the polynomial.

Rational functions are not quite so nice:

  • behavior at \(-\infty\) and \(-\infty\) can be like a polynomial of any degree, including constant
  • behavior at any value \(x\) can blow up due to division by 0 - rational functions, unlike polynomials, are not always defined
  • The function may or may not cross zero, even if the range includes every other point, as the graph of \(f(x)=1/x\) will show.
using CalculusWithJulia   # to load the `Plots` and `SymPy` packages
f(x)=(x-1)^2*(x-2) / ((x+3)*(x-3))
## f (generic function with 1 method)
plot(f, -10, 10, ylims=(-3000,4500))

plot(f, -100, 100)

Asympototes

Formally, an asymptote of a curve is a line such that the distance between the curve and the line approaches \(0\) as they tend to infinity. Tending to infity can happen as \(x \rightarrow \pm \infty\) or \(y \rightarrow \pm \infty\), the former being related to horizontal asymtotes or slant asymtotes, the latter being related to vertical asymptotes.

Behaviour as \(a \rightarrow \infty\) or \(x \rightarrow -\infty\)

@vars x real=true
## (x,)
a = (x-1)^2*(x-2)
##                2
## (x - 2)*(x - 1)
b = (x+3)*(x-3)
## (x - 3)*(x + 3)

Euclid’s division algorithm can be used for polynomials \(a(x)\) and \(b(x)\) to produce \(q(x)\) and \(r(x)\) with \(a = b\cdot q + r\) and the degree of \(r(x)\) is less than the degree of \(b(x)\). This is direct analogy to the division algorithm of integers, only there the value of the remainder, \(r(x)\), satisfies \(0 \leq r < b\). Given \(q(x)\) and \(r(x)\) above, we can reexpress the rational function

\[ \frac{a(x)}{b(x)} = q(x) + \frac{r(x)}{b(x)}. \]

q, r = divrem(a, b)
## (floor((x - 2)*(x - 1)^2/((x - 3)*(x + 3))), -(x - 3)*(x + 3)*floor((x - 2)*(x - 1)^2/((x - 3)*(x + 3))) + (x - 2)*(x - 1)^2)
q = r/b
##                        /               2\                   
##                        |(x - 2)*(x - 1) |                  2
## - (x - 3)*(x + 3)*floor|----------------| + (x - 2)*(x - 1) 
##                        \(x - 3)*(x + 3) /                   
## ------------------------------------------------------------
##                       (x - 3)*(x + 3)
f(x)=(x-1)^2*(x-2)/((x+3)*(x-3))  # as a function
## f (generic function with 1 method)
p = f(x) # a symbolic expression
##                2
## (x - 2)*(x - 1) 
## ----------------
## (x - 3)*(x + 3)
apart(p)
##             40          2    
## x - 4 + --------- + ---------
##         3*(x + 3)   3*(x - 3)
plot(apart(p) - (x-4), 10, 100)

cancel(p)
##  3      2          
## x  - 4*x  + 5*x - 2
## -------------------
##         2          
##        x  - 9
p = (x^5 - 2x^4 + 3x^3 - 4x^2 + 5) / (5x^4 + 4x^3 + 3x^2 + 2x + 1)
##  5      4      3      2     
## x  - 2*x  + 3*x  - 4*x  + 5 
## ----------------------------
##    4      3      2          
## 5*x  + 4*x  + 3*x  + 2*x + 1
apart(p)
##             3       2                     
## x      116*x  - 68*x  + 23*x + 139      14
## - + --------------------------------- - --
## 5      /   4      3      2          \   25
##     25*\5*x  + 4*x  + 3*x  + 2*x + 1/
a = 5x^3 + 6x^2 +2
##    3      2    
## 5*x  + 6*x  + 2
b = x-1
## x - 1
q, r = divrem(a, b)
## (floor((5*x^3 + 6*x^2 + 2)/(x - 1)), 5*x^3 + 6*x^2 - (x - 1)*floor((5*x^3 + 6*x^2 + 2)/(x - 1)) + 2)
plot(a/b, -3, 3)

plot!(q)

plot(a/b, 5, 10)

plot!(q)

Partial fractions

The apart function was useful to express a rational function in terms of a polynomial plus additional rational functions whose horizontal asymptotes are \(0\).

x = symbols("x")
## x
p = (x-1)*(x-2)
## (x - 2)*(x - 1)
q = (x-3)^3 * (x^2 - x - 1)
##        3 / 2        \
## (x - 3) *\x  - x - 1/
apart(p/q)
##     2*x - 1           2            1            2     
## --------------- - ---------- + ---------- + ----------
##    / 2        \   25*(x - 3)            2            3
## 25*\x  - x - 1/                5*(x - 3)    5*(x - 3)

Vertical asymptotes

plot(1/x, -1, 1)

f(x) = (x-1)^2 * (x-2) / ((x+3)*(x-3) )
## f (generic function with 1 method)
f(3), f(-3)
## (Inf, -Inf)
function trimplot(f, a, b, c=20; kwargs...)
   fn = x -> abs(f(x)) < c ? f(x) : NaN
   plot(fn, a, b; kwargs...)
end
trimplot(f, -25, 25, 30)

Sign charts

When sketching graphs of rational functions by hand, it is useful to use sign charts. A sign chart of a function indicates when the function is positive, negative, \(0\), or underfined.

function signchart(f, a, b)
   xs = range(a, stop=b, length=200)
   ys = f.(xs)
   cols = [fx < 0 ? :red  : :blue for fx in ys]
   plot(xs, ys, color=cols, linewidth=5, legend=false)
   plot!(zero)
   end
f(x) = x^3 - x
## f (generic function with 1 method)
signchart(f, -3/2, 3/2)

Pade approximate

One area where rational functions are employed is in approximating functions.

sin_p(x) = (x - (7/60)*x^3) / (1 + (1/20)*x^2)
## sin_p (generic function with 1 method)
tan_p(x) = (x - (1/15)*x^3) / (1 - (2/5)*x^2)
## tan_p (generic function with 1 method)
plot(sin, -pi, pi)

plot!(sin_p)

plot(tan, -pi/2 + 0.2, pi/2 - 0.2)

plot!(tan_p)