%% Partial Fraction Expansion
% Partial fraction expansion simplifies a fraction of two polynomials by writing the fraction as a sum of simple fractions. This makes the expression easier to integrate or transform.
%% Definition and Implementation Using residue
% Consider the fraction F(s) of two polynomials b and a of degree n and m, respectively
% F(s) = \frac{b(s)}{a(s)} = \frac{b_ns^n+...+b_2s^2+b_1s+b_0}{a_ms^m+...+a_2s^2+a_1s+a_0}.
% The fraction F(s) can be represented as a sum of simple fractions
% F(s) = \frac{b(s)}{a(s)} = \frac{r_m}{s-p_m}+...+\frac{r_2}{s-p_2}+\frac{r_1}{s-p_1}+k(s).
% This sum is the partial fraction expansion of F. The values r_m,...,r_1 are the residues, the values p_m,...,p_1 are the poles, and k(s) is a polynomial in s. For most textbook problems, k(s) is 0 or a constant.
% The residue function finds the partial fraction expansion of F(s). The syntax is residue(b,a) where b and a are vectors of coefficients of b(s) and a(s), specified as b = [bn,...,b0] and a = [am,...,a0]. The output arguments are r, p, and k, which specify the residues, poles, and polynomial in s as
% r = [rM ... r2 r1]
% p = [pM ... p2 p1]
% k = [kN ... k2 k1]
% The output arguments use the syntax [r,p,k] = residue(b,a).
%% Expansion with Real Roots
% Consider a fraction of two polynomials
% F(s)=\frac{b(s)}{a(s)}=\frac{-4s+8}{s^2+6s+8}.
% The partial fraction expansion of F(s) is the sum of simple fractions
% \frac{-4s+8}{s^2+6s+8}=\frac{-12}{s+4}+\frac{8}{s+2}.
% Represent F(s) in terms of the coefficients of b(s) and a(s).

b = [-4 8];
a = [1 6 8];

%
% Find the partial fraction expansion using residue with b and a as inputs. The returned values of r, p, and k represent the partial fraction expansion.

[r,p,k] = residue(b,a)

%
% Convert the partial fraction expansion back to polynomial form using r, p, and k as inputs to residue.

[b2,a2] = residue(r,p,k)

%
%% Expansion with Complex Roots and Equal Degree of Numerator and Denominator
% If the degree of the numerator is equal to the degree of the denominator, the output k can be nonzero.
% Consider a fraction of two polynomials F(s) with complex roots and equal degree of numerator and denominator, where F(s) is
% F(s) = \frac{b(s)}{a(s)} = \frac{2s^3+s^2}{s^3+s+1}.
% Find the partial fraction expansion of F(s).

b = [2 1 0 0];
a = [1 0 1 1];
[r,p,k] = residue(b,a)

% residue returns the complex roots and poles, and a constant term in k, representing the partial fraction expansion
% F(s) = \frac{b(s)}{a(s)} = \frac{2s^3+s^2}{s^3+s^2+1} =
% \frac{0.5354+1.0390i}{s-(0.3412+1.1615i)} + \frac{0.5354-1.0390i}{s-(0.3412-1.1615i)} +
% \frac{-0.0708}{s+0.6823}+2.
%
%% Expansion with Numerator Degree Greater Than Denominator Degree
% When the degree of the numerator is greater than the degree of the denominator, k is a vector that represents the coefficients of a polynomial in s.
% Find the partial fraction expansion of a fraction where the degree of the numerator is larger than the degree of the denominator.

b = [2 0 0 1 0];
a = [1 0 1];
[r,p,k] = residue(b,a)

% k represents the polynomial 2s^2-2, where r, p, and k represent the partial fraction expansion
% F(s) = \frac{b(s)}{a(s)} = \frac{0.5-1i}{s-1i} + \frac{0.5+1i}{s+1i} + 2s^2-2.
% Copyright 2015 The MathWorks, Inc.