# Mason's gain formula

Mason's gain formula (MGF) is a method for finding the transfer function of a linear signal-flow graph (SFG). The formula was derived by Samuel Jefferson Mason, for whom it is named. MGF is an alternate method to finding the transfer function algebraically by labeling each signal, writing down the equation for how that signal depends on other signals, and then solving the multiple equations for the output signal in terms of the input signal. MGF provides a step by step method to obtain the transfer function from a SFG. Often, MGF can be determined by inspection of the SFG. The method can easily handle SFGs with many variables and loops including loops with inner loops. MGF comes up often in the context of control systems, microwave circuits and digital filters because these are often represented by SFGs.

## Formula

The gain formula is as follows:
    $G={\frac {y_{\text{out}}}{y_{\text{in}}}}={\frac {\sum _{k=1}^{N}{G_{k}\Delta _{k}}}{\Delta \ }}$
    $\Delta =1-\sum L_{i}+\sum L_{i}L_{j}-\sum L_{i}L_{j}L_{k}+\cdots +(-1)^{m}\sum \cdots +\cdots$
where:
* Δ = the determinant of the graph.
* y$_{in}$ = input-node variable
* y$_{out}$ = output-node variable
* G = complete gain between y$_{in}$ and y$_{out}$
* N = total number of forward paths between y$_{in}$ and y$_{out}$
* G$_k$ = path gain of the kth forward path between y$_{in}$ and y$_{out}$
* L$_i$ = loop gain of each closed loop in the system
* L$_i$L$_j$ = product of the loop gains of any two non-touching loops (no common nodes)
* L$_i$L$_j$L$_k$ = product of the loop gains of any three pairwise nontouching loops
* Δ$_k$ = the cofactor value of Δ for the kth forward path, with the loops touching the kth forward path removed.

### Definitions

Source:
* Path: a continuous set of branches traversed in the direction that they indicate.
* Forward path: A path from an input node to an output node in which no node is touched more than once.
* Loop: A path that originates and ends on the same node in which no node is touched more than once.
* Path gain: the product of the gains of all the branches in the path.
* Loop gain: the product of the gains of all the branches in the loop.

### Procedure to find the solution

* Make a list of all forward paths, and their gains, and label these G$_k$.
* Make a list of all the loops and their gains, and label these L$_i$ (for i loops). Make a list of all pairs of non-touching loops, and the products of their gains (L$_i$L$_j$). Make a list of all pairwise non-touching loops taken three at a time (L$_i$L$_j$L$_k$), then four at a time, and so forth, until there are no more.
* Compute the determinant Δ and cofactors Δ$_k$.
* Apply the formula.

## Examples
### Circuit containing two-port

The transfer function from V$_{in}$ to V$_2$ is desired.
There is only one forward path:
   * V$_{in}$ to V$_1$ to I$_2$ to V$_2$ with gain $G_{1}=-y_{21}R_{L}\,$
There are three loops:
   * V$_1$ to I$_1$ to V$_1$ with gain $L_{1}=-R_{\text{in}}y_{11}\,$
   * V$_2$ to I$_2$ to V$_2$ with gain $L_{2}=-R_{L}y_{22}\,$
   * V$_1$ to I$_2$ to V$_2$ to I$_1$ to V$_1$ with gain $L_{3}=y_{21}R_{L}y_{12}R_{\text{in}}\,$
    $\Delta =1-(L_{1}+L_{2}+L_{3})+(L_{1}L_{2})\,$ note: L$_1$ and L$_2$ do not touch each other whereas L$_3$ touches both of the other loops.
    $\Delta _{1}=1\,$ note: the forward path touches all the loops so all that is left is 1.
    $G={\frac {G_{1}\Delta _{1}}{\Delta }}={\frac {-y_{21}R_{L}}{1+R_{\text{in}}y_{11}+R_{L}y_{22}-y_{21}R_{L}y_{12}R_{\text{in}}+R_{\text{in}}y_{11}R_{L}y_{22}}}\,$

### Digital IIR biquad filter

Digital filters are often diagramed as signal flow graphs.
    There are two loops
   * $L_{1}=-a_{1}Z^{-1}\,$
   * $L_{2}=-a_{2}Z^{-2}\,$
    $\Delta =1-(L_{1}+L_{2})\,$ Note, the two loops touch so there is no term for their product.
    There are three forward paths
   * $G_{0}=b_{0}\,$
   * $G_{1}=b_{1}Z^{-1}\,$
   * $G_{2}=b_{2}Z^{-2}\,$
    All the forward paths touch all the loops so $\Delta _{0}=\Delta _{1}=\Delta _{2}=1\,$
    $G={\frac {G_{0}\Delta _{0}+G_{1}\Delta _{1}+G_{2}\Delta _{2}}{\Delta }}\,$
    $G={\frac {b_{0}+b_{1}Z^{-1}+b_{2}Z^{-2}}{1+a_{1}Z^{-1}+a_{2}Z^{-2}}}\,$

### Servo

The signal flow graph has six loops. They are:
   * $L_{0}=-{\frac {\beta }{sM}}\,$
   * $L_{1}={\frac {-(R_{M}+R_{S})}{sL_{M}}}\,$
   * $L_{2}=\,{\frac {-G_{M}K_{M}}{s^{2}L_{M}M}}$
   * $L_{3}={\frac {-K_{C}R_{S}}{sL_{M}}}\,$
   * $L_{4}={\frac {-K_{V}K_{C}K_{M}G_{T}}{s^{2}L_{M}M}}\,$
   * $L_{5}={\frac {-K_{P}K_{V}K_{C}K_{M}}{s^{3}L_{M}M}}\,$
    $\Delta =1-(L_{0}+L_{1}+L_{2}+L_{3}+L_{4}+L_{5})+(L_{0}L_{1}+L_{0}L_{3})\,$
There is one forward path:
   * $g_{0}={\frac {K_{P}K_{V}K_{C}K_{M}}{s^{3}L_{M}M}}\,$
The forward path touches all the loops therefore the co-factor $\Delta _{0}=1$
And the gain from input to output is ${\frac {\theta _{L}}{\theta _{C}}}={\frac {g_{0}\Delta _{0}}{\Delta }}\,$

## Equivalent matrix form

Mason's rule can be stated in a simple matrix form. Assume $\mathbf {T}$ is the transient matrix of the graph where $t_{nm}=\left[\mathbf {T} \right]_{nm}$ is the sum transmittance of branches from node m toward node n. Then, the gain from node m to node n of the graph is equal to $u_{nm}=\left[\mathbf {U} \right]_{nm}$, where
    $\mathbf {U} =\left(\mathbf {I} -\mathbf {T} \right)^{-1}$,
and $\mathbf {I}$ is the identity matrix.
Mason's Rule is also particularly useful for deriving the z-domain transfer function of discrete networks that have inner feedback loops embedded within outer feedback loops (nested loops). If the discrete network can be drawn as a signal flow graph, then the application of Mason's Rule will give that network's z-domain H(z) transfer function.

## Complexity and computational applications

Mason's Rule can grow factorially, because the enumeration of paths in a directed graph grows dramatically. To see this consider the complete directed graph on $n$ vertices, having an edge between every pair of vertices. There is a path form $y_{\text{in}}$ to $y_{\text{out}}$ for each of the $(n-2)!$ permutations of the intermediate vertices. Thus Gaussian elimination is more efficient in the general case.
Yet Mason's rule characterizes the transfer functions of interconnected systems in a way which is simultaneously algebraic and combinatorial, allowing for general statements and other computations in algebraic systems theory. While numerous inverses occur during Gaussian elimination, Mason's rule naturally collects these into a single quasi-inverse. General form is
       ${\frac {p}{1-q}},$
Where as described above, $q$ is a sum of cycle products, each of which typically falls into an ideal (for example, the strictly causal operators). Fractions of this form make a subring $R(1+\langle L_{i}\rangle )^{-1}$ of the rational function field. This observation carries over to the noncommutative case, even though Mason's rule itself must then be replaced by Riegle's rule.