# margin

Gain margin, phase margin, and crossover frequencies

## Syntax

`[Gm,Pm,Wgm,Wpm] = margin(sys) [Gm,Pm,Wgm,Wpm] = margin(mag,phase,w) margin(sys)`

## Description

`margin ` calculates the minimum gain margin, `Gm`, phase margin, `Pm`, and associated frequencies `Wgm` and `Wpm` of SISO open-loop models. The gain and phase margin of a system `sys` indicates the relative stability of the closed-loop system formed by applying unit negative feedback to `sys`, as in the following illustration.

The gain margin is the amount of gain increase or decrease required to make the loop gain unity at the frequency `Wgm` where the phase angle is –180° (modulo 360°). In other words, the gain margin is 1/g if g is the gain at the –180° phase frequency. Similarly, the phase margin is the difference between the phase of the response and –180° when the loop gain is 1.0. The frequency `Wpm` at which the magnitude is 1.0 is called the unity-gain frequency or gain crossover frequency. It is generally found that gain margins of three or more combined with phase margins between 30 and 60 degrees result in reasonable trade-offs between bandwidth and stability.

`[Gm,Pm,Wgm,Wpm] = margin(sys) ` computes the gain margin `Gm`, the phase margin `Pm`, and the corresponding frequencies `Wgm` and `Wpm`, given the SISO open-loop dynamic system model `sys`. `Wgm` is the frequency where the gain margin is measured, which is a –180 degree phase crossing frequency. `Wpm` is the frequency where the phase margin is measured, which is a 0dB gain crossing frequency. These frequencies are expressed in radians/`TimeUnit`, where `TimeUnit` is the unit specified in the `TimeUnit` property of `sys`. When `sys` has several crossovers, `margin` returns the smallest gain and phase margins and corresponding frequencies.

The phase margin `Pm` is in degrees. The gain margin `Gm` is an absolute magnitude. You can compute the gain margin in dB by

```Gm_dB = 20*log10(Gm) ```

`[Gm,Pm,Wgm,Wpm] = margin(mag,phase,w) ` derives the gain and phase margins from Bode frequency response data (magnitude, phase, and frequency vector). `margin` interpolates between the frequency points to estimate the margin values. Provide the gain data `mag` in absolute units, and phase data `phase` in degrees. You can provide the frequency vector `w` in any units; `margin` returns `Wgm` and `Wpm` in the same units.

 Note:   When you use `margin(mag,phase,w)`, `margin` relies on interpolation to approximate the margins, which generally produces less accurate results. For example, if there is no 0 dB crossing within the `w` range, `margin` returns a phase margin of `Inf`. Therefore, if you have an analytical model `sys`, using ```[Gm,Pm,Wgm,Wpm] = margin(sys)``` is the most robust way to obtain the margins.

`margin(sys)`, without output arguments, plots the Bode response of `sys` on the screen and indicates the gain and phase margins on the plot. By default, gain margins are expressed in dB on the plot.

## Examples

Gain and Phase Margins of Open-Loop Transfer Function

Create an open-loop discrete-time transfer function.

```hd = tf([0.04798 0.0464],[1 -1.81 0.9048],0.1) ```
```hd = 0.04798 z + 0.0464 --------------------- z^2 - 1.81 z + 0.9048 Sample time: 0.1 seconds Discrete-time transfer function. ```

Compute the gain and phase margins.

```[Gm,Pm,Wgm,Wpm] = margin(hd) ```
```Gm = 2.0517 Pm = 13.5711 Wgm = 5.4374 Wpm = 4.3544 ```

Display the gain and phase margins graphically.

```margin(hd) ```

Solid vertical lines mark the gain margin and phase margin. The dashed vertical lines indicate the locations of `Wpm`, the frequency where the phase margin is measured, and `Wgm`, the frequency where the gain margin is measured.

expand all

### Algorithms

The phase margin is computed using H theory, and the gain margin by solving $H\left(j\omega \right)=\overline{H\left(j\omega \right)}$ for the frequency ω.